diff --git a/README.md b/README.md index 25db1ecb..488ac197 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Selectable menus will be available when using `kubie ctx` and `kubie ns`. * `kubie update` will check the latest kubie version and update your local installation if needed ## Settings -You can customize kubie's behavior with the `~/.kube/kubie.yaml` file. The settings available and their defaults are +You can customize kubie's behavior with the `~/.config/kubie/config.yaml` file. The settings available and their defaults are available below. ```yaml @@ -165,7 +165,7 @@ behavior: # Optional start and stop hooks hooks: - # A command hook to run when a CTX is started. + # A command hook to run when a CTX is started. # This example re-labels your terminal window # Default: none start_ctx: > diff --git a/src/settings.rs b/src/settings.rs index 4e2b1813..11f1f93d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::fs::File; +use std::fs::{self, File}; use std::io::BufReader; use std::path::{Path, PathBuf}; @@ -14,6 +14,11 @@ lazy_static! { .to_str() .expect("home directory contains non unicode characters") .to_string(); + static ref CONFIG_DIR: String = dirs::config_dir() + .expect("could not get config_dir") + .to_str() + .expect("config_dir contains non unicode characters") + .to_string(); } #[inline] @@ -21,6 +26,11 @@ fn home_dir() -> &'static str { &HOME_DIR } +#[inline] +fn config_dir() -> &'static str { + &CONFIG_DIR +} + pub fn expanduser(path: &str) -> String { if let Some(stripped) = path.strip_prefix("~/") { format!("{}/{}", home_dir(), stripped) @@ -45,7 +55,18 @@ pub struct Settings { impl Settings { pub fn path() -> String { - format!("{}/.kube/kubie.yaml", home_dir()) + let legacy_path = format!("{}/.kube/kubie.yaml", home_dir()); + let xdg_dir = format!("{}/kubie", config_dir()); + let xdg_path = format!("{}/config.yaml", xdg_dir); + + if Path::new(&legacy_path).exists() { + legacy_path + } else if !Path::new(&xdg_dir).is_dir() { + fs::create_dir_all(xdg_dir).expect("could not create config directory"); + xdg_path + } else { + xdg_path + } } pub fn load() -> Result { @@ -60,7 +81,8 @@ impl Settings { Settings::default() }; - // Very important to exclude kubie's own config file ~/.kube/kubie.yaml from the results. + // Very important to exclude kubie's own config file from the results + // it is ~/.config/kubie/config.yaml by default settings.configs.exclude.push(settings_path_str); Ok(settings) }