Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions cli/core/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ impl OutputProcessor {
Some(OutputFormat::Wide) => OutputFor::Human,
_ => OutputFor::Machine,
};
let mut hints: Vec<String> = args.config().hints.clone();

if let (Some(resource_key), Some(action)) = (&resource_key, &action) {
args.config()
.command_hints
.get(resource_key.as_ref())
.and_then(|cmd_hints| {
cmd_hints.get(action.as_ref()).map(|val| {
hints.extend(val.clone());
})
});
let mut hints: Vec<String> = Vec::new();

if args.config().enable_hints {
hints.extend(args.config().hints.clone());

if let (Some(resource_key), Some(action)) = (&resource_key, &action) {
args.config()
.command_hints
.get(resource_key.as_ref())
.and_then(|cmd_hints| {
cmd_hints.get(action.as_ref()).map(|val| {
hints.extend(val.clone());
})
});
}
}

Self {
Expand Down Expand Up @@ -914,4 +918,50 @@ mod tests {
op.hints
);
}

#[test]
fn test_output_processor_from_args_hints_disabled() {
let mut config_file = Builder::new().suffix(".yaml").tempfile().unwrap();

const CONFIG_DATA: &str = r#"
command_hints:
res:
cmd:
- cmd_hint1
hints:
- hint1
enable_hints: false
"#;

write!(config_file, "{CONFIG_DATA}").unwrap();

#[derive(Parser)]
struct Cli {
#[command(flatten)]
global_opts: GlobalOpts,
#[arg(long("cli-config"), value_parser = parse_config, default_value_t = Config::new().unwrap())]
config: Config,
}

impl CliArgs for Cli {
fn global_opts(&self) -> &GlobalOpts {
&self.global_opts
}

fn config(&self) -> &Config {
&self.config
}
}

let op = OutputProcessor::from_args(
&Cli::parse_from([
"osc",
"--cli-config",
&config_file.path().as_os_str().to_string_lossy(),
]),
Some("res"),
Some("cmd"),
);
assert_eq!(Some(Vec::<String>::new()), op.hints);
}
}
7 changes: 3 additions & 4 deletions openstack_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::Parser;

use openstack_cli_core::cli::{
CliArgs, CompletionCommand, ConnectionRequirements, ConnectionRequirementsProvider, GlobalOpts,
parse_config, styles,
styles,
};
use openstack_cli_core::config::Config;
use openstack_cli_core::error::OpenStackCliError;
Expand Down Expand Up @@ -95,9 +95,8 @@ pub struct Cli {

/// CLI configuration
///
/// This does not accept parameters at the moment and will always get config from default
/// location.
#[arg(hide = true, long("cli-config"), value_parser = parse_config, default_value_t = Config::new().unwrap_or_default())]
/// This does not accept parameters and always gets config from the default location.
#[arg(skip = Config::new().unwrap_or_default())]
pub config: Config,
}

Expand Down
Loading