Use proper config directory

This commit is contained in:
chmanie 2023-06-02 20:05:31 +02:00
parent 4fbeccdde1
commit 553f34a111
4 changed files with 49 additions and 4 deletions

28
Cargo.lock generated
View File

@ -377,6 +377,7 @@ dependencies = [
"anyhow",
"async-trait",
"crabidy-core",
"dirs",
"flume",
"futures",
"gstreamer",
@ -461,6 +462,15 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -471,6 +481,18 @@ dependencies = [
"dirs-sys-next",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.48.0",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
@ -1453,6 +1475,12 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "option-operations"
version = "0.5.0"

View File

@ -17,6 +17,7 @@ pub trait ProviderClient: std::fmt::Debug + Send + Sync {
#[derive(Clone, Debug, Hash)]
pub enum ProviderError {
Config(String),
UnknownUser,
CouldNotLogin,
FetchError,

View File

@ -20,3 +20,4 @@ tonic = "0.9.2"
async-trait = "0.1.68"
futures = "0.3.28"
tokio-stream = { version = "0.1.14", features = ["sync"] }
dirs = "5.0.1"

View File

@ -19,6 +19,7 @@ use tokio_stream::StreamExt;
use std::{
fs,
path::PathBuf,
pin::Pin,
sync::{Arc, Mutex},
};
@ -149,10 +150,24 @@ impl ProviderOrchestrator {
#[async_trait]
impl ProviderClient for ProviderOrchestrator {
async fn init(_s: &str) -> Result<Self, ProviderError> {
let raw_toml_settings = fs::read_to_string("/tmp/tidaldy.toml").unwrap_or("".to_owned());
let config_dir = dirs::config_dir()
.map(|d| d.join("crabidy"))
.unwrap_or(PathBuf::from("/tmp"));
let dir_exists = tokio::fs::try_exists(&config_dir)
.await
.map_err(|e| ProviderError::Config(e.to_string()))?;
if !dir_exists {
tokio::fs::create_dir(&config_dir)
.await
.map_err(|e| ProviderError::Config(e.to_string()))?;
}
let config_file = config_dir.join("tidaly.toml");
let raw_toml_settings = fs::read_to_string(&config_file).unwrap_or("".to_owned());
let tidal_client = Arc::new(tidaldy::Client::init(&raw_toml_settings).await.unwrap());
let new_toml_config = tidal_client.settings();
fs::write("/tmp/tidaldy.toml", new_toml_config).unwrap();
tokio::fs::write(&config_file, new_toml_config)
.await
.map_err(|e| ProviderError::Config(e.to_string()));
let (provider_tx, provider_rx) = flume::bounded(100);
Ok(Self {
provider_rx,
@ -291,12 +306,12 @@ impl Playback {
duration: self
.play
.duration()
.and_then(|t| Some(t.mseconds() as u32))
.map(|t| t.mseconds() as u32)
.unwrap_or(0),
position: self
.play
.position()
.and_then(|t| Some(t.mseconds() as u32))
.map(|t| t.mseconds() as u32)
.unwrap_or(0),
};
let play_state = match *self.state.lock().unwrap() {