Skip to content

Commit

Permalink
include files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Oct 20, 2024
1 parent fa607de commit 238e9d2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
13 changes: 0 additions & 13 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ pub fn get_namespaces() -> Vec<String> {
string.lines().skip(1).map(ToOwned::to_owned).collect()
}

pub fn get_current_context() -> String {
let output = Command::new("kubectl")
.args(["config", "current-context"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();

String::from_utf8(output.stdout).unwrap().trim().to_owned()
}

/// Prompts the user to select an item from a list.
/// Returns the selected item or `None` if no item was selected
pub fn selectable_list(input: Vec<String>) -> Option<String> {
Expand Down
47 changes: 25 additions & 22 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ use kube::config::NamedContext;
use std::fs::{self, File};
use std::io::{BufReader, BufWriter, Read};
use std::path::Path;
use std::path::PathBuf;

pub fn get() -> Kubeconfig {
let mut merged_config = Kubeconfig::default();

let kube_dir = dirs::home_dir()
.map(|home| home.join(".kube"))
.expect("Failed to find home directory");

if let Ok(entries) = fs::read_dir(&kube_dir) {
for entry in entries.flatten() {
let path = entry.path();

if path.is_file() {
match Kubeconfig::read_from(&path) {
Ok(kubeconfig) => {
merged_config.contexts.extend(kubeconfig.contexts);
merged_config.clusters.extend(kubeconfig.clusters);
merged_config.auth_infos.extend(kubeconfig.auth_infos);
}
Err(err) => {
eprintln!("Failed to parse file {:?} as a Kubeconfig: {:?}", path, err);
}
}
let config_paths = KUBECONFIG.clone();
let config_map: Vec<(usize, String)> = config_paths
.split(':')
.enumerate()
.map(|(index, path)| (index, path.to_string()))
.collect();

let mut config = Kubeconfig::default();

for (_index, path_str) in config_map.iter() {
let path = PathBuf::from(path_str);
match Kubeconfig::read_from(&path) {
Ok(kubeconfig) => {
config.contexts.extend(kubeconfig.contexts);
config.clusters.extend(kubeconfig.clusters);
config.auth_infos.extend(kubeconfig.auth_infos);
}
Err(err) => {
eprintln!(
"Failed to load Kubeconfig from '{}': {}",
path.display(),
err
);
}
}
}

merged_config
config
}

pub fn build(
Expand Down
44 changes: 34 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ mod modes;

use crate::error::Error;
use clap::Parser;
use std::collections::HashSet;
use std::env;
use std::fs;
use std::io;
use std::process;

Expand All @@ -14,19 +16,41 @@ extern crate lazy_static;

lazy_static! {
static ref KUBECONFIG: String = {
match env::var("KUBECONFIG") {
Ok(val) => {
let mut paths: String = String::new();
for s in val.split_inclusive(':') {
if s.contains("/kubesess/cache") {
continue;
let mut paths_set: HashSet<String> = HashSet::new();

// Get KUBECONFIG environment variable and add unique paths, ignoring kubesess/cache
if let Ok(val) = env::var("KUBECONFIG") {
val.split(':')
.filter(|s| !s.contains("/kubesess/cache"))
.for_each(|s| {
paths_set.insert(s.to_string());
});
}

// Add all files under .kube directory
if let Some(home_dir) = dirs::home_dir() {
let kube_dir = home_dir.join(".kube");
if let Ok(entries) = fs::read_dir(&kube_dir) {
entries.filter_map(Result::ok).for_each(|entry| {
let path = entry.path();
if path.is_file() {
if let Some(path_str) = path.to_str() {
paths_set.insert(path_str.to_string());
}
}
paths.push_str(s);
}
paths
});
}
Err(_e) => format!("{}/.kube/config", dirs::home_dir().unwrap().display()),
}

// Collect all unique paths, sort them, and move .kube/config to the end if it exists
let mut paths_vec: Vec<_> = paths_set.into_iter().collect();
paths_vec.sort();
if let Some(pos) = paths_vec.iter().position(|p| p.ends_with(".kube/config")) {
let kube_config = paths_vec.remove(pos);
paths_vec.push(kube_config);
}

paths_vec.join(":")
};
static ref KUBESESSCONFIG: String = {
match env::var("KUBECONFIG") {
Expand Down

0 comments on commit 238e9d2

Please sign in to comment.