@@ -98,13 +98,14 @@ lazy_static! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
filename: String,
|
||||||
values: RwLock<ConfigValues>,
|
values: RwLock<ConfigValues>,
|
||||||
state: RwLock<UserState>,
|
state: RwLock<UserState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new() -> Self {
|
pub fn new(filename: &str) -> Self {
|
||||||
let values = load().unwrap_or_else(|e| {
|
let values = load(filename).unwrap_or_else(|e| {
|
||||||
eprintln!("could not load config: {}", e);
|
eprintln!("could not load config: {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
@@ -124,6 +125,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
filename: filename.to_string(),
|
||||||
values: RwLock::new(values),
|
values: RwLock::new(values),
|
||||||
state: RwLock::new(userstate),
|
state: RwLock::new(userstate),
|
||||||
}
|
}
|
||||||
@@ -159,13 +161,13 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload(&self) {
|
pub fn reload(&self) {
|
||||||
let cfg = load().expect("could not reload config");
|
let cfg = load(&self.filename).expect("could not reload config");
|
||||||
*self.values.write().expect("can't writelock config values") = cfg
|
*self.values.write().expect("can't writelock config values") = cfg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load() -> Result<ConfigValues, String> {
|
fn load(filename: &str) -> Result<ConfigValues, String> {
|
||||||
let path = config_path("config.toml");
|
let path = config_path(filename);
|
||||||
TOML.load_or_generate_default(path, || Ok(ConfigValues::default()), false)
|
TOML.load_or_generate_default(path, || Ok(ConfigValues::default()), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@@ -122,6 +122,15 @@ async fn main() {
|
|||||||
.help("custom basepath to config/cache files")
|
.help("custom basepath to config/cache files")
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("config")
|
||||||
|
.short("c")
|
||||||
|
.long("config")
|
||||||
|
.value_name("FILE")
|
||||||
|
.help("Filename of config file in basepath")
|
||||||
|
.takes_value(true)
|
||||||
|
.default_value("config.toml"),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
if let Some(filename) = matches.value_of("debug") {
|
if let Some(filename) = matches.value_of("debug") {
|
||||||
@@ -138,7 +147,9 @@ async fn main() {
|
|||||||
|
|
||||||
// Things here may cause the process to abort; we must do them before creating curses windows
|
// Things here may cause the process to abort; we must do them before creating curses windows
|
||||||
// otherwise the error message will not be seen by a user
|
// otherwise the error message will not be seen by a user
|
||||||
let cfg: Arc<crate::config::Config> = Arc::new(Config::new());
|
let cfg: Arc<crate::config::Config> = Arc::new(Config::new(
|
||||||
|
matches.value_of("config").unwrap_or("config.toml"),
|
||||||
|
));
|
||||||
|
|
||||||
let cache = Cache::new(
|
let cache = Cache::new(
|
||||||
Some(config::cache_path("librespot")),
|
Some(config::cache_path("librespot")),
|
||||||
|
|||||||
Reference in New Issue
Block a user