Skip to content

Commit 54022fd

Browse files
authored
Merge pull request #64 from embik/remove-current
feat(remove): add flag to remove the currently active kubeconfig
2 parents cfc1301 + 6023eb4 commit 54022fd

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

src/cmd/remove.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::kubeconfig;
21
use crate::metadata::{self, Metadata};
32
use crate::Error;
3+
use crate::{config, kubeconfig};
44
use anyhow::Result;
55
use anyhow::{anyhow, bail};
6-
use clap::{value_parser, Arg, ArgGroup, ArgMatches, Command};
6+
use clap::{value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command};
77
use std::{fs, path::Path};
88

99
pub const NAME: &str = "remove";
@@ -23,8 +23,17 @@ pub fn command() -> Command {
2323
.value_delimiter(',')
2424
.value_parser(metadata::selectors::parse),
2525
)
26+
.arg(
27+
Arg::new("active")
28+
.help("Remove the currently active kubeconfig")
29+
.long("active")
30+
.required(false)
31+
.action(ArgAction::SetTrue)
32+
.value_parser(clap::value_parser!(bool)),
33+
34+
)
2635
.group(ArgGroup::new("target")
27-
.args(["kubeconfig", "selectors"])
36+
.args(["kubeconfig", "selectors", "active"])
2837
.required(true))
2938
.arg_required_else_help(true)
3039
}
@@ -45,16 +54,28 @@ pub fn execute(config_dir: &Path, matches: &ArgMatches) -> Result<()> {
4554
kubeconfig::list(config_dir, &metadata, Some(selectors))?
4655
}
4756
false => {
48-
let config = matches
49-
.get_one::<String>("kubeconfig")
50-
.ok_or_else(|| anyhow!("failed to get kubeconfig argument"))?;
57+
if matches.contains_id("kubeconfig") {
58+
let config = matches
59+
.get_one::<String>("kubeconfig")
60+
.ok_or_else(|| anyhow!("failed to get kubeconfig argument"))?;
5161

52-
vec![
53-
(kubeconfig::ListEntry {
54-
name: config.to_string(),
55-
labels: None,
56-
}),
57-
]
62+
vec![
63+
(kubeconfig::ListEntry {
64+
name: config.to_string(),
65+
labels: None,
66+
}),
67+
]
68+
} else if matches.contains_id("active") {
69+
let current = config::get_last_active(config_dir)?;
70+
vec![
71+
(kubeconfig::ListEntry {
72+
name: current,
73+
labels: None,
74+
}),
75+
]
76+
} else {
77+
vec![]
78+
}
5879
}
5980
};
6081

tests/remove.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,72 @@ fn test_kbs_remove_by_selector() {
141141
.success()
142142
.stdout(is_empty());
143143
}
144+
145+
#[test]
146+
fn test_kbs_remove_active() {
147+
let temp_dir = tempdir().unwrap();
148+
let base_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/files");
149+
150+
// initial import should succeed.
151+
Command::cargo_bin("kbs")
152+
.unwrap()
153+
.args(&[
154+
"-c",
155+
temp_dir.path().to_str().unwrap(),
156+
"import",
157+
base_dir.join("test.kubeconfig").to_str().unwrap(),
158+
])
159+
.assert()
160+
.success();
161+
162+
Command::cargo_bin("kbs")
163+
.unwrap()
164+
.args(&[
165+
"-c",
166+
temp_dir.path().to_str().unwrap(),
167+
"import",
168+
base_dir.join("localhost.kubeconfig").to_str().unwrap(),
169+
])
170+
.assert()
171+
.success();
172+
173+
// both should be listed.
174+
Command::cargo_bin("kbs")
175+
.unwrap()
176+
.args(&["-c", temp_dir.path().to_str().unwrap(), "list"])
177+
.assert()
178+
.success()
179+
.stdout(is_match("^kubernetes.embik.me\nlocalhost\n$").unwrap());
180+
181+
// use one of them
182+
Command::cargo_bin("kbs")
183+
.unwrap()
184+
.args(&[
185+
"-c",
186+
temp_dir.path().to_str().unwrap(),
187+
"use",
188+
"kubernetes.embik.me",
189+
])
190+
.assert()
191+
.success();
192+
193+
// removing active kubeconfig should succeed.
194+
Command::cargo_bin("kbs")
195+
.unwrap()
196+
.args(&[
197+
"-c",
198+
temp_dir.path().to_str().unwrap(),
199+
"remove",
200+
"--active",
201+
])
202+
.assert()
203+
.success();
204+
205+
// only the other kubeconfig should be listed.
206+
Command::cargo_bin("kbs")
207+
.unwrap()
208+
.args(&["-c", temp_dir.path().to_str().unwrap(), "list"])
209+
.assert()
210+
.success()
211+
.stdout(is_match("^localhost\n$").unwrap());
212+
}

0 commit comments

Comments
 (0)