From c9be12f9c31f11066a839777fece842f27101e4e Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 18 Apr 2024 14:15:37 -0500 Subject: [PATCH] Prepare to write the setup stuff --- src/cli/config.rs | 8 ++++++++ src/config.rs | 28 +++++++++++++++++++++++++--- src/main.rs | 2 +- src/tasks.rs | 4 +--- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/cli/config.rs b/src/cli/config.rs index 97b7156..3d1d629 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -3,12 +3,14 @@ use crate::cli::prelude::*; #[derive(Subcommand)] pub enum Command { Show, + Setup, } impl Command { pub async fn exec(&self, tasks: SharedTasks) -> Result<()> { match self { Command::Show => self.show(tasks).await, + Command::Setup => self.setup(tasks).await, } } @@ -16,4 +18,10 @@ impl Command { println!("{:#?}", tasks.config()); Ok(()) } + + pub async fn setup(&self, tasks: SharedTasks) -> Result<()> { + // TODO: initialize known keyring values/entries + println!("You're all set!"); + Ok(()) + } } diff --git a/src/config.rs b/src/config.rs index 19d37bf..77309c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,19 +18,41 @@ pub struct Jira { pub url: String, } +#[derive(Deserialize, Debug, Clone, Default)] +pub struct Secrets { + jira_token: String, + gitlab_token: String, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Config { pub version: u64, pub gitlab: Gitlab, pub jira: Jira, + + #[serde(skip_serializing)] + pub secrets: Secrets, } impl Config { pub fn load(config_file_path: &Path) -> Result { - let c = Self::builder(config_file_path)?.build()?; - Ok(c.try_deserialize() + let builder = Self::builder(config_file_path)?.build()?; + let mut conf: Self = builder + .try_deserialize() .suggestion("run `taskr config setup` to ensure valid configuration") - .with_context(|| "failed to deserialize configuration")?) + .with_context(|| "failed to deserialize configuration")?; + + if conf.secrets.jira_token == "" { + conf.secrets.jira_token = + keyring::Entry::new("taskr", "gitlab_token")?.get_password()?; + } + + if conf.secrets.gitlab_token == "" { + conf.secrets.gitlab_token = + keyring::Entry::new("taskr", "gitlab_token")?.get_password()?; + } + + Ok(conf) } pub fn builder(config_file_path: &Path) -> Result> diff --git a/src/main.rs b/src/main.rs index 30e54cd..9381b66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod task; mod tasks; mod tui; -use crate::cli::{Cli, Commands}; +use crate::cli::Cli; use crate::prelude::*; #[tokio::main] diff --git a/src/tasks.rs b/src/tasks.rs index 62cd134..d2e5e0b 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -25,7 +25,6 @@ pub struct Tasks { config: OnceLock, gitlab: OnceLock, jira: OnceLock, - db: OnceLock, } @@ -36,6 +35,7 @@ impl Tasks { where D: AsRef, { + let db = OnceLock::new(); let config = OnceLock::new(); let gitlab = OnceLock::new(); let jira = OnceLock::new(); @@ -64,8 +64,6 @@ impl Tasks { xdg()?.create_data_directory("taskr/data")? }; - let db = OnceLock::new(); - Ok(Self { config_file_path, config,