Skip to content

Commit

Permalink
feat: logout command implemented. login command started.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 17, 2024
1 parent 8da7a71 commit 53e30b4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 68 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion src/credential_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 11 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::{Command, Arg};

mod subcommands;
Expand Down Expand Up @@ -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(())
}
}
34 changes: 10 additions & 24 deletions src/subcommands/login_subcommand.rs
Original file line number Diff line number Diff line change
@@ -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::<String>("URL").context("URL not provided.")?;
let user = arg_matches.get_one::<String>("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
// }
Expand All @@ -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::<String>("URL")?;
let user = arg_matches.get_one::<String>("USER")?;

self.url = url.clone();
self.user = user.clone();

Some(())
}
}
32 changes: 8 additions & 24 deletions src/subcommands/logout_subcommand.rs
Original file line number Diff line number Diff line change
@@ -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::<String>("URL").context("URL not provided.")?;

fn parse_args(&mut self, arg_matches: &clap::ArgMatches) -> Option<()> {
let url = arg_matches.get_one::<String>("URL")?;
self.url = url.clone();
let credential_manager = CredentialManager { };
credential_manager.remove_credential(url)?;

Some(())
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/subcommands/subcommand.rs
Original file line number Diff line number Diff line change
@@ -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<()>;
}

0 comments on commit 53e30b4

Please sign in to comment.