refactor: migrate from clap v2 to clap v4

This commit is contained in:
VC
2022-11-14 20:34:03 +01:00
parent ffbe98f838
commit 89de1cf7a3
3 changed files with 73 additions and 86 deletions

60
Cargo.lock generated
View File

@@ -46,15 +46,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@@ -261,9 +252,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.22" version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
@@ -277,17 +268,24 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "4.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" checksum = "60494cedb60cb47462c0ff7be53de32c0e42a6fc2c772184554fa12bd9489c03"
dependencies = [ dependencies = [
"ansi_term",
"atty", "atty",
"bitflags", "bitflags",
"clap_lex",
"strsim", "strsim",
"textwrap", "termcolor",
"unicode-width", ]
"vec_map",
[[package]]
name = "clap_lex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
dependencies = [
"os_str_bytes",
] ]
[[package]] [[package]]
@@ -1567,6 +1565,12 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "os_str_bytes"
version = "6.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5bf27447411e9ee3ff51186bf7a08e16c341efdde93f4d823e8844429bed7e"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.9.0" version = "0.9.0"
@@ -2099,13 +2103,12 @@ dependencies = [
[[package]] [[package]]
name = "scootaloo" name = "scootaloo"
version = "0.8.2" version = "0.8.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",
"egg-mode", "egg-mode",
"elefren", "elefren",
"futures 0.3.25",
"html-escape", "html-escape",
"log", "log",
"mime", "mime",
@@ -2368,9 +2371,9 @@ dependencies = [
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "subtle" name = "subtle"
@@ -2430,15 +2433,6 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.37" version = "1.0.37"
@@ -2869,12 +2863,6 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "scootaloo" name = "scootaloo"
version = "0.8.2" version = "0.8.3"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021" edition = "2021"
@@ -10,8 +10,7 @@ edition = "2021"
chrono = "^0.4" chrono = "^0.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "^0.5" toml = "^0.5"
clap = "^2.34" clap = "^4"
futures = "^0.3"
egg-mode = "^0.16" egg-mode = "^0.16"
rusqlite = "^0.27" rusqlite = "^0.27"
tokio = { version = "1", features = ["full"]} tokio = { version = "1", features = ["full"]}

View File

@@ -1,5 +1,5 @@
use clap::{App, Arg, SubCommand}; use clap::{Arg, Command};
use log::{error, LevelFilter}; use log::LevelFilter;
use scootaloo::*; use scootaloo::*;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use std::str::FromStr; use std::str::FromStr;
@@ -7,115 +7,121 @@ use std::str::FromStr;
const DEFAULT_CONFIG_PATH: &str = "/usr/local/etc/scootaloo.toml"; const DEFAULT_CONFIG_PATH: &str = "/usr/local/etc/scootaloo.toml";
fn main() { fn main() {
let matches = App::new(env!("CARGO_PKG_NAME")) let matches = Command::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("A Twitter to Mastodon bot") .about("A Twitter to Mastodon bot")
.arg( .arg(
Arg::with_name("config") Arg::new("config")
.short("c") .short('c')
.long("config") .long("config")
.value_name("CONFIG_FILE") .value_name("CONFIG_FILE")
.help(&format!( .help(&format!(
"TOML config file for scootaloo (default {})", "TOML config file for scootaloo (default {})",
DEFAULT_CONFIG_PATH DEFAULT_CONFIG_PATH
)) ))
.takes_value(true) .num_args(1)
.default_value(DEFAULT_CONFIG_PATH)
.display_order(1), .display_order(1),
) )
.arg( .arg(
Arg::with_name("log_level") Arg::new("log_level")
.short("l") .short('l')
.long("loglevel") .long("loglevel")
.value_name("LOGLEVEL") .value_name("LOGLEVEL")
.help("Log level.Valid values are: Off, Warn, Error, Info, Debug") .help("Log level.Valid values are: Off, Warn, Error, Info, Debug")
.takes_value(true) .num_args(1)
.value_parser(["Off", "Warn", "Error", "Info", "Debug"])
.display_order(2), .display_order(2),
) )
.subcommand( .subcommand(
SubCommand::with_name("register") Command::new("register")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("Command to register to a Mastodon Instance") .about("Command to register to a Mastodon Instance")
.arg( .arg(
Arg::with_name("host") Arg::new("host")
.short("H") .short('H')
.long("host") .long("host")
.value_name("HOST") .value_name("HOST")
.help("Base URL of the Mastodon instance to register to (no default)") .help("Base URL of the Mastodon instance to register to (no default)")
.takes_value(true) .num_args(1)
.required(true) .required(true)
.display_order(1) .display_order(1)
) )
.arg( .arg(
Arg::with_name("name") Arg::new("name")
.short("n") .short('n')
.long("name") .long("name")
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)") .help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
.takes_value(true) .num_args(1)
.required(true) .required(true)
.display_order(2) .display_order(2)
), ),
) )
.subcommand( .subcommand(
SubCommand::with_name("init") Command::new("init")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("Command to init Scootaloo DB") .about("Command to init Scootaloo DB")
.arg( .arg(
Arg::with_name("config") Arg::new("config")
.short("c") .short('c')
.long("config") .long("config")
.value_name("CONFIG_FILE") .value_name("CONFIG_FILE")
.help(&format!( .help(&format!(
"TOML config file for scootaloo (default {})", "TOML config file for scootaloo (default {})",
DEFAULT_CONFIG_PATH DEFAULT_CONFIG_PATH
)) ))
.takes_value(true) .default_value(DEFAULT_CONFIG_PATH)
.num_args(1)
.display_order(1), .display_order(1),
), ),
) )
.subcommand( .subcommand(
SubCommand::with_name("migrate") Command::new("migrate")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("Command to migrate Scootaloo DB") .about("Command to migrate Scootaloo DB")
.arg( .arg(
Arg::with_name("config") Arg::new("config")
.short("c") .short('c')
.long("config") .long("config")
.value_name("CONFIG_FILE") .value_name("CONFIG_FILE")
.help(&format!("TOML config file for scootaloo (default {})", DEFAULT_CONFIG_PATH)) .help(&format!("TOML config file for scootaloo (default {})", DEFAULT_CONFIG_PATH))
.takes_value(true) .default_value(DEFAULT_CONFIG_PATH)
.num_args(1)
.display_order(1), .display_order(1),
) )
.arg( .arg(
Arg::with_name("name") Arg::new("name")
.short("n") .short('n')
.long("name") .long("name")
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)") .help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
.takes_value(true) .num_args(1)
.display_order(2) .display_order(2)
) )
) )
.get_matches(); .get_matches();
match matches.subcommand() { match matches.subcommand() {
("register", Some(sub_m)) => { Some(("register", sub_m)) => {
register( register(
sub_m.value_of("host").unwrap(), sub_m.get_one::<String>("host").unwrap(),
sub_m.value_of("name").unwrap(), sub_m.get_one::<String>("name").unwrap(),
); );
return; return;
} }
("init", Some(sub_m)) => { Some(("init", sub_m)) => {
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH)); let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
init_db(&config.scootaloo.db_path).unwrap(); init_db(&config.scootaloo.db_path).unwrap();
return; return;
} }
("migrate", Some(sub_m)) => { Some(("migrate", sub_m)) => {
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH)); let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
let config_twitter_screen_name = let config_twitter_screen_name =
&config.mastodon.values().next().unwrap().twitter_screen_name; &config.mastodon.values().next().unwrap().twitter_screen_name;
migrate_db( migrate_db(
&config.scootaloo.db_path, &config.scootaloo.db_path,
sub_m.value_of("name").unwrap_or(config_twitter_screen_name), sub_m
.get_one::<String>("name")
.unwrap_or(config_twitter_screen_name),
) )
.unwrap(); .unwrap();
return; return;
@@ -123,20 +129,14 @@ fn main() {
_ => (), _ => (),
} }
if matches.is_present("log_level") { if let Some(level) = matches.get_one::<String>("log_level") {
match LevelFilter::from_str(matches.value_of("log_level").unwrap()) {
Ok(level) => SimpleLogger::new().with_level(level).init().unwrap(),
Err(e) => {
SimpleLogger::new() SimpleLogger::new()
.with_level(LevelFilter::Error) .with_level(LevelFilter::from_str(level).unwrap())
.init() .init()
.unwrap(); .unwrap();
error!("Unknown log level filter: {}", e);
}
};
} }
let config = parse_toml(matches.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH)); let config = parse_toml(matches.get_one::<String>("config").unwrap());
run(config); run(config);
} }