From 4b4a027c3cdbcf9e85a607d628c57100a1d460c0 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Thu, 16 May 2019 23:53:15 +0200 Subject: [PATCH] add basepath cmdline flag this allows a basepath to be set via commandline where ncspot will place configuration and cache files. fixes #65 --- Cargo.lock | 1 + Cargo.toml | 1 + src/config.rs | 12 +++++++++++- src/main.rs | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 55ed213..b4641ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1560,6 +1560,7 @@ dependencies = [ "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "librespot 0.1.0 (git+https://github.com/librespot-org/librespot.git?rev=14721f4)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c1631f4..e12aa9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ directories = "1.0" failure = "0.1.3" fern = "0.5" futures = "0.1" +lazy_static = "1.3.0" log = "0.4.0" #rspotify = "0.4.0" serde = "1.0" diff --git a/src/config.rs b/src/config.rs index b9db2fb..977316b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::fs; use std::path::{Path, PathBuf}; +use std::sync::RwLock; use directories::ProjectDirs; @@ -33,8 +34,17 @@ pub struct ConfigTheme { pub cmdline_bg: Option, } +lazy_static! { + pub static ref BASE_PATH: RwLock> = RwLock::new(None); +} + fn proj_dirs() -> ProjectDirs { - ProjectDirs::from("org", "affekt", "ncspot").expect("can't determine project paths") + match *BASE_PATH.read().expect("can't readlock BASE_PATH") { + Some(ref basepath) => ProjectDirs::from_path(basepath.clone()).expect("invalid basepath"), + None => { + ProjectDirs::from("org", "affekt", "ncspot").expect("can't determine project paths") + } + } } pub fn config_path(file: &str) -> PathBuf { diff --git a/src/main.rs b/src/main.rs index e9ff401..c78ea94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ extern crate clipboard; extern crate directories; extern crate failure; extern crate futures; +#[macro_use] +extern crate lazy_static; extern crate librespot; extern crate rspotify; extern crate tokio; @@ -30,7 +32,9 @@ extern crate fern; extern crate rand; use std::fs; +use std::path::PathBuf; use std::process; +use std::str::FromStr; use std::sync::Arc; use clap::{App, Arg}; @@ -120,12 +124,28 @@ fn main() { .help("Enable debug logging to the specified file") .takes_value(true), ) + .arg( + Arg::with_name("basepath") + .short("b") + .long("basepath") + .value_name("PATH") + .help("custom basepath to config/cache files") + .takes_value(true), + ) .get_matches(); if let Some(filename) = matches.value_of("debug") { setup_logging(filename).expect("can't setup logging"); } + if let Some(basepath) = matches.value_of("basepath") { + let path = PathBuf::from_str(basepath).expect("invalid path"); + if !path.exists() { + fs::create_dir(&path).expect("could not create basepath directory"); + } + *config::BASE_PATH.write().unwrap() = Some(path); + } + // 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: ::config::Config = {