diff options
| author | a-kenji <aks.kenji@protonmail.com> | 2021-04-21 08:58:38 +0200 |
|---|---|---|
| committer | a-kenji <aks.kenji@protonmail.com> | 2021-04-21 09:03:46 +0200 |
| commit | 2c12e8ff8324b3ebaa10ac8162af09f5da295e1b (patch) | |
| tree | 674d5988c5ca0a34e6931798467fed61b45da3cf /src | |
| parent | fix(release): beta tag breaks cargo install (diff) | |
| download | sl0ppij-2c12e8ff8324b3ebaa10ac8162af09f5da295e1b.tar.gz sl0ppij-2c12e8ff8324b3ebaa10ac8162af09f5da295e1b.tar.xz sl0ppij-2c12e8ff8324b3ebaa10ac8162af09f5da295e1b.zip | |
Change Config To Flag (#290)
This changes the `config` subcommand to a config flag.
Add option subcommand for now, for the clearing of default path.
Add `ZELLIJ_CONFIG` environment variable.
The configuration locations are now as follows:
* `--config` flag
* `ZELLIJ_CONFIG` environment variable
* default config location
in that order.
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 14 | ||||
| -rw-r--r-- | src/common/input/config.rs | 20 | ||||
| -rw-r--r-- | src/common/mod.rs | 2 |
3 files changed, 22 insertions, 14 deletions
@@ -1,6 +1,9 @@ use std::path::PathBuf; use structopt::StructOpt; +// TODO add to consts.rs +const ZELLIJ_CONFIG_ENV: &str = "ZELLIJ_CONFIG"; + #[derive(StructOpt, Default, Debug)] #[structopt(name = "zellij")] pub struct CliArgs { @@ -28,8 +31,12 @@ pub struct CliArgs { #[structopt(short, long)] pub layout: Option<PathBuf>, + /// Change where zellij looks for the configuration + #[structopt(short, long, env=ZELLIJ_CONFIG_ENV)] + pub config: Option<PathBuf>, + #[structopt(subcommand)] - pub config: Option<ConfigCli>, + pub option: Option<ConfigCli>, #[structopt(short, long)] pub debug: bool, @@ -37,10 +44,9 @@ pub struct CliArgs { #[derive(Debug, StructOpt)] pub enum ConfigCli { - /// Path to the configuration yaml file - #[structopt(alias = "c")] + /// Change the behaviour of zellij + #[structopt(name = "option")] Config { - path: Option<PathBuf>, #[structopt(long)] /// Disables loading of configuration file at default location clean: bool, diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 1926b11c..17e1edf1 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -79,19 +79,23 @@ impl Config { /// Entry point of the configuration #[cfg(not(test))] - pub fn from_cli_config(cli_config: Option<ConfigCli>) -> ConfigResult { + pub fn from_cli_config( + location: Option<PathBuf>, + cli_config: Option<ConfigCli>, + ) -> ConfigResult { + if let Some(path) = location { + return Config::new(&path); + } + match cli_config { Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), - Some(ConfigCli::Config { path, .. }) if path.is_some() => { - Ok(Config::new(&path.unwrap())?) - } Some(_) | None => Ok(Config::from_default_path()?), } } /// In order not to mess up tests from changing configurations #[cfg(test)] - pub fn from_cli_config(_: Option<ConfigCli>) -> ConfigResult { + pub fn from_cli_config(_: Option<PathBuf>, _: Option<ConfigCli>) -> ConfigResult { Ok(Config::default()) } } @@ -137,19 +141,17 @@ mod config_test { #[test] fn clean_option_equals_default_config() { - let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); let cli_config = ConfigCli::Config { - path: Some(no_file), clean: true, }; - let config = Config::from_cli_config(Some(cli_config)).unwrap(); + let config = Config::from_cli_config(None, Some(cli_config)).unwrap(); let default = Config::default(); assert_eq!(config, default); } #[test] fn no_config_option_file_equals_default_config() { - let config = Config::from_cli_config(None).unwrap(); + let config = Config::from_cli_config(None, None).unwrap(); let default = Config::default(); assert_eq!(config, default); } diff --git a/src/common/mod.rs b/src/common/mod.rs index 860d12db..e2cd2220 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -128,7 +128,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) { .write(take_snapshot.as_bytes()) .unwrap(); - let config = Config::from_cli_config(opts.config) + let config = Config::from_cli_config(opts.config, opts.option) .map_err(|e| { eprintln!("There was an error in the config file:\n{}", e); std::process::exit(1); |
