summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Dudley2024-08-09 18:57:06 -0600
committerRory Dudley2024-08-09 19:15:24 -0600
commit3f23ad58d2d0d1bd506a5d52bdf2d24c803f2105 (patch)
tree3528e3c928f75ba1d3b07cd853c20c3fa0521ad8
parent9d32dc6a186f38380befb58a144f4d93cfca30eb (diff)
downloaddwarvish-3f23ad58d2d0d1bd506a5d52bdf2d24c803f2105.tar.gz
Better handling for run command files
Rewrites the compose::env() function to be more modular, concerning the run command files to read in. Also, the rrr() function now takes a vector of files to read in. If a file is missing, even the global rc file, it is skipped, instead of an error being thrown, and the shell exiting. Signed-off-by: Rory Dudley <rory@netc.lu>
-rw-r--r--src/compose.rs105
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;
+ }
}
}
}