From a052e17e6f66d739449775cf5c5b14fc76693fea Mon Sep 17 00:00:00 2001 From: Vibhav Bobade Date: Wed, 27 Nov 2024 15:32:25 +0530 Subject: [PATCH] feat: use default config path when no arg for -f is provided --- changelog.d/1706.fixed.md | 1 + mirrord/cli/src/config.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 changelog.d/1706.fixed.md diff --git a/changelog.d/1706.fixed.md b/changelog.d/1706.fixed.md new file mode 100644 index 00000000000..ba3eb35ac2a --- /dev/null +++ b/changelog.d/1706.fixed.md @@ -0,0 +1 @@ +make `-f` flag accept optional argument, defaulting to "./mirrord.json" when no argument is provided \ No newline at end of file diff --git a/mirrord/cli/src/config.rs b/mirrord/cli/src/config.rs index 96548235ad4..806c4fcaf3d 100644 --- a/mirrord/cli/src/config.rs +++ b/mirrord/cli/src/config.rs @@ -204,7 +204,8 @@ pub(super) struct ExecParams { pub disable_version_check: bool, /// Load config from config file - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + /// When using -f flag without a value, defaults to "./mirrord.json" + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_missing_value = "./mirrord.json", num_args = 0..=1)] pub config_file: Option, /// Kube context to use from Kubeconfig @@ -426,7 +427,8 @@ pub(super) struct PortForwardArgs { pub disable_version_check: bool, /// Load config from config file - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + /// When using -f flag without a value, defaults to "./mirrord.json" + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_missing_value = "./mirrord.json", num_args = 0..=1)] pub config_file: Option, /// Kube context to use from Kubeconfig @@ -587,7 +589,7 @@ pub(super) enum OperatorCommand { /// Print operator status Status { /// Specify config file to use - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_value = "./mirrord.json")] config_file: Option, }, /// Operator session management commands. @@ -727,7 +729,7 @@ impl ListTargetArgs { #[derive(Args, Debug)] pub(super) struct ExtensionExecArgs { /// Specify config file to use - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_value = "./mirrord.json")] pub config_file: Option, /// Specify target #[arg(short = 't')] @@ -766,7 +768,7 @@ pub(super) enum DiagnoseCommand { /// Check network connectivity and provide RTT (latency) statistics. Latency { /// Specify config file to use - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_value = "./mirrord.json")] config_file: Option, }, } @@ -895,7 +897,8 @@ pub(super) struct VpnArgs { pub namespace: Option, /// Load config from config file - #[arg(short = 'f', long, value_hint = ValueHint::FilePath)] + /// If not specified, defaults to "./mirrord.json" + #[arg(short = 'f', long, value_hint = ValueHint::FilePath, default_value = "./mirrord.json")] pub config_file: Option, #[cfg(target_os = "macos")] @@ -999,4 +1002,19 @@ mod tests { assert_eq!(runtime_args, vec!["-it", "--rm", "debian"]); } + + #[test] + fn config_file_arg() { + // No -f flag used + let args = ExecParams::parse_from(["mirrord"]); + assert_eq!(args.config_file, None); + + // -f flag used without value + let args = ExecParams::parse_from(["mirrord", "-f"]); + assert_eq!(args.config_file, Some(PathBuf::from("./mirrord.json"))); + + // -f flag used with value + let args = ExecParams::parse_from(["mirrord", "-f", "custom.json"]); + assert_eq!(args.config_file, Some(PathBuf::from("custom.json"))); + } }