From bdac2501e4d121a43199723f0cc16823ed4c6a89 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 9 Jun 2021 13:06:14 -0300 Subject: [PATCH] Added command line interface with clap --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 16 ++++++++------- src/args/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 10 ++++++++- 4 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 src/args/mod.rs diff --git a/Cargo.lock b/Cargo.lock index e87f534..9f8995c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "arrayvec" version = "0.4.11" @@ -171,6 +180,21 @@ dependencies = [ "time", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -303,9 +327,10 @@ checksum = "923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97" [[package]] name = "downfav" -version = "0.4.0" +version = "0.4.1" dependencies = [ "chrono", + "clap", "elefren", "env_logger", "html2md", @@ -1723,6 +1748,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "0.15.44" @@ -1807,6 +1838,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thread_local" version = "0.3.6" @@ -2011,6 +2051,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -2075,6 +2121,12 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33dd455d0f96e90a75803cfeb7f948768c08d70a6de9a8d2362461935698bf95" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.1.5" diff --git a/Cargo.toml b/Cargo.toml index 3faf911..67179d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,21 @@ [package] name = "downfav" -version = "0.4.0" +version = "0.4.1" authors = ["Julio Biason "] edition = "2018" +description = "Download Mastodon favourites" [dependencies] +chrono = "0.4" +clap = "2.33" elefren = { version = "0.20", features = ["toml"] } +env_logger = "0.8" html2md = "0.2" +html5ever = "0.25" +log = "0.4" +markup5ever = "0.10" +markup5ever_rcdom = "0.1" reqwest = "0.9" serde = "*" serde_derive = "*" toml = "0.5" -log = "0.4" -env_logger = "0.8" -chrono = "0.4" -html5ever = "0.25" -markup5ever_rcdom = "0.1" -markup5ever = "0.10" diff --git a/src/args/mod.rs b/src/args/mod.rs new file mode 100644 index 0000000..108b656 --- /dev/null +++ b/src/args/mod.rs @@ -0,0 +1,43 @@ +/* + 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 clap::App; +use clap::SubCommand; + +/// Possible commands +pub enum Command { + /// Got a command that we don't know + Unknown, + /// Fetch all new favourites + Fetch, +} + +/// Parse the command line, returning the necessary command. +pub fn parse() -> Command { + let parser = App::new(clap::crate_name!()) + .version(clap::crate_version!()) + .author(clap::crate_authors!()) + .about(clap::crate_description!()) + .subcommand(SubCommand::with_name("fetch").about("Fetch the new favourites")); + let matches = parser.get_matches(); + match matches.subcommand() { + ("", _) => Command::Fetch, + ("fetch", _) => Command::Fetch, + _ => Command::Unknown, + } +} diff --git a/src/main.rs b/src/main.rs index 0e03797..67c5a79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,12 +27,21 @@ use crate::storage::joplin::Joplin; use crate::storage::org::Org; use crate::storage::storage::Storage; +mod args; mod config; mod storage; fn main() { env_logger::init(); + match args::parse() { + args::Command::Fetch => fetch_favourites(), + _ => println!("Unknown command"), + } +} + +/// Retrieve favourites +fn fetch_favourites() { let config = match config::Config::get() { Ok(config) => config, Err(e) => { @@ -80,7 +89,6 @@ fn main() { config.save(&new_favourite); } } - /// Create a connection to a mastodon server. fn connect_to_mastodon() -> elefren::data::Data { println!("Your server URL: ");