Skip to content

Commit 205fd23

Browse files
committed
.csmrc: If present but unreadable, don't continue
This indicates that the user likely intended to use a .csmrc config, but for some reason it could not be read (e.g. due to permissions). Bail out early in this case.
1 parent a20ad98 commit 205fd23

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/csmrc.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ impl Config {
2424
return Ok(Self::default());
2525
};
2626
let csmrc_path = home.join(".csmrc");
27-
let Ok(csmrc_data) = std::fs::read_to_string(csmrc_path) else {
28-
debug!("No .csmrc found, using defaults");
29-
return Ok(Config::default());
30-
};
31-
let config =
32-
serde_yaml_ng::from_str(&csmrc_data).map_err(|e| Error::new(ErrorKind::InvalidData, e));
33-
debug!("config: {:?}", config);
34-
config
27+
match std::fs::read_to_string(csmrc_path) {
28+
Err(e) if e.kind() == ErrorKind::NotFound => {
29+
debug!("No .csmrc found, using defaults");
30+
Ok(Config::default())
31+
}
32+
Err(e) => Err(e),
33+
Ok(csmrc_data) => {
34+
let config = serde_yaml_ng::from_str(&csmrc_data)
35+
.map_err(|e| Error::new(ErrorKind::InvalidData, e));
36+
debug!("config: {:?}", config);
37+
config
38+
}
39+
}
3540
}
3641
}

src/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
9393
Subcommand::Create(args) => {
9494
let Some(env_name) = determine_env_name(args) else {
9595
error!("No environment name could be determined. You can specify one with --name");
96-
return ExitCode::FAILURE
96+
return ExitCode::FAILURE;
9797
};
9898
println!("env: {}", env_name);
9999
}

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clap::{Parser, Subcommand};
77
use log::{LevelFilter, debug, error, warn};
88
use std::fs::File;
99
use std::io::Write;
10-
use std::path::PathBuf;
10+
use std::path::Path;
1111
use std::process::ExitCode;
1212

1313
#[derive(Parser, Debug)]
@@ -54,7 +54,11 @@ fn main() -> ExitCode {
5454

5555
if let Err(e) = create_mambarc(&home) {
5656
let attempted_path = home.join(".mambarc");
57-
warn!("Could not create {}, but continuing: {}", attempted_path.display(), e);
57+
warn!(
58+
"Could not create {}, but continuing: {}",
59+
attempted_path.display(),
60+
e
61+
);
5862
}
5963

6064
let config = match csmrc::Config::from_csmrc() {
@@ -72,7 +76,7 @@ fn main() -> ExitCode {
7276

7377
/// Create a ~/.mambarc (%UserProfile%\.mambarc on Windows) if it does not
7478
/// exist.
75-
fn create_mambarc(home: &PathBuf) -> std::io::Result<()> {
79+
fn create_mambarc(home: &Path) -> std::io::Result<()> {
7680
let mambarc = include_str!("../templates/mambarc");
7781
let mambarc_path = home.join(".mambarc");
7882
match File::create_new(&mambarc_path) {

src/util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ use std::env;
22
use std::path::PathBuf;
33

44
pub fn homedir() -> Option<PathBuf> {
5-
env::var("HOME").ok().or_else(|| env::var("USERPROFILE").ok()).map(PathBuf::from)
5+
env::var("HOME")
6+
.ok()
7+
.or_else(|| env::var("USERPROFILE").ok())
8+
.map(PathBuf::from)
69
}

0 commit comments

Comments
 (0)