Skip to content

Commit

Permalink
Merge pull request #3264 from jarhodes314/feat/cli-filtering
Browse files Browse the repository at this point in the history
feat: add filtering to `tedge config list`
  • Loading branch information
jarhodes314 authored Nov 29, 2024
2 parents 649e4ed + 7066d85 commit 9113b6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
10 changes: 9 additions & 1 deletion crates/core/tedge/src/cli/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub enum ConfigCmd {
/// Prints all keys and descriptions with example values
#[clap(long = "doc")]
is_doc: bool,

/// Prints only the keys that contain the provided filter string
filter: Option<String>,
},
}

Expand Down Expand Up @@ -90,10 +93,15 @@ impl BuildCommand for ConfigCmd {
config_location,
}
.into_boxed()),
ConfigCmd::List { is_all, is_doc } => Ok(ListConfigCommand {
ConfigCmd::List {
is_all,
is_doc,
filter,
} => Ok(ListConfigCommand {
is_all,
is_doc,
config: config_location.load()?,
filter,
}
.into_boxed()),
}
Expand Down
33 changes: 26 additions & 7 deletions crates/core/tedge/src/cli/config/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use yansi::Paint;
pub struct ListConfigCommand {
pub is_all: bool,
pub is_doc: bool,
pub filter: Option<String>,
pub config: TEdgeConfig,
}

Expand All @@ -20,18 +21,25 @@ impl Command for ListConfigCommand {

fn execute(&self) -> Result<(), MaybeFancy<anyhow::Error>> {
if self.is_doc {
print_config_doc(&self.config);
print_config_doc(self.filter.as_deref());
} else {
print_config_list(&self.config, self.is_all)?;
print_config_list(&self.config, self.is_all, self.filter.as_deref())?;
}

Ok(())
}
}

fn print_config_list(config: &TEdgeConfig, all: bool) -> Result<(), anyhow::Error> {
fn print_config_list(
config: &TEdgeConfig,
all: bool,
filter: Option<&str>,
) -> Result<(), anyhow::Error> {
let mut keys_without_values = Vec::new();
for config_key in config.readable_keys() {
if !key_matches_filter(&config_key.to_cow_str(), filter) {
continue;
}
match config.read_string(&config_key).ok() {
Some(value) => {
println!("{}={}", config_key, value);
Expand All @@ -50,14 +58,18 @@ fn print_config_list(config: &TEdgeConfig, all: bool) -> Result<(), anyhow::Erro
Ok(())
}

fn print_config_doc(config: &TEdgeConfig) {
let max_length = config
.readable_keys()
.map(|c| c.to_cow_str().len())
fn print_config_doc(filter: Option<&str>) {
let max_length = READABLE_KEYS
.iter()
.filter(|(key, _)| key_matches_filter(key, filter))
.map(|(key, _)| key.len())
.max()
.unwrap_or_default();

for (key, ty) in READABLE_KEYS.iter() {
if !key_matches_filter(key, filter) {
continue;
}
let docs = ty
.comment
.map(|c| {
Expand Down Expand Up @@ -116,3 +128,10 @@ fn print_config_doc(config: &TEdgeConfig) {
}
}
}

fn key_matches_filter(key: &str, filter: Option<&str>) -> bool {
match filter {
Some(filter) => key.contains(filter),
None => true,
}
}

0 comments on commit 9113b6f

Please sign in to comment.