Add commandline flag for custom config filename

Related to #565
This commit is contained in:
Henrik Friedrichsen
2021-07-22 17:17:57 +02:00
parent 1ed972190e
commit 2a82826e7a
2 changed files with 19 additions and 6 deletions

View File

@@ -98,13 +98,14 @@ lazy_static! {
}
pub struct Config {
filename: String,
values: RwLock<ConfigValues>,
state: RwLock<UserState>,
}
impl Config {
pub fn new() -> Self {
let values = load().unwrap_or_else(|e| {
pub fn new(filename: &str) -> Self {
let values = load(filename).unwrap_or_else(|e| {
eprintln!("could not load config: {}", e);
process::exit(1);
});
@@ -124,6 +125,7 @@ impl Config {
}
Self {
filename: filename.to_string(),
values: RwLock::new(values),
state: RwLock::new(userstate),
}
@@ -159,13 +161,13 @@ impl Config {
}
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
}
}
fn load() -> Result<ConfigValues, String> {
let path = config_path("config.toml");
fn load(filename: &str) -> Result<ConfigValues, String> {
let path = config_path(filename);
TOML.load_or_generate_default(path, || Ok(ConfigValues::default()), false)
}

View File

@@ -122,6 +122,15 @@ async fn main() {
.help("custom basepath to config/cache files")
.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();
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
// 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(
Some(config::cache_path("librespot")),