fix(setup): switch setup order (#765)

- commands that don't use the config file now wont throw an error on
  malformed config files
This commit is contained in:
a-kenji 2021-10-05 13:43:05 +02:00 committed by GitHub
parent f616af362c
commit 0ca28486a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -159,6 +159,17 @@ impl Setup {
_ => false,
};
// setup functions that don't require deserialisation of the config
if let Some(Command::Setup(ref setup)) = &opts.command {
setup.from_cli().map_or_else(
|e| {
eprintln!("{:?}", e);
process::exit(1);
},
|_| {},
);
};
let config = if !clean {
match Config::try_from(opts) {
Ok(config) => config,
@ -190,20 +201,22 @@ impl Setup {
};
if let Some(Command::Setup(ref setup)) = &opts.command {
setup.from_cli(opts, &config_options).map_or_else(
|e| {
eprintln!("{:?}", e);
process::exit(1);
},
|_| {},
);
setup
.from_cli_with_options(opts, &config_options)
.map_or_else(
|e| {
eprintln!("{:?}", e);
process::exit(1);
},
|_| {},
);
};
Ok((config, layout, config_options))
}
/// General setup helpers
pub fn from_cli(&self, opts: &CliArgs, config_options: &Options) -> std::io::Result<()> {
pub fn from_cli(&self) -> std::io::Result<()> {
if self.clean {
return Ok(());
}
@ -213,11 +226,6 @@ impl Setup {
std::process::exit(0);
}
if self.check {
Setup::check_defaults_config(opts, config_options)?;
std::process::exit(0);
}
if let Some(shell) = &self.generate_completion {
Self::generate_completion(shell.into());
std::process::exit(0);
@ -231,6 +239,19 @@ impl Setup {
Ok(())
}
/// Checks the merged configuration
pub fn from_cli_with_options(
&self,
opts: &CliArgs,
config_options: &Options,
) -> std::io::Result<()> {
if self.check {
Setup::check_defaults_config(opts, config_options)?;
std::process::exit(0);
}
Ok(())
}
pub fn check_defaults_config(opts: &CliArgs, config_options: &Options) -> std::io::Result<()> {
let data_dir = opts.data_dir.clone().unwrap_or_else(get_default_data_dir);
let config_dir = opts.config_dir.clone().or_else(find_default_config_dir);