Skip to content

Commit

Permalink
set default ns solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Oct 20, 2024
1 parent cda6ea7 commit 2a2072e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 2 additions & 5 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
32 changes: 21 additions & 11 deletions src/modes.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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 = &current_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")
Expand Down Expand Up @@ -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, &current_session);
println!(
"{}/{}:{}",
&DEST.as_str(),
Expand Down

0 comments on commit 2a2072e

Please sign in to comment.