diff --git a/default.nix b/default.nix index 5637cf2..eca00ba 100644 --- a/default.nix +++ b/default.nix @@ -4,7 +4,7 @@ let manifest = (pkgs.lib.importTOML ./winapps-cli/Cargo.toml).package; in -pkgs.rustPlatform.buildRustPackage rec { +pkgs.rustPlatform.buildRustPackage { pname = manifest.name; version = manifest.version; cargoLock.lockFile = ./Cargo.lock; @@ -16,4 +16,5 @@ pkgs.rustPlatform.buildRustPackage rec { PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; propagatedBuildInputs = with pkgs; [ freerdp3 ]; + wrapperArgs = [ ]; } diff --git a/winapps-cli/src/main.rs b/winapps-cli/src/main.rs index dc12cd8..0cb9c01 100644 --- a/winapps-cli/src/main.rs +++ b/winapps-cli/src/main.rs @@ -23,13 +23,14 @@ fn main() -> Result<()> { // .with_timer(tracing_subscriber::fmt::time::uptime()) .without_time() .with_target(false) + .with_level(true) .init(); let cli = cli(); let matches = cli.clone().get_matches(); + Config::load(None)?; let config = Config::get(); - config.load(None)?; let client = Freerdp::new(); let backend = config.get_backend(); diff --git a/winapps/src/backend/docker.rs b/winapps/src/backend/docker.rs index 5606629..2c35b77 100644 --- a/winapps/src/backend/docker.rs +++ b/winapps/src/backend/docker.rs @@ -1,7 +1,8 @@ -use crate::command::execute_str; -use crate::{ensure, Backend, Config, Error, Result}; use std::sync::RwLockReadGuard; +use crate::{command::execute_str, ensure, Backend, Config, Error, Result}; + +#[derive(Debug)] pub struct Docker { config: RwLockReadGuard<'static, Config>, } diff --git a/winapps/src/backend/mod.rs b/winapps/src/backend/mod.rs index 9b2afbb..b0c52b8 100644 --- a/winapps/src/backend/mod.rs +++ b/winapps/src/backend/mod.rs @@ -1,10 +1,10 @@ -mod docker; +use std::fmt::Debug; + +use crate::{backend::docker::Docker, Config, Result}; -use crate::backend::docker::Docker; -use crate::config::Config; -use crate::errors::Result; +mod docker; -pub trait Backend { +pub trait Backend: Debug { fn check_depends(&self) -> Result<()>; fn get_host(&self) -> String; diff --git a/winapps/src/config/apps.rs b/winapps/src/config/apps.rs index 12b33fd..9f8eab4 100644 --- a/winapps/src/config/apps.rs +++ b/winapps/src/config/apps.rs @@ -1,5 +1,4 @@ -use crate::config::App; -use crate::Config; +use crate::{config::App, Config}; impl PartialEq for App { fn eq(&self, other: &Self) -> bool { diff --git a/winapps/src/config/operations.rs b/winapps/src/config/operations.rs index 7a427f0..e9fe63d 100644 --- a/winapps/src/config/operations.rs +++ b/winapps/src/config/operations.rs @@ -4,6 +4,8 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::{LazyLock, RwLock, RwLockReadGuard}; +use tracing::warn; + use crate::{bail, Config, Error, IntoResult, Result}; fn path_ok(path: &Path) -> Result<()> { @@ -27,25 +29,17 @@ impl Config { CONFIG.read().unwrap() } - fn get_path(path: Option<&str>) -> Result { - let path = match (path, dirs::config_dir()) { - (Some(path), _) => Ok(PathBuf::from(path)), - (None, Some(path)) => Ok(path), - _ => "Could not find $XDG_CONFIG_HOME and no config path specified".into_result(), - } - .map(|path| path.join("winapps").join("config.toml"))?; - - let parent = path.parent().unwrap(); - path_ok(parent)?; - - Ok(path) - } - - pub fn load(&self, path: Option<&str>) -> Result<()> { + /// Reads the config from disk. + /// + /// Note: Since this uses a RwLock under the hood, call this before aquiring any read locks + /// (ensure `Config::get` isn't called anywhere unless the config is already loaded) + /// TODO: This behaviour is not ideal. + pub fn load(path: Option<&str>) -> Result<()> { let config_path = Self::get_path(path)?; if !config_path.exists() { - return self.save(path); + warn!("Config does not exist, writing default..."); + return CONFIG.read().unwrap().save(path); } let config_file = fs::read_to_string(config_path).into_result()?; @@ -55,12 +49,26 @@ impl Config { bail!(Error::Config("More than one backend enabled, please set only one of libvirt.enable, container.enable, and manual.enable")); } - let mut global_config = CONFIG.write().unwrap(); + let mut global_config = CONFIG.try_write().unwrap(); *global_config = config; Ok(()) } + fn get_path(path: Option<&str>) -> Result { + let path = match (path, dirs::config_dir()) { + (Some(path), _) => Ok(PathBuf::from(path)), + (None, Some(path)) => Ok(path), + _ => "Could not find $XDG_CONFIG_HOME and no config path specified".into_result(), + } + .map(|path| path.join("winapps").join("config.toml"))?; + + let parent = path.parent().unwrap(); + path_ok(parent)?; + + Ok(path) + } + pub fn save(&self, path: Option<&str>) -> Result<()> { let config_path = Self::get_path(path)?; let serialized_config = toml::to_string(&self).into_result()?; diff --git a/winapps/src/remote_client/freerdp.rs b/winapps/src/remote_client/freerdp.rs index 7776682..0a430fb 100644 --- a/winapps/src/remote_client/freerdp.rs +++ b/winapps/src/remote_client/freerdp.rs @@ -1,9 +1,9 @@ use std::process::{Command, Stdio}; use std::sync::RwLockReadGuard; + use tracing::{info, warn}; -use crate::command::execute; -use crate::{Backend, Config, RemoteClient, Result}; +use crate::{command::execute, Backend, Config, RemoteClient, Result}; pub struct Freerdp { config: RwLockReadGuard<'static, Config>,