From 2a2072e59e7f46e4cb926335b2458d10306b8dab Mon Sep 17 00:00:00 2001 From: Rami Daghlawi Date: Sun, 20 Oct 2024 22:01:34 +0200 Subject: [PATCH] set default ns solved --- src/commands.rs | 7 ++----- src/modes.rs | 32 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index eedde85..6985c2f 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -10,13 +10,10 @@ extern crate skim; use kube::config::Kubeconfig; use skim::prelude::*; -pub fn set_default_namespace(ns: &str, ctx: &str) { +pub fn set_default_namespace(ns: &str, ctx: &str, target: &PathBuf) { Command::new("kubectl") .arg("config") - .arg(format!( - "--kubeconfig={}/.kube/config", - dirs::home_dir().unwrap().display() - )) + .arg(format!("--kubeconfig={}", target.to_string_lossy())) .arg("set-context") .arg(ctx) .arg(format!("--namespace={}", ns)) diff --git a/src/modes.rs b/src/modes.rs index e10ad35..0af0ebf 100644 --- a/src/modes.rs +++ b/src/modes.rs @@ -1,8 +1,4 @@ -use crate::{ - commands, config, - error::{Error, SetContextError}, - Cli, DEST, KUBECONFIG, -}; +use crate::{commands, config, error::Error, Cli, DEST, KUBECONFIG}; pub fn default_context(args: Cli) -> Result<(), Error> { let config = config::get(None); @@ -142,16 +138,17 @@ pub fn namespace(args: Cli) -> Result<(), Error> { } pub fn default_namespace(args: Cli) -> Result<(), Error> { - let config = config::get_current_session(); - let ctx = &config + let current_session = config::get_current_session(); + let config = config::get(None); + let ctx = ¤t_session .current_context .as_deref() .unwrap_or("No current context set"); if args.current { - if let Some(ctx) = config.contexts.iter().find(|x| { + if let Some(ctx) = current_session.contexts.iter().find(|x| { x.name - == config + == current_session .current_context .as_deref() .unwrap_or("No current context set") @@ -179,8 +176,21 @@ pub fn default_namespace(args: Cli) -> Result<(), Error> { Some(x) => x.trim().to_string(), }; - commands::set_default_namespace(&ns, &ctx); - let result = commands::set_namespace(&ctx, &ns, &DEST, &config); + if let Some(target) = config + .configs + .iter() + .find(|(kubeconfig, _)| { + kubeconfig + .contexts + .iter() + .any(|context| context.name == *ctx) + }) + .map(|(_, path)| path.clone()) + { + commands::set_default_namespace(&ns, &ctx, &target); + } + + let result = commands::set_namespace(&ctx, &ns, &DEST, ¤t_session); println!( "{}/{}:{}", &DEST.as_str(),