WIP keyring

This commit is contained in:
Daniel Flanagan 2024-04-17 13:27:18 -05:00
parent 7dfcb2ad95
commit e581727d23
9 changed files with 989 additions and 53 deletions

931
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,7 @@ clap_derive = "4.5.4"
color-eyre = "0.6.3"
crossterm = "0.27.0"
futures = "0.3.30"
keyring = "2.3.2"
ratatui = "0.26.2"
redact = "0.1.9"
regex = "1.10.3"

View file

@ -13,7 +13,7 @@ pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// The directory to write log files
/// The directory to write log files (defaults to $XDG_CACHE_HOME/taskr/logs)
#[arg(long, default_value = None)]
pub logs_directory: Option<String>,
@ -22,6 +22,10 @@ pub struct Cli {
/// for details
#[arg(long, default_value = None)]
pub tracing_env_filter: Option<String>,
/// The directory that data is stored in (defaults to $XDG_DATA_HOME/taskr/data)
#[arg(long, default_value = None)]
pub data_directory: Option<String>,
}
#[derive(Subcommand)]
@ -63,8 +67,7 @@ mod list {
}
impl Args {
pub async fn list(&self) -> Result<()> {
let tasks = Arc::new(crate::tasks::Tasks::try_new()?);
pub async fn list(&self, tasks: SharedTasks) -> Result<()> {
if self.sync {
eprintln!("Syncing...");
tasks.sync().await?;
@ -92,9 +95,9 @@ mod sync {
pub struct Args {}
impl Args {
pub async fn sync(&self) -> Result<()> {
pub async fn sync(&self, tasks: SharedTasks) -> Result<()> {
eprintln!("Syncing...");
Arc::new(crate::tasks::Tasks::try_new()?).sync().await?;
tasks.sync().await?;
eprintln!("Done!");
Ok(())
}

View file

@ -6,9 +6,9 @@ pub enum Command {
}
impl Command {
pub async fn exec(&self) -> Result<()> {
pub async fn exec(&self, tasks: SharedTasks) -> Result<()> {
match self {
Command::Me(args) => args.me().await,
Command::Me(args) => args.me(tasks).await,
}
}
}
@ -17,8 +17,7 @@ impl Command {
pub struct MeArgs {}
impl MeArgs {
pub async fn me(&self) -> Result<()> {
let tasks = crate::tasks::Tasks::try_new()?;
pub async fn me(&self, tasks: SharedTasks) -> Result<()> {
println!("{:?}", tasks.gitlab.me().await?);
Ok(())
}

View file

@ -6,9 +6,9 @@ pub enum Command {
}
impl Command {
pub async fn exec(&self) -> Result<()> {
pub async fn exec(&self, tasks: SharedTasks) -> Result<()> {
match self {
Command::Issue(args) => args.issue().await,
Command::Issue(args) => args.issue(tasks).await,
}
}
}
@ -20,8 +20,7 @@ pub struct IssueArgs {
}
impl IssueArgs {
pub async fn issue(&self) -> Result<()> {
let tasks = crate::tasks::Tasks::try_new()?;
pub async fn issue(&self, tasks: SharedTasks) -> Result<()> {
let issue = tasks.jira.issue(&self.key).await?;
println!("{issue:?}");
Ok(())

View file

@ -23,15 +23,15 @@ async fn main() -> Result<()> {
// https://docs.rs/tracing-appender/latest/tracing_appender/non_blocking/struct.WorkerGuard.html
let _log_guard = observe::setup_logging(cli.logs_directory, cli.tracing_env_filter)?;
println!("Starting...");
trace!("Starting...");
info!("Starting...");
println!("Initializing taskr...");
let tasks = Arc::new(crate::tasks::Tasks::try_new(cli.data_directory)?);
let result = match cli.command {
Commands::Sync(args) => args.sync().await,
Commands::Ui(_args) => tui::run().await,
Commands::List(args) => args.list().await,
Commands::Jira(jira) => jira.exec().await,
Commands::Gitlab(gitlab) => gitlab.exec().await,
Commands::Sync(args) => args.sync(tasks).await,
Commands::Ui(_args) => tui::run(tasks).await,
Commands::List(args) => args.list(tasks).await,
Commands::Jira(jira) => jira.exec(tasks).await,
Commands::Gitlab(gitlab) => gitlab.exec(tasks).await,
};
result
}

View file

@ -5,3 +5,7 @@ pub use regex::Regex;
pub use std::sync::Arc;
pub use tokio::sync::Mutex;
pub use tracing::{debug, error, info, trace, warn};
use crate::tasks::Tasks;
pub type SharedTasks = Arc<Tasks>;

View file

@ -4,6 +4,7 @@ use std::{
collections::{HashMap, HashSet},
env,
hash::RandomState,
path::Path,
process::Command,
sync::OnceLock,
};
@ -25,17 +26,41 @@ pub struct Tasks {
const MY_DONE_STATUS_CATEGORY_KEY: &str = "done";
impl Tasks {
pub fn try_new() -> Result<Self> {
// TODO: cache, use keyring, talk to a daemon, or otherwise cache this safely
// TODO: or find a way to more-lazily load the token?
let gl_token = env::var("GITLAB_TOKEN").or_else(|_| -> Result<String> {
let output = Command::new("pass")
.arg("client/divvy/gitlab-glpat")
.output()?;
pub fn try_new<D>(data_dir: Option<D>) -> Result<Self>
where
D: AsRef<Path>,
{
// TODO: find a way to more-lazily load the token?
let gitlab_token_entry = keyring::Entry::new("taskr", "gitlab_token")?;
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
})?;
let gl_token = match gitlab_token_entry.get_password() {
Ok(token) => {
info!("GitLab token loaded from keyring");
token
}
Err(_) => {
let token = match env::var("GITLAB_TOKEN") {
Ok(token) => {
info!("GitLab token loaded from environment variable GITLAB_TOKEN");
token
}
Err(_) => {
let output = Command::new("pass")
.arg("client/divvy/gitlab-glpat")
.output()?;
let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
info!("GitLab token loaded from password-store entry client/divvy/gitlab-glpat");
result
}
};
info!("GitLab token stored in keyring");
gitlab_token_entry.set_password(&token)?;
token
}
};
let gitlab = GitLab::try_new("https://git.hq.bill.com/api/v4", &gl_token)?;
// TODO: ensure the token works?
// TODO: cache, use keyring, talk to a daemon, or otherwise cache this safely
// TODO: or find a way to more-lazily load the token?
@ -48,7 +73,20 @@ impl Tasks {
})?;
let jira = Jira::try_new("https://billcom.atlassian.net", &jira_token)?;
let db = sled::open("data/tasks")?;
let mut default_data_dir = Default::default();
let data_dir = data_dir
.as_ref()
.map(D::as_ref)
.map(Result::Ok)
.unwrap_or_else(|| {
// really want to avoid calling this so it's put in here to force it to be lazy
// also have to shove it in a function-scoped variable so the
// reference we create in this closure has the same lifetime as the function
default_data_dir =
xdg::BaseDirectories::new()?.create_data_directory("taskr/data")?;
Ok(default_data_dir.as_ref())
})?;
let db = sled::open(data_dir)?;
Ok(Self { gitlab, jira, db })
}

View file

@ -1,6 +1,5 @@
use crate::prelude::*;
use crate::task;
use crate::tasks::Tasks;
use crossterm::{
event::{self, KeyCode, KeyEventKind},
@ -13,9 +12,7 @@ use ratatui::{
};
use std::io::stdout;
pub async fn run() -> Result<()> {
let t = Tasks::try_new()?;
pub async fn run(t: SharedTasks) -> Result<()> {
// print!("{ANSI_CLEAR}");
// let gitlab_user = tasks.gitlab.me().await?;
// info!("{gitlab_user:#?}");