diff options
Diffstat (limited to 'src/compose.rs')
-rw-r--r-- | src/compose.rs | 105 |
1 files changed, 56 insertions, 49 deletions
diff --git a/src/compose.rs b/src/compose.rs index b12d1a4..befaf04 100644 --- a/src/compose.rs +++ b/src/compose.rs @@ -17,31 +17,39 @@ pub fn env() -> Environment { // Create a new Environment object, to store some extra shell info let mut env = Environment::new(); - // Use local repo path if running the debug target - let global_rc = if cfg!(debug_assertions) { - let mut base = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - base.push("dist/etc/dwvshrc"); - base - } else { - PathBuf::from("/etc/dwvshrc") - }; - - // User defined rc file in ~ - let local_rc = match env::var("HOME") { - Ok(val) => { - let mut rc = PathBuf::from(val); - rc.push(".dwvshrc"); - rc - } - Err(_) => { - eprintln!("dwvsh: unknown home, falling back to /etc"); - PathBuf::from(&global_rc) - } - }; + // Try to get paths for default run command file locations + let paths = vec![ + // -> ./dist/etc/dwvshrc + // -> /etc/dwvshrc + if cfg!(debug_assertions) { + let mut base = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + base.push("dist/etc/dwvshrc"); + Some(base) + } else { + Some(PathBuf::from("/etc/dwvshrc")) + }, + // -> ./dist/etc/linuxrc + // -> /etc/dwvshrc + if cfg!(debug_assertions) && std::env::consts::OS == "linux" { + let mut base = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + base.push("dist/etc/dwvshrc"); + Some(base) + } else { + None + }, + // -> $HOME/.dwvshrc + match env::var("HOME") { + Ok(home) => { + let mut base = PathBuf::from(home); + base.push(".dwvshrc"); + Some(base) + } + Err(_) => None, + }, + ]; // Read, read, and recite - rrr(global_rc, &mut env); - rrr(local_rc, &mut env); + rrr(paths, &mut env); // Return the new environment env @@ -54,37 +62,36 @@ pub fn env() -> Environment { /// from disk, then [read][crate::poem::read]s (parses) a [Poem], /// then [recite][crate::poem::recite]s that [Poem]. /// Configuration files are just shell scripts. -fn rrr(path: PathBuf, env: &mut Environment) { - let poetry = match fs::read_to_string(&path) { - Ok(poetry) => poetry, - Err(e) => { - if path.to_string_lossy().contains("etc/dwvshrc") { +fn rrr(paths: Vec<Option<PathBuf>>, env: &mut Environment) { + for path in paths { + let path = match path { + Some(path) => path, + None => continue, + }; + + let poetry = match fs::read_to_string(&path) { + Ok(poetry) => poetry, + Err(_) => continue, + }; + + let poem = match Poem::read(poetry, &mut Environment::new()) { + Ok(poem) => poem, + Err(e) => { eprintln!( - "dwvsh: unable to read global dwvshrc file: {}", + "dwvsh: error in {}: {}", + path.display(), e.to_string().to_lowercase() ); + continue; } - return; - } - }; + }; - let poem = match Poem::read(poetry, &mut Environment::new()) { - Ok(poem) => poem, - Err(e) => { - eprintln!( - "dwvsh: error in {}: {}", - path.display(), - e.to_string().to_lowercase() - ); - return; - } - }; - - match poem.recite(env) { - Ok(_) => {} - Err(e) => { - eprintln!("dwvsh: {}", e.to_string().to_lowercase()); - return; + match poem.recite(env) { + Ok(_) => {} + Err(e) => { + eprintln!("dwvsh: {}", e.to_string().to_lowercase()); + continue; + } } } } |