From 14d4f0d179be1c8a20e0f763489e3484304c3329 Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Sun, 25 Aug 2024 17:27:54 +0200 Subject: [PATCH] Support profile as argument You can now specify a profile as an argument to the `lock` and `source` commands. This can be more readable than using environment variables or the `--profile` option. Example: ``` sheldon source my-profile ``` --- completions/sheldon.bash | 4 ++-- completions/sheldon.zsh | 2 ++ src/cli/mod.rs | 11 +++++++++-- src/cli/raw.rs | 8 ++++++++ src/cli/testdata/raw_opt_lock_help.golden | 5 ++++- src/cli/testdata/raw_opt_source_help.golden | 5 ++++- src/cli/tests.rs | 7 +++++-- 7 files changed, 34 insertions(+), 8 deletions(-) diff --git a/completions/sheldon.bash b/completions/sheldon.bash index ad1c10e2..d5543700 100644 --- a/completions/sheldon.bash +++ b/completions/sheldon.bash @@ -197,7 +197,7 @@ _sheldon() { return 0 ;; sheldon__lock) - opts="-h --update --reinstall --help" + opts="-h --update --reinstall --help [PROFILE]" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -225,7 +225,7 @@ _sheldon() { return 0 ;; sheldon__source) - opts="-h --relock --update --reinstall --help" + opts="-h --relock --update --reinstall --help [PROFILE]" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/sheldon.zsh b/completions/sheldon.zsh index 0817bac3..86cd310c 100644 --- a/completions/sheldon.zsh +++ b/completions/sheldon.zsh @@ -85,6 +85,7 @@ _arguments "${_arguments_options[@]}" : \ '(--update)--reinstall[Reinstall all plugin sources]' \ '-h[Print help]' \ '--help[Print help]' \ +'::profile -- The profile used for conditional plugins:' \ && ret=0 ;; (source) @@ -94,6 +95,7 @@ _arguments "${_arguments_options[@]}" : \ '(--update)--reinstall[Reinstall all plugin sources (implies --relock)]' \ '-h[Print help]' \ '--help[Print help]' \ +'::profile -- The profile used for conditional plugins:' \ && ret=0 ;; (completions) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 0f7d2840..f837d411 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -67,7 +67,7 @@ impl Opt { data_dir, config_dir, config_file, - profile, + mut profile, command, } = raw_opt; @@ -84,7 +84,12 @@ impl Opt { } RawCommand::Edit => Command::Edit, RawCommand::Remove { name } => Command::Remove { name }, - RawCommand::Lock { update, reinstall } => { + RawCommand::Lock { + update, + reinstall, + profile: profile_arg, + } => { + profile = profile_arg.or(profile); lock_mode = LockMode::from_lock_flags(update, reinstall); Command::Lock } @@ -92,7 +97,9 @@ impl Opt { relock, update, reinstall, + profile: p, } => { + profile = p.or(profile); lock_mode = LockMode::from_source_flags(relock, update, reinstall); Command::Source } diff --git a/src/cli/raw.rs b/src/cli/raw.rs index d6fa0a9c..8024ab86 100644 --- a/src/cli/raw.rs +++ b/src/cli/raw.rs @@ -101,6 +101,10 @@ pub enum RawCommand { /// Reinstall all plugin sources. #[clap(long, conflicts_with = "update")] reinstall: bool, + + /// The profile used for conditional plugins. + #[clap(value_name = "PROFILE", env = "SHELDON_PROFILE")] + profile: Option, }, /// Generate and print out the script. @@ -116,6 +120,10 @@ pub enum RawCommand { /// Reinstall all plugin sources (implies --relock). #[clap(long, conflicts_with = "update")] reinstall: bool, + + /// The profile used for conditional plugins. + #[clap(value_name = "PROFILE", env = "SHELDON_PROFILE")] + profile: Option, }, /// Generate completions for the given shell. diff --git a/src/cli/testdata/raw_opt_lock_help.golden b/src/cli/testdata/raw_opt_lock_help.golden index 096c60a1..fe568e3f 100644 --- a/src/cli/testdata/raw_opt_lock_help.golden +++ b/src/cli/testdata/raw_opt_lock_help.golden @@ -1,6 +1,9 @@ Install the plugins sources and generate the lock file -Usage: sheldon lock [OPTIONS] +Usage: sheldon lock [OPTIONS] [PROFILE] + +Arguments: + [PROFILE] The profile used for conditional plugins [env: SHELDON_PROFILE=] Options: --update Update all plugin sources diff --git a/src/cli/testdata/raw_opt_source_help.golden b/src/cli/testdata/raw_opt_source_help.golden index 589e06cf..b87c20b8 100644 --- a/src/cli/testdata/raw_opt_source_help.golden +++ b/src/cli/testdata/raw_opt_source_help.golden @@ -1,6 +1,9 @@ Generate and print out the script -Usage: sheldon source [OPTIONS] +Usage: sheldon source [OPTIONS] [PROFILE] + +Arguments: + [PROFILE] The profile used for conditional plugins [env: SHELDON_PROFILE=] Options: --relock Regenerate the lock file diff --git a/src/cli/tests.rs b/src/cli/tests.rs index 89f212c6..681085d3 100644 --- a/src/cli/tests.rs +++ b/src/cli/tests.rs @@ -73,7 +73,8 @@ fn raw_opt_no_options() { profile: None, command: RawCommand::Lock { update: false, - reinstall: false + reinstall: false, + profile: None, }, } ); @@ -97,6 +98,7 @@ fn raw_opt_options() { "--profile", "profile", "lock", + "profile2", ]), RawOpt { quiet: true, @@ -109,7 +111,8 @@ fn raw_opt_options() { profile: Some("profile".into()), command: RawCommand::Lock { update: false, - reinstall: false + reinstall: false, + profile: Some("profile2".into()), }, } );