Prepare to write the setup stuff

This commit is contained in:
Daniel Flanagan 2024-04-18 14:15:37 -05:00
parent ab9b3647f9
commit c9be12f9c3
4 changed files with 35 additions and 7 deletions

View file

@ -3,12 +3,14 @@ use crate::cli::prelude::*;
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Command { pub enum Command {
Show, Show,
Setup,
} }
impl Command { impl Command {
pub async fn exec(&self, tasks: SharedTasks) -> Result<()> { pub async fn exec(&self, tasks: SharedTasks) -> Result<()> {
match self { match self {
Command::Show => self.show(tasks).await, Command::Show => self.show(tasks).await,
Command::Setup => self.setup(tasks).await,
} }
} }
@ -16,4 +18,10 @@ impl Command {
println!("{:#?}", tasks.config()); println!("{:#?}", tasks.config());
Ok(()) Ok(())
} }
pub async fn setup(&self, tasks: SharedTasks) -> Result<()> {
// TODO: initialize known keyring values/entries
println!("You're all set!");
Ok(())
}
} }

View file

@ -18,19 +18,41 @@ pub struct Jira {
pub url: String, pub url: String,
} }
#[derive(Deserialize, Debug, Clone, Default)]
pub struct Secrets {
jira_token: String,
gitlab_token: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config { pub struct Config {
pub version: u64, pub version: u64,
pub gitlab: Gitlab, pub gitlab: Gitlab,
pub jira: Jira, pub jira: Jira,
#[serde(skip_serializing)]
pub secrets: Secrets,
} }
impl Config { impl Config {
pub fn load(config_file_path: &Path) -> Result<Self> { pub fn load(config_file_path: &Path) -> Result<Self> {
let c = Self::builder(config_file_path)?.build()?; let builder = Self::builder(config_file_path)?.build()?;
Ok(c.try_deserialize() let mut conf: Self = builder
.try_deserialize()
.suggestion("run `taskr config setup` to ensure valid configuration") .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<ConfigBuilder<DefaultState>> pub fn builder(config_file_path: &Path) -> Result<ConfigBuilder<DefaultState>>

View file

@ -12,7 +12,7 @@ mod task;
mod tasks; mod tasks;
mod tui; mod tui;
use crate::cli::{Cli, Commands}; use crate::cli::Cli;
use crate::prelude::*; use crate::prelude::*;
#[tokio::main] #[tokio::main]

View file

@ -25,7 +25,6 @@ pub struct Tasks {
config: OnceLock<Config>, config: OnceLock<Config>,
gitlab: OnceLock<GitLab>, gitlab: OnceLock<GitLab>,
jira: OnceLock<Jira>, jira: OnceLock<Jira>,
db: OnceLock<Db>, db: OnceLock<Db>,
} }
@ -36,6 +35,7 @@ impl Tasks {
where where
D: AsRef<Path>, D: AsRef<Path>,
{ {
let db = OnceLock::new();
let config = OnceLock::new(); let config = OnceLock::new();
let gitlab = OnceLock::new(); let gitlab = OnceLock::new();
let jira = OnceLock::new(); let jira = OnceLock::new();
@ -64,8 +64,6 @@ impl Tasks {
xdg()?.create_data_directory("taskr/data")? xdg()?.create_data_directory("taskr/data")?
}; };
let db = OnceLock::new();
Ok(Self { Ok(Self {
config_file_path, config_file_path,
config, config,