diff --git a/src/args/mod.rs b/src/args/mod.rs index 03c3f54..821fa24 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -20,26 +20,11 @@ use clap::App; use clap::Arg; use clap::SubCommand; -/// Possible commands -pub enum Command { - /// Got a command that we don't know - Unknown, - /// Fetch favourites from all accounts - FetchAll, - /// Fetch favourites from a specific account - Fetch(String), - /// Add a new account with the specified name - CreateAccount(String), - /// Remove the account with the specified name - RemoveAccount(String), - /// Add a storage for the account - AddStorage(String, String), - /// Remove a storage in the account - RemoveStorage(String, String), -} +use super::commands::addaccount::AddAccount; +use super::commands::Command; /// Parse the command line, returning the necessary command. -pub fn parse() -> Command { +pub fn parse() -> Box { let parser = App::new(clap::crate_name!()) .version(clap::crate_version!()) .author(clap::crate_authors!()) @@ -73,32 +58,8 @@ pub fn parse() -> Command { .arg(Arg::with_name("type") .help("Storage type to be removed") .takes_value(true) - .required(true)))); + .required(true)))); + let matches = parser.get_matches(); - if let Some(account) = matches.value_of("account") { - match matches.subcommand() { - ("fetch", _) => Command::Fetch(account.into()), - ("create", _) => Command::CreateAccount(account.into()), - ("remove", _) => Command::RemoveAccount(account.into()), - ("storage", Some(arguments)) => { - if let Some(_type) = arguments.value_of("type") { - match arguments.subcommand() { - ("add", _) => { - Command::AddStorage(account.into(), _type.into()) - } - ("remove", _) => { - Command::RemoveStorage(account.into(), _type.into()) - } - _ => Command::Unknown, - } - } else { - Command::Unknown - } - } - _ => Command::Unknown, - } - } else { - log::debug!("No account provided, assuming fetch"); - Command::FetchAll - } + Box::new(AddAccount::new("something")) } diff --git a/src/commands/addaccount.rs b/src/commands/addaccount.rs new file mode 100644 index 0000000..e975f92 --- /dev/null +++ b/src/commands/addaccount.rs @@ -0,0 +1,58 @@ +/* + 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::Command; + +use std::io; +use std::io::prelude::*; + +use elefren::helpers::cli; +use elefren::prelude::*; + +use crate::config::config::Config; + +pub struct AddAccount { + name: String, +} + +impl AddAccount { + pub fn new(name: &str) -> Self { + Self { name: name.into() } + } +} + +impl Command for AddAccount { + fn execute(&self) -> Result<(), ()> { + let mut server = String::new(); + + print!("Your server URL: "); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut server) + .expect("you need to ender your server URL"); + let registration = Registration::new(server.trim()) + .client_name("Downfav") + .build()?; + let connection = cli::authenticate(registration)?.data; + + let mut config = Config::open()?; + config.add_account(&self.name, connection); + config.save()?; + Ok(()) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..b0aa707 --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,23 @@ +/* + 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 . +*/ + +pub trait Command { + fn execute(&self) -> Result<(), ()>; +} + +pub mod addaccount; diff --git a/src/main.rs b/src/main.rs index 6a085bb..676da63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,10 +18,8 @@ use std::io; -use elefren::helpers::cli; -use elefren::prelude::*; - mod args; +mod commands; mod config; mod filesystem; mod storage; @@ -29,28 +27,12 @@ mod storage; fn main() { env_logger::init(); - match args::parse() { - args::Command::FetchAll => fetch_all_favourites(), - args::Command::Fetch(account_name) => fetch_favourites(&account_name), - args::Command::CreateAccount(account_name) => { - add_account(&account_name) - } - args::Command::RemoveAccount(account_name) => { - remove_account(&account_name) - } - args::Command::AddStorage(account_name, storage_type) => { - add_storage(&account_name, &storage_type) - } - _ => println!("Unknown command"), - } -} - -/// Create a new account -fn add_account(name: &str) { - let mut config = config::config::Config::open().unwrap(); - let connection_info = connect_to_mastodon(); - config.add_account(name, connection_info); - config.save().unwrap(); + let command = args::parse(); + command.execute(); + // match args::parse() { + // Ok(command) => command.execute(), + // Err(()) => println!("error"), + // } } /// Remove account @@ -136,18 +118,3 @@ fn fetch_favourites(_account: &str) { // config.save(&new_favourite); // } // } - -/// Create a connection to a mastodon server. -fn connect_to_mastodon() -> elefren::data::Data { - println!("Your server URL: "); - let mut server = String::new(); - io::stdin() - .read_line(&mut server) - .expect("You need to enter yoru server URL"); - - let registration = Registration::new(server.trim()) - .client_name("downfav") - .build() - .unwrap(); - cli::authenticate(registration).unwrap().data -}