Skip to content

Commit

Permalink
default context
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Oct 19, 2024
1 parent 7434e9e commit f3f2ffe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
32 changes: 16 additions & 16 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let output = Command::new("kubectl")
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Cli {
enum Mode {
Namespace,
Context,
// DefaultContext,
DefaultContext,
DefaultNamespace,
CompletionContext,
CompletionNamespace,
Expand All @@ -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);
Expand Down
68 changes: 37 additions & 31 deletions src/modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> = 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 {
Expand Down

0 comments on commit f3f2ffe

Please sign in to comment.