Skip to content

Commit

Permalink
feat: parsing implemented. logout command implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 17, 2024
1 parent de4b817 commit 6db67f9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,22 @@ fn main() {

match matches.subcommand() {
Some(("login", sub_matches)) => {
let login_command = LoginSubcommand { };
login_command.execute(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
}
},
Some(("logout", sub_matches)) => {
let logout_command = LogoutSubcommand { };
logout_command.execute(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
}
}
_ => println!("No subcommand")
}
Expand Down
27 changes: 22 additions & 5 deletions src/subcommands/login_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ use crate::subcommands::Subcommand;
use crate::credential_manager::CredentialManager;

pub struct LoginSubcommand {

url: String,
user: String
}

impl LoginSubcommand {
fn login(user: &str, url: &str) {
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();

let credential_manager = CredentialManager { };

if credential_manager.has_credential(url) {
Expand All @@ -23,10 +36,14 @@ impl LoginSubcommand {

// else throw error
}
}

impl Subcommand for LoginSubcommand {
fn execute(&self, arg_matches: &ArgMatches) {
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(())
}
}
25 changes: 19 additions & 6 deletions src/subcommands/logout_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ use crate::subcommands::subcommand::Subcommand;
use crate::credential_manager::CredentialManager;

pub struct LogoutSubcommand {

url: String
}

impl LogoutSubcommand {
fn logout(url: &str) {
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()
}
}
}

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);
}
}
}

impl Subcommand for LogoutSubcommand {
fn execute(&self, arg_matches: &clap::ArgMatches) {

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

Some(())
}
}
3 changes: 2 additions & 1 deletion src/subcommands/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::ArgMatches;

pub trait Subcommand {
fn execute(&self, arg_matches: &ArgMatches);
fn execute(&self);
fn parse_args(&mut self, arg_matches: &ArgMatches) -> Option<()>;
}

0 comments on commit 6db67f9

Please sign in to comment.