-
-
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
8cf0036
commit 371f8db
Showing
19 changed files
with
286 additions
and
186 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,92 @@ | ||
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: &'static str, | ||
pub(crate) custom_name: &'static str, | ||
} | ||
|
||
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: &'static 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, custom_name }) | ||
}) | ||
.collect(); | ||
Self { commands } | ||
} | ||
|
||
pub fn hide(&self, command_name: &'static str) -> bool { | ||
// No restrictions | ||
if self.commands.is_empty() { | ||
return false; | ||
} | ||
// Check if the command is in the list of hidden commands | ||
!self.commands.iter().any(|c| c.name == command_name) | ||
} | ||
|
||
pub fn name(&self, command_name: &'static str) -> &'static str { | ||
// No restrictions | ||
if self.commands.is_empty() { | ||
return command_name; | ||
} | ||
// Check the custom name in the list of renamed commands | ||
self.commands | ||
.iter() | ||
.find(|c| c.name == command_name) | ||
.map_or(command_name, |c| c.custom_name) | ||
} | ||
} | ||
|
||
#[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
Oops, something went wrong.