Skip to content

Commit

Permalink
feat(remove): add flag to remove the currently active kubeconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Marvin Beckers <mail@embik.me>
  • Loading branch information
embik committed Sep 27, 2024
1 parent cfc1301 commit 6023eb4
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/cmd/remove.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::kubeconfig;
use crate::metadata::{self, Metadata};
use crate::Error;
use crate::{config, kubeconfig};
use anyhow::Result;
use anyhow::{anyhow, bail};
use clap::{value_parser, Arg, ArgGroup, ArgMatches, Command};
use clap::{value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command};
use std::{fs, path::Path};

pub const NAME: &str = "remove";
Expand All @@ -23,8 +23,17 @@ pub fn command() -> Command {
.value_delimiter(',')
.value_parser(metadata::selectors::parse),
)
.arg(
Arg::new("active")
.help("Remove the currently active kubeconfig")
.long("active")
.required(false)
.action(ArgAction::SetTrue)
.value_parser(clap::value_parser!(bool)),

)
.group(ArgGroup::new("target")
.args(["kubeconfig", "selectors"])
.args(["kubeconfig", "selectors", "active"])
.required(true))
.arg_required_else_help(true)
}
Expand All @@ -45,16 +54,28 @@ pub fn execute(config_dir: &Path, matches: &ArgMatches) -> Result<()> {
kubeconfig::list(config_dir, &metadata, Some(selectors))?
}
false => {
let config = matches
.get_one::<String>("kubeconfig")
.ok_or_else(|| anyhow!("failed to get kubeconfig argument"))?;
if matches.contains_id("kubeconfig") {
let config = matches
.get_one::<String>("kubeconfig")
.ok_or_else(|| anyhow!("failed to get kubeconfig argument"))?;

vec![
(kubeconfig::ListEntry {
name: config.to_string(),
labels: None,
}),
]
vec![
(kubeconfig::ListEntry {
name: config.to_string(),
labels: None,
}),
]
} else if matches.contains_id("active") {
let current = config::get_last_active(config_dir)?;
vec![
(kubeconfig::ListEntry {
name: current,
labels: None,
}),
]
} else {
vec![]
}
}
};

Expand Down
69 changes: 69 additions & 0 deletions tests/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,72 @@ fn test_kbs_remove_by_selector() {
.success()
.stdout(is_empty());
}

#[test]
fn test_kbs_remove_active() {
let temp_dir = tempdir().unwrap();
let base_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/files");

// initial import should succeed.
Command::cargo_bin("kbs")
.unwrap()
.args(&[
"-c",
temp_dir.path().to_str().unwrap(),
"import",
base_dir.join("test.kubeconfig").to_str().unwrap(),
])
.assert()
.success();

Command::cargo_bin("kbs")
.unwrap()
.args(&[
"-c",
temp_dir.path().to_str().unwrap(),
"import",
base_dir.join("localhost.kubeconfig").to_str().unwrap(),
])
.assert()
.success();

// both should be listed.
Command::cargo_bin("kbs")
.unwrap()
.args(&["-c", temp_dir.path().to_str().unwrap(), "list"])
.assert()
.success()
.stdout(is_match("^kubernetes.embik.me\nlocalhost\n$").unwrap());

// use one of them
Command::cargo_bin("kbs")
.unwrap()
.args(&[
"-c",
temp_dir.path().to_str().unwrap(),
"use",
"kubernetes.embik.me",
])
.assert()
.success();

// removing active kubeconfig should succeed.
Command::cargo_bin("kbs")
.unwrap()
.args(&[
"-c",
temp_dir.path().to_str().unwrap(),
"remove",
"--active",
])
.assert()
.success();

// only the other kubeconfig should be listed.
Command::cargo_bin("kbs")
.unwrap()
.args(&["-c", temp_dir.path().to_str().unwrap(), "list"])
.assert()
.success()
.stdout(is_match("^localhost\n$").unwrap());
}

0 comments on commit 6023eb4

Please sign in to comment.