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
use dirs;
use std::env;
use std::path::{Path, PathBuf};
const DEVLOG_REPO_ENV_VAR: &'static str = "DEVLOG_REPO";
const DEFAULT_HOME_DIR: &'static str = "devlogs";
const EDITOR_ENV_VAR: &'static str = "DEVLOG_EDITOR";
const DEFAULT_EDITOR: &'static str = "nano";
pub struct Config {
repo_dir: PathBuf,
editor_prog: String,
}
impl Config {
pub fn new(repo_dir: &Path, editor_prog: &str) -> Config {
Config {
repo_dir: repo_dir.to_path_buf(),
editor_prog: editor_prog.to_string(),
}
}
pub fn load() -> Config {
let repo_dir_str = env::var(DEVLOG_REPO_ENV_VAR)
.ok()
.unwrap_or_else(default_repo_dir);
let repo_dir = PathBuf::from(repo_dir_str);
let editor_prog = env::var(EDITOR_ENV_VAR).unwrap_or(DEFAULT_EDITOR.to_string());
Config {
repo_dir,
editor_prog,
}
}
pub fn repo_dir(&self) -> &Path {
self.repo_dir.as_path()
}
pub fn editor_prog(&self) -> &str {
&self.editor_prog
}
}
fn default_repo_dir() -> String {
let mut p = PathBuf::new();
p.push(dirs::home_dir().expect("Could not find home directory"));
p.push(DEFAULT_HOME_DIR);
p.to_string_lossy().to_string()
}