diff --git a/Cargo.toml b/Cargo.toml index bff9f87..54d03a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,3 @@ clap = "4.5.21" futures-macro = "0.3.31" keyring = { version = "3.6.1", features = ["apple-native", "windows-native", "sync-secret-service"] } rusqlite = { version = "0.32.1", features = ["bundled"] } -thiserror = "2.0.3" diff --git a/src/credential_manager.rs b/src/credential_manager.rs index ece76b8..4fa3a13 100644 --- a/src/credential_manager.rs +++ b/src/credential_manager.rs @@ -4,7 +4,7 @@ use aes_gcm::{aead::{Aead, OsRng}, AeadCore, Aes256Gcm, Key, KeyInit, Nonce}; use app_dirs2::{AppDataType, AppInfo, app_root}; use anyhow::{Result, Context}; use keyring::Entry; -use rusqlite::{Connection, MappedRows}; +use rusqlite::Connection; #[derive(Debug)] struct DatabaseCredential { diff --git a/src/main.rs b/src/main.rs index 26c8f12..8d915a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use anyhow::Result; use clap::{Command, Arg}; mod subcommands; @@ -44,28 +45,22 @@ fn cli() -> Command { ) } -fn main() { +fn main() -> Result<()> { let matches = cli().get_matches(); match matches.subcommand() { Some(("login", sub_matches)) => { - let mut login_command = LoginSubcommand::new(); - if login_command.parse_args(sub_matches).is_some() { - login_command.execute(); - } - else { - // TODO: We failed processing - } + let login_command = LoginSubcommand { }; + login_command.execute(sub_matches)?; + + Ok(()) }, Some(("logout", sub_matches)) => { - let mut logout_command = LogoutSubcommand::new(); - if logout_command.parse_args(sub_matches).is_some() { - logout_command.execute(); - } - else { - // TODO: We failed processing - } + let logout_command = LogoutSubcommand { }; + logout_command.execute(sub_matches)?; + + Ok(()) } - _ => println!("No subcommand") + _ => Ok(()) } } diff --git a/src/subcommands/login_subcommand.rs b/src/subcommands/login_subcommand.rs index 1cb47c1..e4302f2 100644 --- a/src/subcommands/login_subcommand.rs +++ b/src/subcommands/login_subcommand.rs @@ -1,29 +1,25 @@ +use anyhow::{Context, Result}; use clap::ArgMatches; use crate::subcommands::Subcommand; use crate::credential_manager::CredentialManager; pub struct LoginSubcommand { - url: String, - user: String -} - -impl LoginSubcommand { - pub fn new() -> LoginSubcommand { - LoginSubcommand { - url: "".to_string(), - user: "".to_string() - } - } } impl Subcommand for LoginSubcommand { - fn execute(&self) { - let url = self.url.as_str(); - let user = self.user.as_str(); + fn execute(&self, arg_matches: &ArgMatches) -> Result<()> { + let url = arg_matches.get_one::("URL").context("URL not provided.")?; + let user = arg_matches.get_one::("USER").context("USER not provided.")?; let credential_manager = CredentialManager { }; + if !credential_manager.has_credential(url)? { + // TODO need to ask for password from user + } + + Ok(()) + // if credential_manager.has_credential(url) { // // get password and totp command // } @@ -36,14 +32,4 @@ impl Subcommand for LoginSubcommand { // else throw error } - - fn parse_args(&mut self, arg_matches: &ArgMatches) -> Option<()> { - let url = arg_matches.get_one::("URL")?; - let user = arg_matches.get_one::("USER")?; - - self.url = url.clone(); - self.user = user.clone(); - - Some(()) - } } \ No newline at end of file diff --git a/src/subcommands/logout_subcommand.rs b/src/subcommands/logout_subcommand.rs index 8e1a720..a7604b8 100644 --- a/src/subcommands/logout_subcommand.rs +++ b/src/subcommands/logout_subcommand.rs @@ -1,35 +1,19 @@ use crate::subcommands::subcommand::Subcommand; use crate::credential_manager::CredentialManager; -pub struct LogoutSubcommand { - url: String -} +use anyhow::{Context, Result}; +use clap::ArgMatches; -impl LogoutSubcommand { - pub fn new() -> LogoutSubcommand { - // Intentionally pass an empty string instead of using Some. - // Url is required and will be handled by parsing. - // We will still check during execution to see if the url is set just in case. - LogoutSubcommand { - url: "".to_string() - } - } +pub struct LogoutSubcommand { } impl Subcommand for LogoutSubcommand { - fn execute(&self) { - let url = self.url.as_str(); - let credential_manager = CredentialManager { }; - - // if credential_manager.has_credential(url) { - // credential_manager.remove_credential(url); - // } - } + fn execute(&self, arg_matches: &ArgMatches) -> Result<()> { + let url = arg_matches.get_one::("URL").context("URL not provided.")?; - fn parse_args(&mut self, arg_matches: &clap::ArgMatches) -> Option<()> { - let url = arg_matches.get_one::("URL")?; - self.url = url.clone(); + let credential_manager = CredentialManager { }; + credential_manager.remove_credential(url)?; - Some(()) + Ok(()) } } diff --git a/src/subcommands/subcommand.rs b/src/subcommands/subcommand.rs index bc1d544..696a469 100644 --- a/src/subcommands/subcommand.rs +++ b/src/subcommands/subcommand.rs @@ -1,6 +1,6 @@ +use anyhow::Result; use clap::ArgMatches; pub trait Subcommand { - fn execute(&self); - fn parse_args(&mut self, arg_matches: &ArgMatches) -> Option<()>; + fn execute(&self, arg_matches: &ArgMatches) -> Result<()>; } \ No newline at end of file