Browse Source

Fixed the account management commands

master
Julio Biason 3 years ago
parent
commit
e7fe007d8c
  1. 24
      src/args/errors.rs
  2. 21
      src/args/mod.rs
  3. 21
      src/commands/addaccount.rs
  4. 30
      src/commands/errors.rs
  5. 10
      src/commands/mod.rs
  6. 40
      src/commands/removeaccount.rs
  7. 44
      src/main.rs

24
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 <https://www.gnu.org/licenses/>.
*/
/// Parsing errors
#[derive(Debug)]
pub enum ParsingError {
/// The command is not recognized
UnknownCommand,
}

21
src/args/mod.rs

@ -16,20 +16,22 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<dyn Command> {
pub fn parse() -> Result<Box<dyn commands::Command>, 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<dyn Command> {
.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),
}
}

21
src/commands/addaccount.rs

@ -16,15 +16,16 @@
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 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<elefren::Error> for CommandError {
fn from(_: elefren::Error) -> CommandError {
CommandError::ConnectError
}
}
impl From<ConfigError> for CommandError {
fn from(_: ConfigError) -> CommandError {
CommandError::ConfigError
}
}

30
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 <https://www.gnu.org/licenses/>.
*/
/// 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,
}

10
src/commands/mod.rs

@ -16,8 +16,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;

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

44
src/main.rs

@ -16,8 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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() {

Loading…
Cancel
Save