summaryrefslogtreecommitdiffstats
path: root/src/compose.rs
blob: b12d1a441d58a566a3b22dbadc277cc4cef64d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;

/// Setup the global shell environment
///
/// Sets up the shell's environment via configuration files. In order:
/// * `/etc/dwvshrc`
/// * `~/.dwvshrc`
///
/// For debug builds, all files will instead be sourced from
/// `./dist/...` with the exception of `~/.dwvshrc`.
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)
        }
    };

    // Read, read, and recite
    rrr(global_rc, &mut env);
    rrr(local_rc, &mut env);

    // Return the new environment
    env
}

/// Read, read, and recite
///
/// Small, reusable function used to do the heavy lifting in regards to
/// sourcing configuration files. [Read][fs::read_to_string]s a file
/// 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") {
                eprintln!(
                    "dwvsh: unable to read global dwvshrc file: {}",
                    e.to_string().to_lowercase()
                );
            }
            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;
        }
    }
}