Skip to content

Commit

Permalink
Switch from structopt to clap
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bernier St-Pierre committed Jul 22, 2022
1 parent db37d18 commit c5695b2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 106 deletions.
127 changes: 59 additions & 68 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
51 changes: 25 additions & 26 deletions src/cmd/meta.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

/// 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<String>,

/// 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.
Expand All @@ -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.
Expand All @@ -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<ContextHeaderBehavior>,
/// Command to run as well as its arguments.
args: Vec<String>,
},

/// 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<String>,
},

/// 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<String>,
},
}

#[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,
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use structopt::StructOpt;
use clap::Parser;

use cmd::meta::Kubie;
use settings::Settings;
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit c5695b2

Please sign in to comment.