Skip to content

Commit

Permalink
fix filepath name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Oct 19, 2024
1 parent b550b11 commit 10b7e32
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
10 changes: 7 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ pub fn selectable_list(input: Vec<String>) -> Option<String> {
// 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<String, SetContextError> {
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(),
Expand Down
15 changes: 10 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), ":", "_");
Expand All @@ -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
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions src/modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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> {
Expand Down

0 comments on commit 10b7e32

Please sign in to comment.