Skip to content

Commit

Permalink
Don't complain on missing default config
Browse files Browse the repository at this point in the history
  • Loading branch information
KonishchevDmitry committed Nov 19, 2024
1 parent e485e6a commit 529607c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::install::Mode;
pub struct CliArgs {
pub log_level: Level,
pub config_path: PathBuf,
pub custom_config: bool,
pub action: Action,
}

Expand Down Expand Up @@ -76,8 +77,10 @@ pub fn parse_args() -> GenericResult<CliArgs> {
_ => return Err!("Invalid verbosity level"),
};

let config_path = matches.get_one("config").cloned().unwrap_or_else(||
PathBuf::from(shellexpand::tilde(DEFAULT_CONFIG_PATH).to_string()));
let (config_path, custom_config) = match matches.get_one("config").cloned() {
Some(path) => (path, true),
None => (PathBuf::from(shellexpand::tilde(DEFAULT_CONFIG_PATH).to_string()), false),
};

let (command, matches) = matches.subcommand().unwrap();

Expand All @@ -103,5 +106,5 @@ pub fn parse_args() -> GenericResult<CliArgs> {
_ => unreachable!(),
};

Ok(CliArgs {log_level, config_path, action})
Ok(CliArgs {log_level, config_path, custom_config, action})
}
16 changes: 14 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};

use serde::Deserialize;
Expand All @@ -18,6 +19,7 @@ pub struct Config {
#[serde(deserialize_with = "deserialize_path")]
pub path: PathBuf,

#[serde(default)]
#[validate(nested)]
pub tools: BTreeMap<String, Tool>,

Expand All @@ -26,8 +28,18 @@ pub struct Config {
}

impl Config {
pub fn load(path: &Path) -> GenericResult<Config> {
let config: Config = serde_yaml::from_reader(File::open(path)?)?;
pub fn load(path: &Path, custom: bool) -> GenericResult<Config> {
let reader: Box<dyn Read> = match File::open(path) {
Ok(file) => Box::new(file),
Err(err) => {
if custom || err.kind() != ErrorKind::NotFound {
return Err(err.into());
}
Box::new("{}".as_bytes())
},
};

let config: Config = serde_yaml::from_reader(reader)?;
config.validate()?;
Ok(config)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() {
process::exit(1);
}

if let Err(err) = run(&args.config_path, args.action) {
if let Err(err) = run(&args.config_path, args.custom_config, args.action) {
let message = err.to_string();

if message.contains('\n') || message.ends_with('.') {
Expand All @@ -48,8 +48,8 @@ fn main() {
}
}

fn run(config_path: &Path, action: Action) -> EmptyResult {
let config = Config::load(config_path).map_err(|e| format!(
fn run(config_path: &Path, custom_config: bool, action: Action) -> EmptyResult {
let config = Config::load(config_path, custom_config).map_err(|e| format!(
"Error while reading {:?} configuration file: {}", config_path, e))?;

match action {
Expand Down
5 changes: 4 additions & 1 deletion test
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ EOF
)

run() {
cargo run -- --config "$temp_dir/config.yaml" "$@"
cargo run --quiet -- --config "$temp_dir/config.yaml" "$@"
}

# Test work with missing default config
cargo run --quiet -- list

run list

for command in install "install --force" upgrade; do
Expand Down

0 comments on commit 529607c

Please sign in to comment.