diff --git a/src/cli.rs b/src/cli.rs index 492655f..3b84916 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -29,7 +29,7 @@ pub struct Cli { /// The configuration file to load (defaults to $XDG_CONFIG_HOME/taskr/config.toml) #[arg(long, default_value = None)] - pub data_directory: Option, + pub config_file_path: Option, } #[derive(Subcommand)] diff --git a/src/config.rs b/src/config.rs index 4951b2b..225bc3c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use std::{collections::HashMap, fmt::Debug}; +use std::{collections::HashMap, fmt::Debug, path::Path}; use config::{builder::DefaultState, Config as CConfig, ConfigBuilder}; use serde::{Deserialize, Serialize}; @@ -20,36 +20,35 @@ pub struct Jira { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Config { pub version: u64, - pub gitlab: Option, - pub jira: Option, + pub gitlab: Gitlab, + pub jira: Jira, } -const CONFIG_FILE_PATH: &str = "./conf.toml"; - impl Config { - pub fn load() -> Result { - let c = Self::builder()?.build()?; + pub fn load

(config_file_path: Option

) -> Result + where + P: AsRef, + { + let c = Self::builder(config_file_path)?.build()?; Ok(c.try_deserialize()?) } - pub fn builder() -> Result> { - let p = std::path::PathBuf::from(CONFIG_FILE_PATH); - if !p.exists() { - // TODO: this won't output since tracing is not setup before we load - // config, so we need something like - // https://docs.rs/tracing-capture/latest/tracing_capture/ - // to capture trace events to be output _after_ configuration has - // loaded and the "actual" subscriber is setup - warn!( - "Config file '{}' doesn't exist, so defaults will likely be loaded!", - p.display(), - ); - } + pub fn builder

(config_file_path: Option

) -> Result> + where + P: AsRef, + { + let config_file_path: Box> = config_file_path + .as_ref() + .map(|s| Ok::>, anyhow::Error>(Box::new(s.as_ref()))) + .unwrap_or_else(|| { + Ok(Box::new( + xdg::BaseDirectories::new()?.get_config_file("taskr/config.toml"), + )) + })?; - // TODO: log whether or not we were able to load conf.toml? Ok(Self::default_builder() .map_err(anyhow::Error::from)? - .add_source(config::File::from(p).required(false)) + .add_source(config::File::from((*config_file_path).as_ref()).required(false)) .add_source(config::Environment::with_prefix("taskr").separator("__"))) } diff --git a/src/main.rs b/src/main.rs index b00d950..08c2c8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,8 @@ use crate::prelude::*; #[tokio::main] async fn main() -> Result<()> { - let conf = Config::load()?; let cli = Cli::new(); + let conf = Config::load(cli.config_file_path)?; // this guard causes logs to be flushed when dropped (which is at the end of // this function which would be the end of our program) diff --git a/src/tasks.rs b/src/tasks.rs index 2223f47..c306201 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -108,14 +108,8 @@ impl Tasks { token } }; - let result = GitLab::try_new( - &format!( - "{}/api/v4", - self.config - .require_or_prompt_and_set::("gitlab.url")? - ), - &gl_token, - )?; + let result = + GitLab::try_new(&format!("{}/api/v4", self.config.gitlab.url), &gl_token)?; let _ = self.gitlab.set(result); Ok(self.gitlab.get().unwrap()) // TODO: ensure the token works? @@ -158,14 +152,7 @@ impl Tasks { } }; - let result = Jira::try_new( - &format!( - "{}", - self.config - .require_or_prompt_and_set::("jira.url")? - ), - &jira_token, - )?; + let result = Jira::try_new(&format!("{}", self.config.jira.url), &jira_token)?; let _ = self.jira.set(result); Ok(self.jira.get().unwrap()) // TODO: ensure the token works?