From f3f2ffe8d0c3962dc78e85d7c88c837bb2e2d359 Mon Sep 17 00:00:00 2001 From: Rami Daghlawi Date: Sat, 19 Oct 2024 07:42:21 +0200 Subject: [PATCH] default context --- src/commands.rs | 32 +++++++++++------------ src/main.rs | 4 +-- src/modes.rs | 68 +++++++++++++++++++++++++++---------------------- 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index bbc2375..5abf4b7 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -25,22 +25,22 @@ pub fn set_default_namespace(ns: &str, ctx: &str) { .wait() .unwrap(); } -// -// pub fn set_default_context(ctx: &str) { -// Command::new("kubectl") -// .arg("config") -// .arg(format!( -// "--kubeconfig={}/.kube/config", -// dirs::home_dir().unwrap().display() -// )) -// .arg("use-context") -// .arg(ctx) -// .stdout(Stdio::null()) -// .spawn() -// .unwrap() -// .wait() -// .unwrap(); -// } + +pub fn set_default_context(ctx: &str) { + Command::new("kubectl") + .arg("config") + .arg(format!( + "--kubeconfig={}/.kube/config", + dirs::home_dir().unwrap().display() + )) + .arg("use-context") + .arg(ctx) + .stdout(Stdio::null()) + .spawn() + .unwrap() + .wait() + .unwrap(); +} pub fn get_namespaces() -> Vec { let output = Command::new("kubectl") diff --git a/src/main.rs b/src/main.rs index 6bbd4cf..671ea92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ pub struct Cli { enum Mode { Namespace, Context, - // DefaultContext, + DefaultContext, DefaultNamespace, CompletionContext, CompletionNamespace, @@ -75,7 +75,7 @@ impl Mode { match self { Mode::Namespace => modes::namespace(args), Mode::Context => modes::context(args), - // Mode::DefaultContext => modes::default_context(args), + Mode::DefaultContext => modes::default_context(args), Mode::DefaultNamespace => modes::default_namespace(args), Mode::CompletionContext => { modes::completion_context(args); diff --git a/src/modes.rs b/src/modes.rs index 982b7fc..aed085a 100644 --- a/src/modes.rs +++ b/src/modes.rs @@ -5,37 +5,43 @@ use crate::{ Cli, DEST, KUBECONFIG, }; -// pub fn default_context(args: Cli) -> Result<(), Error> { -// let config = config::get(); -// -// if args.current { -// println!("{}", config.current_context); -// return Ok(()); -// } -// -// let ctx = match args.value { -// None => { -// let options: Vec = config -// .contexts -// .iter() -// .map(|context| context.name.to_string()) -// .collect(); -// -// commands::selectable_list(options).ok_or(Error::NoItemSelected { prompt: "context" })? -// } -// Some(x) => x.trim().to_string(), -// }; -// -// commands::set_default_context(&ctx); -// -// let set_context_result = commands::set_context(&ctx, &DEST, &config).map_err(Error::SetContext); -// -// if set_context_result.is_ok() { -// println!("{}", KUBECONFIG.as_str()); -// } -// -// set_context_result -// } +pub fn default_context(args: Cli) -> Result<(), Error> { + let config = config::get(); + + if args.current { + println!( + "{}", + config + .current_context + .as_deref() + .unwrap_or("No current context set") + ); + return Ok(()); + } + + let ctx = match args.value { + None => { + let options: Vec = config + .contexts + .iter() + .map(|context| context.name.to_string()) + .collect(); + + commands::selectable_list(options).ok_or(Error::NoItemSelected { prompt: "context" })? + } + Some(x) => x.trim().to_string(), + }; + + commands::set_default_context(&ctx); + + let set_context_result = commands::set_context(&ctx, &DEST, &config).map_err(Error::SetContext); + + if set_context_result.is_ok() { + println!("{}", KUBECONFIG.as_str()); + } + + Ok(()) +} pub fn context(args: Cli) -> Result<(), Error> { if args.current {