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.
This commit is contained in:
parent
74cc48fc50
commit
2c12e8ff83
6 changed files with 65 additions and 99 deletions
|
|
@ -23,6 +23,8 @@ _zellij() {
|
||||||
'--data-dir=[Change where zellij looks for layouts and plugins]' \
|
'--data-dir=[Change where zellij looks for layouts and plugins]' \
|
||||||
'-l+[Path to a layout yaml file]' \
|
'-l+[Path to a layout yaml file]' \
|
||||||
'--layout=[Path to a layout yaml file]' \
|
'--layout=[Path to a layout yaml file]' \
|
||||||
|
'-c+[Change where zellij looks for the configuration]' \
|
||||||
|
'--config=[Change where zellij looks for the configuration]' \
|
||||||
'-m[Send "move focused pane" to active zellij session]' \
|
'-m[Send "move focused pane" to active zellij session]' \
|
||||||
'--move-focus[Send "move focused pane" to active zellij session]' \
|
'--move-focus[Send "move focused pane" to active zellij session]' \
|
||||||
'-d[]' \
|
'-d[]' \
|
||||||
|
|
@ -40,34 +42,13 @@ _zellij() {
|
||||||
(( CURRENT += 1 ))
|
(( CURRENT += 1 ))
|
||||||
curcontext="${curcontext%:*:*}:zellij-command-$line[1]:"
|
curcontext="${curcontext%:*:*}:zellij-command-$line[1]:"
|
||||||
case $line[1] in
|
case $line[1] in
|
||||||
(c)
|
(option)
|
||||||
_arguments "${_arguments_options[@]}" \
|
_arguments "${_arguments_options[@]}" \
|
||||||
'--clean[Disables loading of configuration file at default location]' \
|
'--clean[Disables loading of configuration file at default location]' \
|
||||||
'-h[Prints help information]' \
|
'-h[Prints help information]' \
|
||||||
'--help[Prints help information]' \
|
'--help[Prints help information]' \
|
||||||
'-V[Prints version information]' \
|
'-V[Prints version information]' \
|
||||||
'--version[Prints version information]' \
|
'--version[Prints version information]' \
|
||||||
'::path:_files' \
|
|
||||||
&& ret=0
|
|
||||||
;;
|
|
||||||
(c)
|
|
||||||
_arguments "${_arguments_options[@]}" \
|
|
||||||
'--clean[Disables loading of configuration file at default location]' \
|
|
||||||
'-h[Prints help information]' \
|
|
||||||
'--help[Prints help information]' \
|
|
||||||
'-V[Prints version information]' \
|
|
||||||
'--version[Prints version information]' \
|
|
||||||
'::path:_files' \
|
|
||||||
&& ret=0
|
|
||||||
;;
|
|
||||||
(config)
|
|
||||||
_arguments "${_arguments_options[@]}" \
|
|
||||||
'--clean[Disables loading of configuration file at default location]' \
|
|
||||||
'-h[Prints help information]' \
|
|
||||||
'--help[Prints help information]' \
|
|
||||||
'-V[Prints version information]' \
|
|
||||||
'--version[Prints version information]' \
|
|
||||||
'::path:_files' \
|
|
||||||
&& ret=0
|
&& ret=0
|
||||||
;;
|
;;
|
||||||
(help)
|
(help)
|
||||||
|
|
@ -86,32 +67,11 @@ esac
|
||||||
(( $+functions[_zellij_commands] )) ||
|
(( $+functions[_zellij_commands] )) ||
|
||||||
_zellij_commands() {
|
_zellij_commands() {
|
||||||
local commands; commands=(
|
local commands; commands=(
|
||||||
"config:Path to the configuration yaml file" \
|
"option:Change the behaviour of zellij" \
|
||||||
"help:Prints this message or the help of the given subcommand(s)" \
|
"help:Prints this message or the help of the given subcommand(s)" \
|
||||||
)
|
)
|
||||||
_describe -t commands 'zellij commands' commands "$@"
|
_describe -t commands 'zellij commands' commands "$@"
|
||||||
}
|
}
|
||||||
(( $+functions[_c_commands] )) ||
|
|
||||||
_c_commands() {
|
|
||||||
local commands; commands=(
|
|
||||||
|
|
||||||
)
|
|
||||||
_describe -t commands 'c commands' commands "$@"
|
|
||||||
}
|
|
||||||
(( $+functions[_zellij__c_commands] )) ||
|
|
||||||
_zellij__c_commands() {
|
|
||||||
local commands; commands=(
|
|
||||||
|
|
||||||
)
|
|
||||||
_describe -t commands 'zellij c commands' commands "$@"
|
|
||||||
}
|
|
||||||
(( $+functions[_zellij__config_commands] )) ||
|
|
||||||
_zellij__config_commands() {
|
|
||||||
local commands; commands=(
|
|
||||||
|
|
||||||
)
|
|
||||||
_describe -t commands 'zellij config commands' commands "$@"
|
|
||||||
}
|
|
||||||
(( $+functions[_zellij__help_commands] )) ||
|
(( $+functions[_zellij__help_commands] )) ||
|
||||||
_zellij__help_commands() {
|
_zellij__help_commands() {
|
||||||
local commands; commands=(
|
local commands; commands=(
|
||||||
|
|
@ -119,5 +79,12 @@ _zellij__help_commands() {
|
||||||
)
|
)
|
||||||
_describe -t commands 'zellij help commands' commands "$@"
|
_describe -t commands 'zellij help commands' commands "$@"
|
||||||
}
|
}
|
||||||
|
(( $+functions[_zellij__option_commands] )) ||
|
||||||
|
_zellij__option_commands() {
|
||||||
|
local commands; commands=(
|
||||||
|
|
||||||
|
)
|
||||||
|
_describe -t commands 'zellij option commands' commands "$@"
|
||||||
|
}
|
||||||
|
|
||||||
_zellij "$@"
|
_zellij "$@"
|
||||||
|
|
@ -13,15 +13,12 @@ _zellij() {
|
||||||
cmd="zellij"
|
cmd="zellij"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
c)
|
|
||||||
cmd+="__c"
|
|
||||||
;;
|
|
||||||
config)
|
|
||||||
cmd+="__config"
|
|
||||||
;;
|
|
||||||
help)
|
help)
|
||||||
cmd+="__help"
|
cmd+="__help"
|
||||||
;;
|
;;
|
||||||
|
option)
|
||||||
|
cmd+="__option"
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
@ -29,7 +26,7 @@ _zellij() {
|
||||||
|
|
||||||
case "${cmd}" in
|
case "${cmd}" in
|
||||||
zellij)
|
zellij)
|
||||||
opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --data-dir --layout config help c c"
|
opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --data-dir --layout --config option help"
|
||||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||||
return 0
|
return 0
|
||||||
|
|
@ -68,6 +65,14 @@ _zellij() {
|
||||||
COMPREPLY=($(compgen -f "${cur}"))
|
COMPREPLY=($(compgen -f "${cur}"))
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
--config)
|
||||||
|
COMPREPLY=($(compgen -f "${cur}"))
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
-c)
|
||||||
|
COMPREPLY=($(compgen -f "${cur}"))
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
;;
|
;;
|
||||||
|
|
@ -76,36 +81,6 @@ _zellij() {
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
zellij__c)
|
|
||||||
opts=" -h -V --clean --help --version <path> "
|
|
||||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
case "${prev}" in
|
|
||||||
|
|
||||||
*)
|
|
||||||
COMPREPLY=()
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
||||||
return 0
|
|
||||||
;;
|
|
||||||
zellij__config)
|
|
||||||
opts=" -h -V --clean --help --version <path> "
|
|
||||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
case "${prev}" in
|
|
||||||
|
|
||||||
*)
|
|
||||||
COMPREPLY=()
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
||||||
return 0
|
|
||||||
;;
|
|
||||||
zellij__help)
|
zellij__help)
|
||||||
opts=" -h -V --help --version "
|
opts=" -h -V --help --version "
|
||||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||||
|
|
@ -121,6 +96,21 @@ _zellij() {
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
zellij__option)
|
||||||
|
opts=" -h -V --clean --help --version "
|
||||||
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||||
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
case "${prev}" in
|
||||||
|
|
||||||
|
*)
|
||||||
|
COMPREPLY=()
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,15 @@ complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open f
|
||||||
complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones'
|
complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -l data-dir -d 'Change where zellij looks for layouts and plugins'
|
complete -c zellij -n "__fish_use_subcommand" -l data-dir -d 'Change where zellij looks for layouts and plugins'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file'
|
complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file'
|
||||||
|
complete -c zellij -n "__fish_use_subcommand" -s c -l config -d 'Change where zellij looks for the configuration'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session'
|
complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -s d -l debug
|
complete -c zellij -n "__fish_use_subcommand" -s d -l debug
|
||||||
complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
|
complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -s V -l version -d 'Prints version information'
|
complete -c zellij -n "__fish_use_subcommand" -s V -l version -d 'Prints version information'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -f -a "config" -d 'Path to the configuration yaml file'
|
complete -c zellij -n "__fish_use_subcommand" -f -a "option" -d 'Change the behaviour of zellij'
|
||||||
complete -c zellij -n "__fish_use_subcommand" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
|
complete -c zellij -n "__fish_use_subcommand" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
|
||||||
complete -c zellij -n "__fish_seen_subcommand_from config" -l clean -d 'Disables loading of configuration file at default location'
|
complete -c zellij -n "__fish_seen_subcommand_from option" -l clean -d 'Disables loading of configuration file at default location'
|
||||||
complete -c zellij -n "__fish_seen_subcommand_from config" -s h -l help -d 'Prints help information'
|
complete -c zellij -n "__fish_seen_subcommand_from option" -s h -l help -d 'Prints help information'
|
||||||
complete -c zellij -n "__fish_seen_subcommand_from config" -s V -l version -d 'Prints version information'
|
complete -c zellij -n "__fish_seen_subcommand_from option" -s V -l version -d 'Prints version information'
|
||||||
complete -c zellij -n "__fish_seen_subcommand_from help" -s h -l help -d 'Prints help information'
|
complete -c zellij -n "__fish_seen_subcommand_from help" -s h -l help -d 'Prints help information'
|
||||||
complete -c zellij -n "__fish_seen_subcommand_from help" -s V -l version -d 'Prints version information'
|
complete -c zellij -n "__fish_seen_subcommand_from help" -s V -l version -d 'Prints version information'
|
||||||
|
|
|
||||||
14
src/cli.rs
14
src/cli.rs
|
|
@ -1,6 +1,9 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
// TODO add to consts.rs
|
||||||
|
const ZELLIJ_CONFIG_ENV: &str = "ZELLIJ_CONFIG";
|
||||||
|
|
||||||
#[derive(StructOpt, Default, Debug)]
|
#[derive(StructOpt, Default, Debug)]
|
||||||
#[structopt(name = "zellij")]
|
#[structopt(name = "zellij")]
|
||||||
pub struct CliArgs {
|
pub struct CliArgs {
|
||||||
|
|
@ -28,8 +31,12 @@ pub struct CliArgs {
|
||||||
#[structopt(short, long)]
|
#[structopt(short, long)]
|
||||||
pub layout: Option<PathBuf>,
|
pub layout: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Change where zellij looks for the configuration
|
||||||
|
#[structopt(short, long, env=ZELLIJ_CONFIG_ENV)]
|
||||||
|
pub config: Option<PathBuf>,
|
||||||
|
|
||||||
#[structopt(subcommand)]
|
#[structopt(subcommand)]
|
||||||
pub config: Option<ConfigCli>,
|
pub option: Option<ConfigCli>,
|
||||||
|
|
||||||
#[structopt(short, long)]
|
#[structopt(short, long)]
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
|
|
@ -37,10 +44,9 @@ pub struct CliArgs {
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
pub enum ConfigCli {
|
pub enum ConfigCli {
|
||||||
/// Path to the configuration yaml file
|
/// Change the behaviour of zellij
|
||||||
#[structopt(alias = "c")]
|
#[structopt(name = "option")]
|
||||||
Config {
|
Config {
|
||||||
path: Option<PathBuf>,
|
|
||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
/// Disables loading of configuration file at default location
|
/// Disables loading of configuration file at default location
|
||||||
clean: bool,
|
clean: bool,
|
||||||
|
|
|
||||||
|
|
@ -79,19 +79,23 @@ impl Config {
|
||||||
|
|
||||||
/// Entry point of the configuration
|
/// Entry point of the configuration
|
||||||
#[cfg(not(test))]
|
#[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 {
|
match cli_config {
|
||||||
Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()),
|
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()?),
|
Some(_) | None => Ok(Config::from_default_path()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// In order not to mess up tests from changing configurations
|
/// In order not to mess up tests from changing configurations
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn from_cli_config(_: Option<ConfigCli>) -> ConfigResult {
|
pub fn from_cli_config(_: Option<PathBuf>, _: Option<ConfigCli>) -> ConfigResult {
|
||||||
Ok(Config::default())
|
Ok(Config::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -137,19 +141,17 @@ mod config_test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn clean_option_equals_default_config() {
|
fn clean_option_equals_default_config() {
|
||||||
let no_file = PathBuf::from(r"../fixtures/config/config.yamlll");
|
|
||||||
let cli_config = ConfigCli::Config {
|
let cli_config = ConfigCli::Config {
|
||||||
path: Some(no_file),
|
|
||||||
clean: true,
|
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();
|
let default = Config::default();
|
||||||
assert_eq!(config, default);
|
assert_eq!(config, default);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_config_option_file_equals_default_config() {
|
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();
|
let default = Config::default();
|
||||||
assert_eq!(config, default);
|
assert_eq!(config, default);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
|
||||||
.write(take_snapshot.as_bytes())
|
.write(take_snapshot.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let config = Config::from_cli_config(opts.config)
|
let config = Config::from_cli_config(opts.config, opts.option)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("There was an error in the config file:\n{}", e);
|
eprintln!("There was an error in the config file:\n{}", e);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue