Browse Source

Switching to a Command pattern

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

49
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<dyn Command> {
let parser = App::new(clap::crate_name!())
.version(clap::crate_version!())
.author(clap::crate_authors!())
@ -74,31 +59,7 @@ pub fn parse() -> Command {
.help("Storage type to be removed")
.takes_value(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"))
}

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

Loading…
Cancel
Save