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

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