Upgrade arg parsing to clap v3, take two (#900)

* Upgrade arg parsing to clap v3

* Bump Rust version in CI to 1.57.0

* Make clippy happy

Co-authored-by: Roey Darwish Dror <roey.ghost@gmail.com>
This commit is contained in:
Marcin Puc
2022-04-23 12:35:06 +02:00
committed by GitHub
parent 8c7f8b0fbd
commit 020a0619b8
7 changed files with 81 additions and 101 deletions

View File

@@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.53.0 toolchain: 1.57.0
profile: minimal profile: minimal
override: true override: true
components: rustfmt, clippy components: rustfmt, clippy

View File

@@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.53.0 toolchain: 1.57.0
profile: minimal profile: minimal
default: true default: true
override: true override: true

View File

@@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.53.0 toolchain: 1.57.0
profile: minimal profile: minimal
override: true override: true
components: rustfmt, clippy components: rustfmt, clippy

102
Cargo.lock generated
View File

@@ -23,15 +23,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.56" version = "1.0.56"
@@ -216,17 +207,41 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "3.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" checksum = "6aad2534fad53df1cc12519c5cda696dd3e20e6118a027e24054aea14a0bdcbe"
dependencies = [ dependencies = [
"ansi_term",
"atty", "atty",
"bitflags", "bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
"strsim", "strsim",
"termcolor",
"textwrap", "textwrap",
"unicode-width", ]
"vec_map",
[[package]]
name = "clap_derive"
version = "3.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "189ddd3b5d32a70b35e7686054371742a937b0d99128e76dde6340210e966669"
dependencies = [
"os_str_bytes",
] ]
[[package]] [[package]]
@@ -634,6 +649,12 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@@ -1081,6 +1102,12 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
] ]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
[[package]] [[package]]
name = "parking" name = "parking"
version = "2.0.0" version = "2.0.0"
@@ -1555,33 +1582,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[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]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "strum" name = "strum"
@@ -1607,7 +1610,7 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "339f799d8b549e3744c7ac7feb216383e4005d94bdb22561b3ab8f3b808ae9fb" checksum = "339f799d8b549e3744c7ac7feb216383e4005d94bdb22561b3ab8f3b808ae9fb"
dependencies = [ dependencies = [
"heck", "heck 0.3.3",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
@@ -1619,7 +1622,7 @@ version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bb0dc7ee9c15cea6199cde9a127fa16a4c5819af85395457ad72d68edc85a38" checksum = "5bb0dc7ee9c15cea6199cde9a127fa16a4c5819af85395457ad72d68edc85a38"
dependencies = [ dependencies = [
"heck", "heck 0.3.3",
"proc-macro2", "proc-macro2",
"quote", "quote",
"rustversion", "rustversion",
@@ -1693,12 +1696,9 @@ dependencies = [
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
@@ -1814,6 +1814,7 @@ dependencies = [
"anyhow", "anyhow",
"cfg-if", "cfg-if",
"chrono", "chrono",
"clap",
"console", "console",
"directories", "directories",
"futures", "futures",
@@ -1830,7 +1831,6 @@ dependencies = [
"self_update", "self_update",
"serde", "serde",
"shellexpand", "shellexpand",
"structopt",
"strum 0.23.0", "strum 0.23.0",
"sys-info", "sys-info",
"tempfile", "tempfile",
@@ -1931,12 +1931,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

@@ -17,7 +17,7 @@ serde = { version = "1.0.125", features = ["derive"] }
toml = "0.5.8" toml = "0.5.8"
which_crate = { version = "4.1.0", package = "which" } which_crate = { version = "4.1.0", package = "which" }
shellexpand = "2.1.0" shellexpand = "2.1.0"
structopt = "0.3.21" clap = { version = "3.1", features = ["cargo", "derive"] }
log = "0.4.14" log = "0.4.14"
walkdir = "2.3.2" walkdir = "2.3.2"
console = "0.15.0" console = "0.15.0"

View File

@@ -1,3 +1,4 @@
#![allow(dead_code)]
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fs::write; use std::fs::write;
use std::path::PathBuf; use std::path::PathBuf;
@@ -5,12 +6,12 @@ use std::process::Command;
use std::{env, fs}; use std::{env, fs};
use anyhow::Result; use anyhow::Result;
use clap::{ArgEnum, Parser};
use directories::BaseDirs; use directories::BaseDirs;
use log::debug; use log::debug;
use regex::Regex; use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use structopt::StructOpt; use strum::{EnumIter, EnumString, EnumVariantNames, IntoEnumIterator};
use strum::{EnumIter, EnumString, EnumVariantNames, IntoEnumIterator, VariantNames};
use sys_info::hostname; use sys_info::hostname;
use which_crate::which; use which_crate::which;
@@ -61,7 +62,8 @@ macro_rules! get_deprecated {
type Commands = BTreeMap<String, String>; type Commands = BTreeMap<String, String>;
#[derive(EnumString, EnumVariantNames, Debug, Clone, PartialEq, Deserialize, EnumIter, Copy)] #[derive(ArgEnum, EnumString, EnumVariantNames, Debug, Clone, PartialEq, Deserialize, EnumIter, Copy)]
#[clap(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")] #[strum(serialize_all = "snake_case")]
pub enum Step { pub enum Step {
@@ -358,68 +360,68 @@ impl ConfigFile {
} }
} }
#[derive(StructOpt, Debug)] // Command line arguments
#[structopt(name = "Topgrade", setting = structopt::clap::AppSettings::ColoredHelp)] #[derive(Parser, Debug)]
/// Command line arguments #[clap(name = "Topgrade", version)]
pub struct CommandLineArgs { pub struct CommandLineArgs {
/// Edit the configuration file /// Edit the configuration file
#[structopt(long = "edit-config")] #[clap(long = "edit-config")]
edit_config: bool, edit_config: bool,
/// Show config reference /// Show config reference
#[structopt(long = "config-reference")] #[clap(long = "config-reference")]
show_config_reference: bool, show_config_reference: bool,
/// Run inside tmux /// Run inside tmux
#[structopt(short = "t", long = "tmux")] #[clap(short = 't', long = "tmux")]
run_in_tmux: bool, run_in_tmux: bool,
/// Cleanup temporary or old files /// Cleanup temporary or old files
#[structopt(short = "c", long = "cleanup")] #[clap(short = 'c', long = "cleanup")]
cleanup: bool, cleanup: bool,
/// Print what would be done /// Print what would be done
#[structopt(short = "n", long = "dry-run")] #[clap(short = 'n', long = "dry-run")]
dry_run: bool, dry_run: bool,
/// Do not ask to retry failed steps /// Do not ask to retry failed steps
#[structopt(long = "no-retry")] #[clap(long = "no-retry")]
no_retry: bool, no_retry: bool,
/// Do not perform upgrades for the given steps /// Do not perform upgrades for the given steps
#[structopt(long = "disable", possible_values = &Step::VARIANTS)] #[clap(long = "disable", arg_enum)]
disable: Vec<Step>, disable: Vec<Step>,
/// Perform only the specified steps (experimental) /// Perform only the specified steps (experimental)
#[structopt(long = "only", possible_values = &Step::VARIANTS)] #[clap(long = "only", arg_enum)]
only: Vec<Step>, only: Vec<Step>,
/// Output logs /// Output logs
#[structopt(short = "v", long = "verbose")] #[clap(short = 'v', long = "verbose")]
pub verbose: bool, pub verbose: bool,
/// Prompt for a key before exiting /// Prompt for a key before exiting
#[structopt(short = "k", long = "keep")] #[clap(short = 'k', long = "keep")]
keep_at_end: bool, keep_at_end: bool,
/// Say yes to package manager's prompt /// Say yes to package manager's prompt
#[structopt(short = "y", long = "yes")] #[clap(short = 'y', long = "yes", arg_enum)]
yes: Option<Vec<Step>>, yes: Option<Vec<Step>>,
/// Don't pull the predefined git repos /// Don't pull the predefined git repos
#[structopt(long = "disable-predefined-git-repos")] #[clap(long = "disable-predefined-git-repos")]
disable_predefined_git_repos: bool, disable_predefined_git_repos: bool,
/// Alternative configuration file /// Alternative configuration file
#[structopt(long = "config")] #[clap(long = "config")]
config: Option<PathBuf>, config: Option<PathBuf>,
/// A regular expression for restricting remote host execution /// A regular expression for restricting remote host execution
#[structopt(long = "remote-host-limit", parse(try_from_str))] #[clap(long = "remote-host-limit")]
remote_host_limit: Option<Regex>, remote_host_limit: Option<Regex>,
/// Show the reason for skipped steps /// Show the reason for skipped steps
#[structopt(long = "show-skipped")] #[clap(long = "show-skipped")]
show_skipped: bool, show_skipped: bool,
} }
@@ -499,7 +501,7 @@ impl Config {
/// The list of additional git repositories to pull. /// The list of additional git repositories to pull.
pub fn git_repos(&self) -> &Option<Vec<String>> { pub fn git_repos(&self) -> &Option<Vec<String>> {
get_deprecated!(&self.config_file, git_repos, git, repos) get_deprecated!(self.config_file, git_repos, git, repos)
} }
/// Tell whether the specified step should run. /// Tell whether the specified step should run.
@@ -569,11 +571,11 @@ impl Config {
/// Extra Git arguments /// Extra Git arguments
pub fn git_arguments(&self) -> &Option<String> { pub fn git_arguments(&self) -> &Option<String> {
get_deprecated!(&self.config_file, git_arguments, git, arguments) get_deprecated!(self.config_file, git_arguments, git, arguments)
} }
/// Extra Tmux arguments /// Extra Tmux arguments
#[allow(dead_code)]
pub fn tmux_arguments(&self) -> &Option<String> { pub fn tmux_arguments(&self) -> &Option<String> {
&self.config_file.tmux_arguments &self.config_file.tmux_arguments
} }
@@ -589,7 +591,6 @@ impl Config {
} }
/// Whether to say yes to package managers /// Whether to say yes to package managers
#[allow(dead_code)]
pub fn yes(&self, step: Step) -> bool { pub fn yes(&self, step: Step) -> bool {
if let Some(yes) = self.config_file.assume_yes { if let Some(yes) = self.config_file.assume_yes {
return yes; return yes;
@@ -607,13 +608,11 @@ impl Config {
} }
/// Bash-it branch /// Bash-it branch
#[allow(dead_code)]
pub fn bashit_branch(&self) -> &str { pub fn bashit_branch(&self) -> &str {
self.config_file.bashit_branch.as_deref().unwrap_or("stable") self.config_file.bashit_branch.as_deref().unwrap_or("stable")
} }
/// Whether to accept all Windows updates /// Whether to accept all Windows updates
#[allow(dead_code)]
pub fn accept_all_windows_updates(&self) -> bool { pub fn accept_all_windows_updates(&self) -> bool {
get_deprecated!( get_deprecated!(
self.config_file, self.config_file,
@@ -625,7 +624,6 @@ impl Config {
} }
/// Whether to self rename the Topgrade executable during the run /// Whether to self rename the Topgrade executable during the run
#[allow(dead_code)]
pub fn self_rename(&self) -> bool { pub fn self_rename(&self) -> bool {
self.config_file self.config_file
.windows .windows
@@ -635,7 +633,6 @@ impl Config {
} }
/// Whether Brew cask should be greedy /// Whether Brew cask should be greedy
#[allow(dead_code)]
pub fn brew_cask_greedy(&self) -> bool { pub fn brew_cask_greedy(&self) -> bool {
self.config_file self.config_file
.brew .brew
@@ -663,13 +660,11 @@ impl Config {
} }
/// Whether to send a desktop notification at the beginning of every step /// Whether to send a desktop notification at the beginning of every step
#[allow(dead_code)]
pub fn notify_each_step(&self) -> bool { pub fn notify_each_step(&self) -> bool {
self.config_file.notify_each_step.unwrap_or(false) self.config_file.notify_each_step.unwrap_or(false)
} }
/// Extra trizen arguments /// Extra trizen arguments
#[allow(dead_code)]
pub fn trizen_arguments(&self) -> &str { pub fn trizen_arguments(&self) -> &str {
self.config_file self.config_file
.linux .linux
@@ -689,7 +684,6 @@ impl Config {
} }
/// Show news on Arch Linux /// Show news on Arch Linux
#[allow(dead_code)]
pub fn show_arch_news(&self) -> bool { pub fn show_arch_news(&self) -> bool {
self.config_file self.config_file
.linux .linux
@@ -699,7 +693,6 @@ impl Config {
} }
/// Extra yay arguments /// Extra yay arguments
#[allow(dead_code)]
pub fn arch_package_manager(&self) -> ArchPackageManager { pub fn arch_package_manager(&self) -> ArchPackageManager {
self.config_file self.config_file
.linux .linux
@@ -709,7 +702,6 @@ impl Config {
} }
/// Extra yay arguments /// Extra yay arguments
#[allow(dead_code)]
pub fn yay_arguments(&self) -> &str { pub fn yay_arguments(&self) -> &str {
get_deprecated!(self.config_file, yay_arguments, linux, yay_arguments) get_deprecated!(self.config_file, yay_arguments, linux, yay_arguments)
.as_deref() .as_deref()
@@ -717,7 +709,6 @@ impl Config {
} }
/// Extra apt arguments /// Extra apt arguments
#[allow(dead_code)]
pub fn apt_arguments(&self) -> Option<&str> { pub fn apt_arguments(&self) -> Option<&str> {
self.config_file self.config_file
.linux .linux
@@ -726,7 +717,6 @@ impl Config {
} }
/// Extra dnf arguments /// Extra dnf arguments
#[allow(dead_code)]
pub fn dnf_arguments(&self) -> Option<&str> { pub fn dnf_arguments(&self) -> Option<&str> {
self.config_file self.config_file
.linux .linux
@@ -761,7 +751,6 @@ impl Config {
} }
/// Enable tlmgr on Linux /// Enable tlmgr on Linux
#[allow(dead_code)]
pub fn enable_tlmgr_linux(&self) -> bool { pub fn enable_tlmgr_linux(&self) -> bool {
self.config_file self.config_file
.linux .linux
@@ -771,7 +760,6 @@ impl Config {
} }
/// Use distro-sync in Red Hat based distrbutions /// Use distro-sync in Red Hat based distrbutions
#[allow(dead_code)]
pub fn redhat_distro_sync(&self) -> bool { pub fn redhat_distro_sync(&self) -> bool {
self.config_file self.config_file
.linux .linux
@@ -781,7 +769,6 @@ impl Config {
} }
/// Use rpm-ostree in *when rpm-ostree is detected* (default: true) /// Use rpm-ostree in *when rpm-ostree is detected* (default: true)
#[allow(dead_code)]
pub fn rpm_ostree(&self) -> bool { pub fn rpm_ostree(&self) -> bool {
self.config_file self.config_file
.linux .linux
@@ -801,7 +788,7 @@ impl Config {
pub fn use_predefined_git_repos(&self) -> bool { pub fn use_predefined_git_repos(&self) -> bool {
!self.opt.disable_predefined_git_repos !self.opt.disable_predefined_git_repos
&& get_deprecated!(&self.config_file, predefined_git_repos, git, pull_predefined).unwrap_or(true) && get_deprecated!(self.config_file, predefined_git_repos, git, pull_predefined).unwrap_or(true)
} }
pub fn verbose(&self) -> bool { pub fn verbose(&self) -> bool {

View File

@@ -5,12 +5,11 @@ use std::io;
use std::process::exit; use std::process::exit;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use clap::{crate_version, Parser};
use console::Key; use console::Key;
use log::debug; use log::debug;
use log::LevelFilter; use log::LevelFilter;
use pretty_env_logger::formatted_timed_builder; use pretty_env_logger::formatted_timed_builder;
use structopt::clap::crate_version;
use structopt::StructOpt;
use self::config::{CommandLineArgs, Config, Step}; use self::config::{CommandLineArgs, Config, Step};
use self::error::StepFailed; use self::error::StepFailed;
@@ -39,7 +38,7 @@ fn run() -> Result<()> {
let base_dirs = directories::BaseDirs::new().ok_or_else(|| anyhow!("No base directories"))?; let base_dirs = directories::BaseDirs::new().ok_or_else(|| anyhow!("No base directories"))?;
let opt = CommandLineArgs::from_args(); let opt = CommandLineArgs::parse();
let mut builder = formatted_timed_builder(); let mut builder = formatted_timed_builder();
if opt.verbose { if opt.verbose {