Browse Source

Switching to a Command pattern

master
Julio Biason 3 years ago
parent
commit
8422f053ba
  1. 51
      src/args/mod.rs
  2. 58
      src/commands/addaccount.rs
  3. 23
      src/commands/mod.rs
  4. 47
      src/main.rs

51
src/args/mod.rs

@ -20,26 +20,11 @@ use clap::App;
use clap::Arg; use clap::Arg;
use clap::SubCommand; use clap::SubCommand;
/// Possible commands use super::commands::addaccount::AddAccount;
pub enum Command { use super::commands::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),
}
/// Parse the command line, returning the necessary command. /// Parse the command line, returning the necessary command.
pub fn parse() -> Command { pub fn parse() -> Box<dyn Command> {
let parser = App::new(clap::crate_name!()) let parser = App::new(clap::crate_name!())
.version(clap::crate_version!()) .version(clap::crate_version!())
.author(clap::crate_authors!()) .author(clap::crate_authors!())
@ -73,32 +58,8 @@ pub fn parse() -> Command {
.arg(Arg::with_name("type") .arg(Arg::with_name("type")
.help("Storage type to be removed") .help("Storage type to be removed")
.takes_value(true) .takes_value(true)
.required(true)))); .required(true))));
let matches = parser.get_matches(); let matches = parser.get_matches();
if let Some(account) = matches.value_of("account") { Box::new(AddAccount::new("something"))
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
}
} }

58
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 <https://www.gnu.org/licenses/>.
*/
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(())
}
}

23
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 <https://www.gnu.org/licenses/>.
*/
pub trait Command {
fn execute(&self) -> Result<(), ()>;
}
pub mod addaccount;

47
src/main.rs

@ -18,10 +18,8 @@
use std::io; use std::io;
use elefren::helpers::cli;
use elefren::prelude::*;
mod args; mod args;
mod commands;
mod config; mod config;
mod filesystem; mod filesystem;
mod storage; mod storage;
@ -29,28 +27,12 @@ mod storage;
fn main() { fn main() {
env_logger::init(); env_logger::init();
match args::parse() { let command = args::parse();
args::Command::FetchAll => fetch_all_favourites(), command.execute();
args::Command::Fetch(account_name) => fetch_favourites(&account_name), // match args::parse() {
args::Command::CreateAccount(account_name) => { // Ok(command) => command.execute(),
add_account(&account_name) // Err(()) => println!("error"),
} // }
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();
} }
/// Remove account /// Remove account
@ -136,18 +118,3 @@ fn fetch_favourites(_account: &str) {
// config.save(&new_favourite); // 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
}

Loading…
Cancel
Save