From be7a15caa03cf37b5c3e55f2eb2dfe316bfe917e Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Tue, 4 Jul 2023 17:51:21 -0500 Subject: [PATCH] Refactor generation commands to use --output for files and printing to stdout by default --- CHANGELOG.md | 3 +++ src/cli/commands/generate.rs | 13 ++++++++----- src/options.rs | 19 ++++++++++--------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e4092..2404624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Cli no longer uses `-c` as shorthand for specifying a config file +- `--file` option for generating completion has been renamed to `--output` +- CLI command to generate config files now defaults to printing to stdout with + `--output` providing the option to write to a file ## [0.20.0-alpha.10] diff --git a/src/cli/commands/generate.rs b/src/cli/commands/generate.rs index 11617a2..54704cd 100644 --- a/src/cli/commands/generate.rs +++ b/src/cli/commands/generate.rs @@ -14,15 +14,18 @@ pub fn run(cmd: GenerateSubcommand) -> CliResult { async fn async_run(cmd: GenerateSubcommand) -> CliResult { match cmd { - GenerateSubcommand::Config { file } => tokio::fs::write(file, Config::default_raw_str()) - .await - .context("Failed to write default config to {file:?}")?, + GenerateSubcommand::Config { output } => match output { + Some(path) => tokio::fs::write(path, Config::default_raw_str()) + .await + .context("Failed to write default config to {path:?}")?, + None => println!("{}", Config::default_raw_str()), + }, - GenerateSubcommand::Completion { file, shell } => { + GenerateSubcommand::Completion { output, shell } => { let name = "distant"; let mut cmd = Options::command(); - if let Some(path) = file { + if let Some(path) = output { clap_generate( shell, &mut cmd, diff --git a/src/options.rs b/src/options.rs index 6ffe38d..700bfc7 100644 --- a/src/options.rs +++ b/src/options.rs @@ -933,15 +933,16 @@ impl ClientFileSystemSubcommand { pub enum GenerateSubcommand { /// Generate configuration file with base settings Config { - /// Path to where the configuration file should be created - file: PathBuf, + /// Write output to a file instead of stdout + #[clap(short, long, value_name = "FILE")] + output: Option, }, // Generate completion info for CLI Completion { - /// If specified, will output to the file at the given path instead of stdout - #[clap(long)] - file: Option, + /// Write output to a file instead of stdout + #[clap(long, value_name = "FILE")] + output: Option, /// Specific shell to target for the generated output #[clap(value_enum, value_parser)] @@ -3340,7 +3341,7 @@ mod tests { log_level: None, }, command: DistantSubcommand::Generate(GenerateSubcommand::Completion { - file: None, + output: None, shell: ClapCompleteShell::Bash, }), }; @@ -3364,7 +3365,7 @@ mod tests { log_level: Some(LogLevel::Trace), }, command: DistantSubcommand::Generate(GenerateSubcommand::Completion { - file: None, + output: None, shell: ClapCompleteShell::Bash, }), } @@ -3380,7 +3381,7 @@ mod tests { log_level: Some(LogLevel::Info), }, command: DistantSubcommand::Generate(GenerateSubcommand::Completion { - file: None, + output: None, shell: ClapCompleteShell::Bash, }), }; @@ -3404,7 +3405,7 @@ mod tests { log_level: Some(LogLevel::Info), }, command: DistantSubcommand::Generate(GenerateSubcommand::Completion { - file: None, + output: None, shell: ClapCompleteShell::Bash, }), }