From e7fe007d8c28a74f04e37642be45f54a4e0d9d9f Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 23 Jun 2021 13:25:26 -0300 Subject: [PATCH] Fixed the account management commands --- src/args/errors.rs | 24 +++++++++++++++++++ src/args/mod.rs | 21 +++++++++++++---- src/commands/addaccount.rs | 21 +++++++++++++---- src/commands/errors.rs | 30 ++++++++++++++++++++++++ src/commands/mod.rs | 10 +++++--- src/commands/removeaccount.rs | 40 +++++++++++++++++++++++++++++++ src/main.rs | 44 +++-------------------------------- 7 files changed, 137 insertions(+), 53 deletions(-) create mode 100644 src/args/errors.rs create mode 100644 src/commands/errors.rs create mode 100644 src/commands/removeaccount.rs diff --git a/src/args/errors.rs b/src/args/errors.rs new file mode 100644 index 0000000..255b6d2 --- /dev/null +++ b/src/args/errors.rs @@ -0,0 +1,24 @@ +/* + DOWNFAV - Download Favourites + Copyright (C) 2020 Julio Biason + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +/// Parsing errors +#[derive(Debug)] +pub enum ParsingError { + /// The command is not recognized + UnknownCommand, +} diff --git a/src/args/mod.rs b/src/args/mod.rs index 821fa24..9d45b3f 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -16,20 +16,22 @@ along with this program. If not, see . */ +mod errors; + use clap::App; use clap::Arg; use clap::SubCommand; -use super::commands::addaccount::AddAccount; -use super::commands::Command; +use self::errors::ParsingError; +use super::commands; /// Parse the command line, returning the necessary command. -pub fn parse() -> Box { +pub fn parse() -> Result, ParsingError> { let parser = App::new(clap::crate_name!()) .version(clap::crate_version!()) .author(clap::crate_authors!()) .about(clap::crate_description!()) - .arg(Arg::with_name("account").help("Account alias")) + .arg(Arg::with_name("account").help("Account alias").required(true)) .subcommand( SubCommand::with_name("create").about("Create the account"), ) @@ -61,5 +63,14 @@ pub fn parse() -> Box { .required(true)))); let matches = parser.get_matches(); - Box::new(AddAccount::new("something")) + let account_name = matches.value_of("account").unwrap(); // we can unwrap 'cause it is required + match matches.subcommand() { + ("create", _) => Ok(Box::new(commands::addaccount::AddAccount::new( + account_name.into(), + ))), + ("remove", _) => Ok(Box::new( + commands::removeaccount::RemoveAccount::new(account_name.into()), + )), + _ => Err(ParsingError::UnknownCommand), + } } diff --git a/src/commands/addaccount.rs b/src/commands/addaccount.rs index e975f92..208a99e 100644 --- a/src/commands/addaccount.rs +++ b/src/commands/addaccount.rs @@ -16,15 +16,16 @@ along with this program. If not, see . */ -use super::Command; - use std::io; use std::io::prelude::*; use elefren::helpers::cli; use elefren::prelude::*; +use super::errors::CommandError; +use super::Command; use crate::config::config::Config; +use crate::config::errors::ConfigError; pub struct AddAccount { name: String, @@ -37,7 +38,7 @@ impl AddAccount { } impl Command for AddAccount { - fn execute(&self) -> Result<(), ()> { + fn execute(&self) -> Result<&str, CommandError> { let mut server = String::new(); print!("Your server URL: "); @@ -53,6 +54,18 @@ impl Command for AddAccount { let mut config = Config::open()?; config.add_account(&self.name, connection); config.save()?; - Ok(()) + Ok("Account created") + } +} + +impl From for CommandError { + fn from(_: elefren::Error) -> CommandError { + CommandError::ConnectError + } +} + +impl From for CommandError { + fn from(_: ConfigError) -> CommandError { + CommandError::ConfigError } } diff --git a/src/commands/errors.rs b/src/commands/errors.rs new file mode 100644 index 0000000..ab9d680 --- /dev/null +++ b/src/commands/errors.rs @@ -0,0 +1,30 @@ +/* + DOWNFAV - Download Favourites + Copyright (C) 2020 Julio Biason + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +/// Errors for the commands +#[derive(Debug)] +pub enum CommandError { + /// Error connecting to the Mastodon account + ConnectError, + + /// Configuration file is broken + ConfigError, + + /// The requested account does not exist + NoSuchAccount, +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b0aa707..a67110a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -16,8 +16,12 @@ along with this program. If not, see . */ +pub mod addaccount; +pub mod errors; +pub mod removeaccount; + +use self::errors::CommandError; + pub trait Command { - fn execute(&self) -> Result<(), ()>; + fn execute(&self) -> Result<&str, CommandError>; } - -pub mod addaccount; diff --git a/src/commands/removeaccount.rs b/src/commands/removeaccount.rs new file mode 100644 index 0000000..4fa9d9e --- /dev/null +++ b/src/commands/removeaccount.rs @@ -0,0 +1,40 @@ +/* + DOWNFAV - Download Favourites + Copyright (C) 2020 Julio Biason + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +use super::errors::CommandError; +use super::Command; +use crate::config::config::Config; + +pub struct RemoveAccount { + name: String, +} + +impl RemoveAccount { + pub fn new(name: &str) -> Self { + Self { name: name.into() } + } +} + +impl Command for RemoveAccount { + fn execute(&self) -> Result<&str, CommandError> { + let mut config = Config::open()?; + config.remove_account(&self.name); + config.save()?; + Ok("Account removed") + } +} diff --git a/src/main.rs b/src/main.rs index 676da63..0c33508 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,6 @@ along with this program. If not, see . */ -use std::io; - mod args; mod commands; mod config; @@ -27,48 +25,12 @@ mod storage; fn main() { env_logger::init(); - let command = args::parse(); - command.execute(); - // match args::parse() { - // Ok(command) => command.execute(), - // Err(()) => println!("error"), - // } -} - -/// Remove account -fn remove_account(name: &str) { - let mut config = config::config::Config::open().unwrap(); - config.remove_account(name); - config.save().unwrap(); -} - -/// Add a storage for an account -fn add_storage(account: &str, storage: &str) { - log::debug!("Adding storage \"{}\" for account \"{}\"", account, storage); - match storage { - "filesystem" => add_filesystem(account), - _ => println!("Storage unknown"), + match args::parse() { + Ok(command) => println!("{}", command.execute().unwrap()), + Err(e) => println!("Error: {:?}", e), } } -fn add_filesystem(account: &str) { - println!("Path for the files: "); - let mut path = String::new(); - io::stdin() - .read_line(&mut path) - .expect("You need to enter yoru server URL"); -} - -/// Fetch from all accounts -fn fetch_all_favourites() { - // let mut config = config::config::Config::open().unwrap(); -} - -/// Fetch the favourites from a single account -fn fetch_favourites(_account: &str) { - // -} - // Retrieve favourites // fn fetch_favourites() { // let config = match config::AccountConfig::get() {