Skip to content

Commit

Permalink
chore(rust): minor improvements in command docs
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Jan 23, 2025
1 parent 00f1f85 commit 888cba0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
25 changes: 18 additions & 7 deletions implementations/rust/ockam/ockam_command/src/bin/brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ static CRATE_DIR: Lazy<PathBuf> = Lazy::new(|| {

static BIN_DIR: Lazy<PathBuf> = Lazy::new(|| CRATE_DIR.join("src/bin"));

/// Builds the binaries with the passed configuration
/// How to run:
/// `cargo run --bin brand ./path/to/config.yaml --release`
/// Builds the binaries with the passed configuration:
/// `cargo run --bin brand ./path/to/config.yaml`
/// `cargo run --bin brand "{bin1: {brand_name: "Name"}}"`
fn main() -> Result<()> {
// first argument: inline config or path to config file
Expand Down Expand Up @@ -93,7 +92,7 @@ fn build_binary(bin_name: &str, brand_settings: Brand) -> Result<()> {
cmd.env(OCKAM_COMMANDS, env_value);
}
if let Some(build_args) = brand_settings.build_args {
cmd.args(build_args.split_whitespace());
cmd.args(build_args);
}

let res = cmd
Expand Down Expand Up @@ -154,7 +153,7 @@ struct Brand {
orchestrator_identifier: Option<String>,
orchestrator_address: Option<String>,
commands: Option<Vec<Command>>,
build_args: Option<String>,
build_args: Option<Vec<String>>,
}

impl Display for Brand {
Expand Down Expand Up @@ -205,7 +204,10 @@ impl Display for Brand {
writeln!(
f,
"{}",
fmt_log!("{PADDING}build args {}", color_primary(build_args))
fmt_log!(
"{PADDING}build args {}",
color_primary(build_args.join(" "))
)
)?;
}
Ok(())
Expand Down Expand Up @@ -238,6 +240,10 @@ mod tests {
bin2:
support_email: bin2@support.io
brand_name: Brand2
build_args:
- --release
- --target
- armv7-unknown-linux-gnueabihf
"#;
let parsed: Config = serde_yaml::from_str(config).unwrap();
assert_eq!(parsed.items.len(), 2);
Expand All @@ -248,7 +254,8 @@ mod tests {
processed.process_defaults().unwrap();

// No defaults used, should be the same as parsed
assert_eq!(parsed.items["bin1"], processed.items["bin1"]);
let bin1 = &processed.items["bin1"];
assert_eq!(&parsed.items["bin1"], bin1);

// Check bin2 defaults
let bin2 = &processed.items["bin2"];
Expand All @@ -258,5 +265,9 @@ mod tests {
assert_eq!(bin2.orchestrator_identifier.as_deref(), None);
assert_eq!(bin2.orchestrator_address.as_deref(), None);
assert_eq!(bin2.commands.as_deref(), None);
assert_eq!(
bin2.build_args.clone().unwrap(),
vec!["--release", "--target", "armv7-unknown-linux-gnueabihf",]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ bin:
home_dir: /home/brand1 # if not set, will default to $HOME/.bin
orchestrator_identifier: brand1 # if not set, will default to the OCKAM_CONTROLLER_IDENTITY_ID env var
orchestrator_address: brand1.network # if not set, will default to the OCKAM_CONTROLLER_ADDR env var
build_args: # if not set, will default to empty string
- --release
commands:
- node: "host"
- node_list
Expand Down
21 changes: 10 additions & 11 deletions implementations/rust/ockam/ockam_command/src/branding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl Debug for Command {
}

impl Commands {
pub fn from_env() -> Result<Self> {
let commands = COMMANDS
fn new(commands: &str) -> Result<Self> {
let commands = commands
.split(',')
.filter_map(|c| {
if c.is_empty() {
Expand All @@ -55,6 +55,10 @@ impl Commands {
Ok(Self { commands })
}

pub fn from_env() -> Result<Self> {
Self::new(COMMANDS)
}

pub fn hide(&self, command_name: &str) -> bool {
if self.commands.is_empty() {
return false;
Expand All @@ -77,33 +81,28 @@ impl Commands {
#[cfg(test)]
mod tests {
use super::*;
use crate::OCKAM_COMMANDS;

#[test]
fn test_hide() {
std::env::set_var(OCKAM_COMMANDS, "node create=host create,project,enroll");
let commands = Commands::from_env().unwrap();
let commands = Commands::new("node create=host create,project,enroll").unwrap();
assert!(!commands.hide("node create"));
assert!(!commands.hide("project"));
assert!(!commands.hide("enroll"));
assert!(commands.hide("command4"));

std::env::set_var(OCKAM_COMMANDS, "");
let commands = Commands::from_env().unwrap();
let commands = Commands::new("").unwrap();
assert!(!commands.hide("command1"));
}

#[test]
fn test_commands() {
std::env::set_var(OCKAM_COMMANDS, "node create=host create,project,enroll");
let commands = Commands::from_env().unwrap();
let commands = Commands::new("node create=host create,project,enroll").unwrap();
assert_eq!(commands.name("node create"), "host create");
assert_eq!(commands.name("project"), "project");
assert_eq!(commands.name("enroll"), "enroll");
assert_eq!(commands.name("command4"), "command4");

std::env::set_var(OCKAM_COMMANDS, "");
let commands = Commands::from_env().unwrap();
let commands = Commands::new("").unwrap();
assert_eq!(commands.name("command1"), "command1");
}
}
26 changes: 14 additions & 12 deletions implementations/rust/ockam/ockam_command/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const PREVIEW_TAG: &str = include_str!("./static/preview_tag.txt");
const UNSAFE_TOOLTIP_TEXT: &str = include_str!("./static/unsafe_tooltip.txt");
const UNSAFE_TAG: &str = include_str!("./static/unsafe_tag.txt");

static IS_MARKDOWN: Lazy<bool> =
Lazy::new(|| get_env_with_default("OCKAM_HELP_RENDER_MARKDOWN", false).unwrap_or(false));

static HIDE: Lazy<bool> =
Lazy::new(|| get_env_with_default("OCKAM_HELP_SHOW_HIDDEN", true).unwrap_or(true));

static HEADER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(Examples:|Learn More:|Feedback:).*$".into()));

static FOOTER: Lazy<String> = Lazy::new(|| {
if BIN_NAME == "ockam" {
"
Expand Down Expand Up @@ -41,15 +50,8 @@ If you have questions, please email us on {SUPPORT_EMAIL}"
}
});

static HEADER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new("^(Examples|Learn More|Feedback):$".into()));

fn is_markdown() -> bool {
get_env_with_default("OCKAM_HELP_RENDER_MARKDOWN", false).unwrap_or(false)
}

pub(crate) fn hide() -> bool {
get_env_with_default("OCKAM_HELP_SHOW_HIDDEN", true).unwrap_or(true)
*HIDE
}

pub(crate) fn about(text: &str) -> &'static str {
Expand All @@ -58,7 +60,7 @@ pub(crate) fn about(text: &str) -> &'static str {

pub(crate) fn before_help(text: &str) -> &'static str {
let mut processed = String::new();
if is_markdown() {
if *IS_MARKDOWN {
if let Some(s) = enrich_preview_tag(text) {
processed.push_str(&s);
}
Expand All @@ -73,7 +75,7 @@ pub(crate) fn before_help(text: &str) -> &'static str {

pub(crate) fn after_help(text: &str) -> &'static str {
let mut processed = String::new();
if is_markdown() {
if *IS_MARKDOWN {
processed.push_str("### Examples\n\n");
processed.push_str(text);
} else {
Expand All @@ -88,7 +90,7 @@ pub(crate) fn after_help(text: &str) -> &'static str {
/// Otherwise, if it is a Markdown document just return a static string
fn render(body: &str) -> &'static str {
let body = process_branding(body);
if is_markdown() {
if *IS_MARKDOWN {
Box::leak(body.into_boxed_str())
} else {
let syntax_highlighted = process_terminal_docs(body);
Expand Down Expand Up @@ -117,7 +119,7 @@ fn process_terminal_docs(input: String) -> String {
for line in LinesWithEndings::from(&input) {
// Bold and underline known headers
if HEADER_RE.is_match(line) {
output.push(line.to_string().bold().underlined().to_string());
output.push(line.bold().underlined().to_string());
}
// Underline H4 headers
else if line.starts_with("#### ") {
Expand Down

0 comments on commit 888cba0

Please sign in to comment.