From c5695b260ad7aaca120fb4e093fcf6bbd36bf0c6 Mon Sep 17 00:00:00 2001 From: Simon Bernier St-Pierre Date: Fri, 22 Jul 2022 19:32:01 -0400 Subject: [PATCH] Switch from structopt to clap --- Cargo.lock | 127 ++++++++++++++++++++++-------------------------- Cargo.toml | 2 +- src/cmd/meta.rs | 51 ++++++++++--------- src/main.rs | 4 +- src/settings.rs | 15 +++--- 5 files changed, 93 insertions(+), 106 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 279be638..3d14c544 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,15 +8,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.58" @@ -96,17 +87,41 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.34.0" +version = "3.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "54635806b078b7925d6e36810b1755f2a4b5b4d57560432c1ecf60bcbe10602b" dependencies = [ - "ansi_term", "atty", "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", "strsim", + "termcolor", "textwrap", - "unicode-width", - "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", ] [[package]] @@ -215,12 +230,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "heck" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "hermit-abi" @@ -295,6 +307,7 @@ dependencies = [ "attohttpc", "atty", "cfg-if 1.0.0", + "clap", "dirs", "fs2", "glob", @@ -304,7 +317,6 @@ dependencies = [ "serde_json", "serde_yaml", "signal-hook", - "structopt", "tempfile", "which", "wildmatch", @@ -358,6 +370,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +[[package]] +name = "os_str_bytes" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -548,33 +566,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[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" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" @@ -602,14 +596,20 @@ dependencies = [ ] [[package]] -name = "textwrap" -version = "0.11.0" +name = "termcolor" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" dependencies = [ - "unicode-width", + "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + [[package]] name = "thiserror" version = "1.0.31" @@ -666,18 +666,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" - -[[package]] -name = "unicode-width" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" - [[package]] name = "untrusted" version = "0.7.1" @@ -696,12 +684,6 @@ dependencies = [ "percent-encoding", ] -[[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.9.4" @@ -830,6 +812,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 907ae818..ccf5a071 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ version = "0.17.0" [dependencies] anyhow = "1" atty = "0.2" +clap = { version = "3.2.14", features = ["derive"] } cfg-if = "1" dirs = "2" fs2 = "0.4" @@ -26,7 +27,6 @@ serde = {version = "1", features = ["derive"]} serde_json = "1" serde_yaml = "0.8" signal-hook = "0.3" -structopt = "0.3" tempfile = "3" which = "4" wildmatch = "2" diff --git a/src/cmd/meta.rs b/src/cmd/meta.rs index 08f4241e..58bd739a 100644 --- a/src/cmd/meta.rs +++ b/src/cmd/meta.rs @@ -1,25 +1,24 @@ -use structopt::clap; -use structopt::StructOpt; +use clap::Parser; use crate::settings::ContextHeaderBehavior; -#[derive(Debug, StructOpt)] -#[structopt(global_setting(clap::AppSettings::VersionlessSubcommands))] +#[derive(Debug, Parser)] +#[clap(disable_version_flag = true)] pub enum Kubie { /// Spawn a shell in the given context. The shell is isolated from other shells. /// Kubie shells can be spawned recursively without any issue. - #[structopt(name = "ctx")] + #[clap(name = "ctx")] Context { /// Specify in which namespace of the context the shell is spawned. - #[structopt(short = "n", long = "namespace")] + #[clap(short = 'n', long = "namespace")] namespace_name: Option, /// Specify files from which to load contexts instead of using the installed ones. - #[structopt(short = "f", long = "kubeconfig")] + #[clap(short = 'f', long = "kubeconfig")] kubeconfigs: Vec, /// Enter the context by spawning a new recursive shell. - #[structopt(short = "r", long = "recursive")] + #[clap(short = 'r', long = "recursive")] recursive: bool, /// Name of the context to enter. Use '-' to switch back to the previous context. @@ -28,14 +27,14 @@ pub enum Kubie { /// Change the namespace in which the current shell operates. The namespace change does /// not affect other shells. - #[structopt(name = "ns")] + #[clap(name = "ns")] Namespace { /// Enter the namespace by spawning a new recursive shell. - #[structopt(short = "r", long = "recursive")] + #[clap(short = 'r', long = "recursive")] recursive: bool, /// Unsets the namespace in the currently active context. - #[structopt(short = "u", long = "unset")] + #[clap(short = 'u', long = "unset")] unset: bool, /// Name of the namespace to enter. Use '-' to switch back to the previous namespace. @@ -44,71 +43,71 @@ pub enum Kubie { /// View info about the current kubie shell, such as the context name and the /// current namespace. - #[structopt(name = "info")] + #[clap(name = "info")] Info(KubieInfo), /// Execute a command inside of the given context and namespace. - #[structopt(name = "exec", setting(clap::AppSettings::TrailingVarArg))] + #[clap(name = "exec", trailing_var_arg = true)] Exec { /// Name of the context in which to run the command. context_name: String, /// Namespace in which to run the command. This is mandatory to avoid potential errors. namespace_name: String, /// Exit early if a command fails when using a wildcard context. - #[structopt(short = "e", long = "exit-early")] + #[clap(short = 'e', long = "exit-early")] exit_early: bool, /// Overrides behavior.print_context_in_exec in Kubie settings file. - #[structopt(long = "context-headers", possible_values = &ContextHeaderBehavior::variants(), case_insensitive = true)] + #[clap(value_enum, long = "context-headers")] context_headers_flag: Option, /// Command to run as well as its arguments. args: Vec, }, /// Check the Kubernetes config files for issues. - #[structopt(name = "lint")] + #[clap(name = "lint")] Lint, /// Edit the given context. - #[structopt(name = "edit")] + #[clap(name = "edit")] Edit { /// Name of the context to edit. context_name: Option, }, /// Edit kubie's config file. - #[structopt(name = "edit-config")] + #[clap(name = "edit-config")] EditConfig, /// Check for a Kubie update and replace Kubie's binary if needed. /// This function can ask for sudo-mode. - #[structopt(name = "update")] + #[clap(name = "update")] Update, /// Delete a context. Automatic garbage collection will be performed. /// Dangling users and clusters will be removed. - #[structopt(name = "delete")] + #[clap(name = "delete")] Delete { /// Name of the context to edit. context_name: Option, }, } -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct KubieInfo { - #[structopt(subcommand)] + #[clap(subcommand)] pub kind: KubieInfoKind, } /// Type of info the user is requesting. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub enum KubieInfoKind { /// Get the current shell's context name. - #[structopt(name = "ctx")] + #[clap(name = "ctx")] Context, /// Get the current shell's namespace name. - #[structopt(name = "ns")] + #[clap(name = "ns")] Namespace, /// Get the current depth of contexts. - #[structopt(name = "depth")] + #[clap(name = "depth")] Depth, } diff --git a/src/main.rs b/src/main.rs index c7a17509..a4039c90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use structopt::StructOpt; +use clap::Parser; use cmd::meta::Kubie; use settings::Settings; @@ -17,7 +17,7 @@ mod vars; fn main() -> Result<()> { let settings = Settings::load()?; - let kubie = Kubie::from_args(); + let kubie = Kubie::parse(); match kubie { Kubie::Context { diff --git a/src/settings.rs b/src/settings.rs index a32ac71f..3144a330 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -7,7 +7,6 @@ use anyhow::{Context, Result}; use glob::glob; use lazy_static::lazy_static; use serde::Deserialize; -use structopt::clap::arg_enum; lazy_static! { static ref HOME_DIR: String = dirs::home_dir() @@ -155,14 +154,12 @@ impl Default for Prompt { } } -arg_enum! { - #[derive(Debug, Deserialize)] - #[serde(rename_all = "lowercase")] - pub enum ContextHeaderBehavior { - Auto, - Always, - Never, - } +#[derive(Debug, Clone, clap::ValueEnum, Deserialize)] +#[clap(rename_all = "lower")] +pub enum ContextHeaderBehavior { + Auto, + Always, + Never, } impl ContextHeaderBehavior {