-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.rs
More file actions
60 lines (48 loc) · 1.59 KB
/
Copy pathconfig.rs
File metadata and controls
60 lines (48 loc) · 1.59 KB
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
//! Default and loaded binge configuration.
use std::path::PathBuf;
use anyhow::{Result, anyhow};
use serde::Deserialize;
use xdg::BaseDirectories;
#[derive(Deserialize)]
struct Toml {
/// Installation path
install_path: PathBuf,
}
pub(crate) struct Config {
base_dir: BaseDirectories,
toml: Option<Toml>,
}
impl Config {
/// Load configuration or create a default one.
pub(crate) fn new() -> Result<Self> {
let base_dir = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME"));
let toml = base_dir
.find_config_file("binge.toml")
.map(std::fs::read_to_string)
.transpose()?
.map(|content| toml::from_str(&content))
.transpose()?;
Ok(Self { base_dir, toml })
}
/// Return path to [`crate::manifest::Manifest`] file.
pub(crate) fn manifest_path(&self) -> Result<PathBuf> {
Ok(self.base_dir.place_state_file("manifest.toml")?)
}
/// Return installation target directory. If not explicitly specified in the configuration,
/// check if `~/.local/bin` is in `$PATH` and return that.
pub(crate) fn install_path(&self) -> Result<PathBuf> {
if let Some(toml) = &self.toml {
return Ok(toml.install_path.clone());
}
// TODO: test
let var = std::env::var("PATH")?;
for path in std::env::split_paths(&var) {
if path.ends_with(".local/bin") {
return Ok(path);
}
}
Err(anyhow!(
"no suitable destination directory found, consider configuring one"
))
}
}