diff --git a/src/commands.rs b/src/commands.rs index 0e989c4..79bcfa7 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -87,10 +87,14 @@ pub fn selectable_list(input: Vec) -> Option { // config::write(choice.unwrap(), Some(selection), temp_dir) // } -pub fn set_context(ctx: &str, temp_dir: &str, config: &Kubeconfig) -> Result<(), SetContextError> { +pub fn set_context( + ctx: &str, + temp_dir: &str, + config: &Kubeconfig, +) -> Result { if let Some(choice) = config.contexts.iter().find(|x| x.name == ctx) { - config::write(choice, None, temp_dir, config); - Ok(()) + let filename = config::write(choice, None, temp_dir, config); + Ok(filename) } else { Err(SetContextError::KubeContextNotFound { ctx: ctx.to_owned(), diff --git a/src/config.rs b/src/config.rs index 95db66f..edb6548 100644 --- a/src/config.rs +++ b/src/config.rs @@ -107,9 +107,7 @@ fn get_file(path: &String) -> File { f } -fn get_path(ctx: &str, namespace: &str, dest: &str) -> String { - let filename = ctx.to_owned() + "_" + &namespace.to_owned(); - +fn get_path(filename: &str, dest: &str) -> String { let path = Path::new(&filename); let parent = path.parent().unwrap(); let dirname = str::replace(&parent.display().to_string(), ":", "_"); @@ -122,7 +120,12 @@ fn get_path(ctx: &str, namespace: &str, dest: &str) -> String { path.display().to_string() } -pub fn write(ctx: &NamedContext, namespace: Option<&str>, dest: &str, config: &Kubeconfig) { +pub fn write( + ctx: &NamedContext, + namespace: Option<&str>, + dest: &str, + config: &Kubeconfig, +) -> String { let minimal_config = build(ctx, namespace, config); let selected_context = minimal_config.current_context.clone().unwrap_or_default(); let selected_ns = minimal_config @@ -131,12 +134,14 @@ pub fn write(ctx: &NamedContext, namespace: Option<&str>, dest: &str, config: &K .and_then(|ctx| ctx.context.as_ref().and_then(|c| c.namespace.clone())) .unwrap_or_else(|| "default".to_string()); - let path = get_path(&selected_context, &selected_ns, dest); + let filename = selected_context.to_owned() + "_" + &selected_ns.to_owned(); + let path = get_path(&filename, dest); let options = get_file(&path); let writer = BufWriter::new(&options); serde_yaml::to_writer(writer, &minimal_config).unwrap(); + return filename; } pub fn get_current_session() -> KubeConfig { diff --git a/src/modes.rs b/src/modes.rs index 6473560..e1b42c7 100644 --- a/src/modes.rs +++ b/src/modes.rs @@ -38,11 +38,11 @@ use crate::{ // } pub fn context(args: Cli) -> Result<(), Error> { - if args.current { - let config = config::get_current_session(); - println!("{}", config.current_context); - return Ok(()); - } + // if args.current { + // let config = config::get_current_session(); + // println!("{}", config.current_context); + // return Ok(config); + // } let config = config::get(); let ctx = match args.value { @@ -64,12 +64,12 @@ pub fn context(args: Cli) -> Result<(), Error> { println!( "{}/{}:{}", &DEST.as_str(), - str::replace(&ctx, ":", "_"), + str::replace(&set_context_result.unwrap(), ":", "_"), *KUBECONFIG ); } - set_context_result + return Ok(()); } // pub fn namespace(args: Cli) -> Result<(), Error> {