summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Dudley2024-03-30 23:20:03 -0600
committerRory Dudley2024-03-30 23:20:03 -0600
commit2534e480bf4de2101f390beded67493565913238 (patch)
treeafbd0f998114e22f4f65868ef895bf4d31b9f212
parent90b4e0226d49f4f61300f91b6a9b1e7978bd2f9b (diff)
downloaddwarvish-2534e480bf4de2101f390beded67493565913238.tar.gz
Replace env!("HOME") with env::var("HOME")
Replaced all (non-test) instances of env!("HOME") with env::var("HOME"). The env! macro should only be used in instances where the environment variable should be resolved during compile time.
-rw-r--r--src/compose.rs14
-rw-r--r--src/main.rs2
-rw-r--r--src/poem/anthology/cd.rs9
3 files changed, 20 insertions, 5 deletions
diff --git a/src/compose.rs b/src/compose.rs
index 3d2a2db..c90f470 100644
--- a/src/compose.rs
+++ b/src/compose.rs
@@ -1,6 +1,7 @@
use crate::poem::{read::Readable, recite::Reciteable, Poem};
pub mod environment;
pub use environment::Environment;
+use std::env;
use std::fs;
use std::path::PathBuf;
@@ -18,8 +19,17 @@ pub fn env() -> Environment {
};
// User defined rc file in ~
- let mut local_rc = PathBuf::from(env!("HOME"));
- local_rc.push(".dwvshrc");
+ 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)
+ }
+ };
// Read, read, and recite
rrr(global_rc, &mut env);
diff --git a/src/main.rs b/src/main.rs
index 898d19e..c17f959 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -80,8 +80,6 @@ fn repl(prompt: &str, at_prompt: &mut Arc<Mutex<bool>>, env: &mut Environment) {
/// Shell setup and entry
fn main() {
// Compose the environment for dwvsh
- // TODO: All instances of `env!("HOME")` need to be changed to use env::var
- // TODO: Will probably need to set $HOME in dwv{profile,login} via passwd
let mut env = compose::env();
// Set the prompt
diff --git a/src/poem/anthology/cd.rs b/src/poem/anthology/cd.rs
index 3b48f60..d3a1998 100644
--- a/src/poem/anthology/cd.rs
+++ b/src/poem/anthology/cd.rs
@@ -1,9 +1,16 @@
use crate::poem::Verse;
+use std::env;
pub fn incant(verse: &Verse) -> i32 {
let path = match verse.clause() {
Some(path) => path[0].to_string(),
- None => env!("HOME").to_string(),
+ None => match env::var("HOME") {
+ Ok(val) => val,
+ Err(_) => {
+ eprintln!("cd: unknown home, staying in pwd");
+ return 1;
+ }
+ },
};
match std::env::set_current_dir(&path) {