From 07597cc209dd76a8bfc4da4d4293e4cd2d268a26 Mon Sep 17 00:00:00 2001 From: Marvin Beckers Date: Wed, 15 May 2024 17:07:27 +0200 Subject: [PATCH] fix(import): correctly import from stdin Signed-off-by: Marvin Beckers --- src/cmd/import.rs | 44 ++++++++++++++++++++++---------------------- tests/import.rs | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/cmd/import.rs b/src/cmd/import.rs index 74c26bf..5b425a8 100644 --- a/src/cmd/import.rs +++ b/src/cmd/import.rs @@ -83,28 +83,7 @@ pub fn execute(config_dir: &Path, matches: &ArgMatches) -> Result<()> { Err(err) => bail!(err), }; - if kubeconfig_path.is_file() { - // run import logic. - let name = kubeconfig::import( - config_dir, - kubeconfig_path, - matches.get_one::("name"), - matches.get_flag("short"), - matches.get_one::("proxy-url"), - )?; - - metadata = metadata.set( - name, - metadata::ConfigMetadata { - labels: Some(labels::to_map(&labels)), - }, - ); - - if matches.get_flag("delete") { - fs::remove_file(kubeconfig_path)?; - log::debug!("deleted {}", kubeconfig_path.display()); - } - } else if kubeconfig_path.is_dir() { + if kubeconfig_path.is_dir() { let files = fs::read_dir(kubeconfig_path)?; for file in files { let entry = file?; @@ -133,6 +112,27 @@ pub fn execute(config_dir: &Path, matches: &ArgMatches) -> Result<()> { }, ); } + } else { + // run import logic. + let name = kubeconfig::import( + config_dir, + kubeconfig_path, + matches.get_one::("name"), + matches.get_flag("short"), + matches.get_one::("proxy-url"), + )?; + + metadata = metadata.set( + name, + metadata::ConfigMetadata { + labels: Some(labels::to_map(&labels)), + }, + ); + + if matches.get_flag("delete") { + fs::remove_file(kubeconfig_path)?; + log::debug!("deleted {}", kubeconfig_path.display()); + } } metadata.write(&metadata_path)?; diff --git a/tests/import.rs b/tests/import.rs index 3b01970..19616fb 100644 --- a/tests/import.rs +++ b/tests/import.rs @@ -1,3 +1,5 @@ +use std::fs; + use assert_cmd::Command; use predicates::str::{contains, is_match}; use tempfile::tempdir; @@ -242,3 +244,25 @@ fn test_kbs_import_directory() { .success() .stdout(is_match("^kubernetes.embik.me\nlocalhost\n$").unwrap()); } + +#[test] +fn test_kbs_import_from_stdin() { + let temp_dir = tempdir().unwrap(); + let base_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/files"); + + let buffer = fs::read(base_dir.join("test.kubeconfig")).unwrap(); + + Command::cargo_bin("kbs") + .unwrap() + .write_stdin(buffer) + .args(&["-c", temp_dir.path().to_str().unwrap(), "import", "-"]) + .assert() + .success(); + + Command::cargo_bin("kbs") + .unwrap() + .args(&["-c", temp_dir.path().to_str().unwrap(), "list"]) + .assert() + .success() + .stdout(is_match("^kubernetes.embik.me\n$").unwrap()); +}