-
-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust): add missing branding replacements in ockam_command
- Loading branch information
1 parent
3c39d90
commit ee4a34e
Showing
17 changed files
with
219 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::fmt::{Debug, Formatter}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Commands { | ||
pub(crate) commands: Vec<Command>, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct Command { | ||
pub(crate) name: String, | ||
pub(crate) custom_name: String, | ||
} | ||
|
||
impl Debug for Command { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Command") | ||
.field("name", &self.name) | ||
.field("custom_name", &self.custom_name) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Commands { | ||
pub fn new(commands: &str) -> Self { | ||
let commands = commands | ||
.split(',') | ||
.filter_map(|c| { | ||
if c.is_empty() { | ||
return None; | ||
} | ||
let mut parts = c.split('='); | ||
let name = match parts.next() { | ||
Some(name) => name, | ||
None => return None, | ||
}; | ||
let custom_name = parts.next().unwrap_or(name); | ||
Some(Command { | ||
name: name.to_string(), | ||
custom_name: custom_name.to_string(), | ||
}) | ||
}) | ||
.collect(); | ||
Self { commands } | ||
} | ||
|
||
pub fn hide(&self, command_name: &'static str) -> bool { | ||
if self.commands.is_empty() { | ||
return false; | ||
} | ||
// we only support custom names for top-level commands | ||
if command_name.contains(" ") { | ||
return false; | ||
} | ||
!self.commands.iter().any(|c| c.name == command_name) | ||
} | ||
|
||
pub fn name(&self, command_name: &'static str) -> &'static str { | ||
if self.commands.is_empty() { | ||
return command_name; | ||
} | ||
// we only support custom names for top-level commands | ||
if command_name.contains(" ") { | ||
return command_name; | ||
} | ||
self.commands | ||
.iter() | ||
.find(|c| c.name == command_name) | ||
.map_or(command_name, |c| { | ||
Box::leak(c.custom_name.clone().into_boxed_str()) | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_hide() { | ||
let commands = Commands::new("node=host,project,enroll"); | ||
assert!(!commands.hide("node")); | ||
assert!(!commands.hide("project")); | ||
assert!(!commands.hide("enroll")); | ||
assert!(commands.hide("command4")); | ||
|
||
let commands = Commands::new(""); | ||
assert!(!commands.hide("command1")); | ||
} | ||
|
||
#[test] | ||
fn test_commands() { | ||
let commands = Commands::new("node=host,project,enroll"); | ||
assert_eq!(commands.name("node"), "host"); | ||
assert_eq!(commands.name("project"), "project"); | ||
assert_eq!(commands.name("enroll"), "enroll"); | ||
assert_eq!(commands.name("command4"), "command4"); | ||
|
||
let commands = Commands::new(""); | ||
assert_eq!(commands.name("command1"), "command1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod colors; | ||
pub mod command; | ||
pub mod output; | ||
pub mod terminal; |
51 changes: 51 additions & 0 deletions
51
implementations/rust/ockam/ockam_api/src/ui/output/branding.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::command::Commands; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct OutputBranding { | ||
pub brand_name: String, | ||
pub bin_name: String, | ||
pub commands: Commands, | ||
} | ||
|
||
impl OutputBranding { | ||
pub fn new(brand_name: String, bin_name: String, commands: Commands) -> Self { | ||
Self { | ||
brand_name, | ||
bin_name, | ||
commands, | ||
} | ||
} | ||
|
||
pub fn replace(&self, text: &str) -> String { | ||
// brand name | ||
let mut text = if self.brand_name != "Ockam" { | ||
text.replace("Ockam", &self.brand_name) | ||
} else { | ||
text.to_string() | ||
}; | ||
// command names | ||
for command in &self.commands.commands { | ||
text = text.replace( | ||
&format!("ockam {}", command.name), | ||
&format!("ockam {}", command.custom_name), | ||
); | ||
} | ||
// bin name | ||
text = if self.bin_name != "ockam" { | ||
text.replace("ockam", &self.bin_name) | ||
} else { | ||
text | ||
}; | ||
text | ||
} | ||
} | ||
|
||
impl Default for OutputBranding { | ||
fn default() -> Self { | ||
Self { | ||
brand_name: "Ockam".to_string(), | ||
bin_name: "ockam".to_string(), | ||
commands: Commands::new(""), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ acme: | |
support_email: [email protected] | ||
# the following fields are optional | ||
brand_name: Acme # if not set, will default to Bin | ||
home_dir: $HOME/.acme # if not set, will default to $HOME/.bin | ||
home_dir: $HOME/.acme # if not set, will default to $HOME/.acme | ||
orchestrator_identifier: I25242aa3d4a7b5aa986fb2bec15b3780aad0530660e5e5a46c7f9ce429e9ec99 # if not set, will default to the OCKAM_CONTROLLER_IDENTIFIER env var | ||
orchestrator_address: /dnsaddr/acme.io/tcp/6252/service/api # if not set, will default to the OCKAM_CONTROLLER_ADDRESS env var | ||
# build_args: # if not set, will default to empty string | ||
build_args: # if not set, will default to empty string | ||
# - --no-default-features | ||
# - --features | ||
# - 'no_std alloc software_vault rust-crypto' | ||
|
Oops, something went wrong.