Skip to content

Commit

Permalink
Merge pull request #58 from embik/fix-stdin-import
Browse files Browse the repository at this point in the history
fix(import): correctly import from stdin
  • Loading branch information
embik authored May 15, 2024
2 parents 1c97761 + 07597cc commit 18e4a37
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/cmd/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("name"),
matches.get_flag("short"),
matches.get_one::<String>("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?;
Expand Down Expand Up @@ -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::<String>("name"),
matches.get_flag("short"),
matches.get_one::<String>("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)?;
Expand Down
24 changes: 24 additions & 0 deletions tests/import.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs;

use assert_cmd::Command;
use predicates::str::{contains, is_match};
use tempfile::tempdir;
Expand Down Expand Up @@ -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());
}

0 comments on commit 18e4a37

Please sign in to comment.