refactor: Refactored the library for gman so that it dynamically names config and password files to be used across any application

This commit is contained in:
2025-10-14 17:12:43 -06:00
parent 5fa4dbfe89
commit 6daa6fd2f2
8 changed files with 66 additions and 44 deletions
+10 -6
View File
@@ -323,6 +323,7 @@ pub fn secrets_completer(current: &OsStr) -> Vec<CompletionCandidate> {
mod tests { mod tests {
use super::*; use super::*;
use crate::cli::generate_files_secret_injections; use crate::cli::generate_files_secret_injections;
use gman::config::get_config_file_path;
use gman::config::{Config, RunConfig}; use gman::config::{Config, RunConfig};
use pretty_assertions::{assert_eq, assert_str_eq}; use pretty_assertions::{assert_eq, assert_str_eq};
use serial_test::serial; use serial_test::serial;
@@ -436,9 +437,10 @@ mod tests {
fn test_run_config_completer_filters_by_prefix() { fn test_run_config_completer_filters_by_prefix() {
let td = tempdir().unwrap(); let td = tempdir().unwrap();
let xdg = td.path().join("xdg"); let xdg = td.path().join("xdg");
let app_dir = xdg.join("gman");
fs::create_dir_all(&app_dir).unwrap();
unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) }; unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) };
let cfg_path = get_config_file_path().unwrap();
let app_dir = cfg_path.parent().unwrap().to_path_buf();
fs::create_dir_all(&app_dir).unwrap();
let yaml = indoc::indoc! { let yaml = indoc::indoc! {
"--- "---
@@ -471,9 +473,10 @@ mod tests {
fn test_provider_completer_lists_matching_providers() { fn test_provider_completer_lists_matching_providers() {
let td = tempdir().unwrap(); let td = tempdir().unwrap();
let xdg = td.path().join("xdg"); let xdg = td.path().join("xdg");
let app_dir = xdg.join("gman");
fs::create_dir_all(&app_dir).unwrap();
unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) }; unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) };
let cfg_path = get_config_file_path().unwrap();
let app_dir = cfg_path.parent().unwrap().to_path_buf();
fs::create_dir_all(&app_dir).unwrap();
let yaml = indoc::indoc! { let yaml = indoc::indoc! {
"--- "---
@@ -508,9 +511,10 @@ mod tests {
async fn test_secrets_completer_filters_keys_by_prefix() { async fn test_secrets_completer_filters_keys_by_prefix() {
let td = tempdir().unwrap(); let td = tempdir().unwrap();
let xdg = td.path().join("xdg"); let xdg = td.path().join("xdg");
let app_dir = xdg.join("gman");
fs::create_dir_all(&app_dir).unwrap();
unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) }; unsafe { std_env::set_var("XDG_CONFIG_HOME", &xdg) };
let cfg_path = get_config_file_path().unwrap();
let app_dir = cfg_path.parent().unwrap().to_path_buf();
fs::create_dir_all(&app_dir).unwrap();
let yaml = indoc::indoc! { let yaml = indoc::indoc! {
"--- "---
+2 -2
View File
@@ -46,7 +46,7 @@ pub fn init_logging_config() -> log4rs::Config {
pub fn get_log_path() -> PathBuf { pub fn get_log_path() -> PathBuf {
let base_dir = dirs::cache_dir().unwrap_or_else(env::temp_dir); let base_dir = dirs::cache_dir().unwrap_or_else(env::temp_dir);
let log_dir = base_dir.join("gman"); let log_dir = base_dir.join(env!("CARGO_CRATE_NAME"));
let dir = if let Err(e) = fs::create_dir_all(&log_dir) { let dir = if let Err(e) = fs::create_dir_all(&log_dir) {
eprintln!( eprintln!(
@@ -77,7 +77,7 @@ pub fn persist_config_file(config: &Config) -> Result<()> {
fs::write(&config_path, s) fs::write(&config_path, s)
.with_context(|| format!("failed to write {}", config_path.display()))?; .with_context(|| format!("failed to write {}", config_path.display()))?;
} else { } else {
confy::store("gman", "config", config) confy::store(env!("CARGO_CRATE_NAME"), "config", config)
.with_context(|| "failed to save updated config via confy")?; .with_context(|| "failed to save updated config via confy")?;
} }
+16 -15
View File
@@ -21,6 +21,7 @@
//! rc.validate().unwrap(); //! rc.validate().unwrap();
//! ``` //! ```
use crate::calling_app_name;
use crate::providers::local::LocalProvider; use crate::providers::local::LocalProvider;
use crate::providers::{SecretProvider, SupportedProvider}; use crate::providers::{SecretProvider, SupportedProvider};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@@ -268,21 +269,18 @@ impl Config {
/// Discover the default password file for the local provider. /// Discover the default password file for the local provider.
/// ///
/// On most systems this resolves to `~/.gman_password` when the file /// On most systems this resolves to `~/.<executable_name>_password`
/// exists, otherwise `None`. pub fn local_provider_password_file() -> PathBuf {
pub fn local_provider_password_file() -> Option<PathBuf> { dirs::home_dir()
let candidate = dirs::home_dir().map(|p| p.join(".gman_password")); .map(|p| p.join(format!(".{}_password", calling_app_name())))
match candidate { .expect("unable to determine home directory for local provider password file")
Some(p) if p.exists() => Some(p),
_ => None,
}
} }
} }
/// Load and validate the application configuration. /// Load and validate the application configuration.
/// ///
/// This uses the `confy` crate to load the configuration from a file /// This uses the `confy` crate to load the configuration from a file
/// (e.g. `~/.config/gman/config.yaml`). If the file does /// (e.g. `~/.config/<executable_name>/config.yaml`). If the file does
/// not exist, a default configuration is created and saved. /// not exist, a default configuration is created and saved.
/// ///
/// ```no_run /// ```no_run
@@ -295,7 +293,7 @@ pub fn load_config(interpolate: bool) -> Result<Config> {
let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from); let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from);
let mut config: Config = if let Some(base) = xdg_path.as_ref() { let mut config: Config = if let Some(base) = xdg_path.as_ref() {
let app_dir = base.join("gman"); let app_dir = base.join(calling_app_name());
let yml = app_dir.join("config.yml"); let yml = app_dir.join("config.yml");
let yaml = app_dir.join("config.yaml"); let yaml = app_dir.join("config.yaml");
if yml.exists() || yaml.exists() { if yml.exists() || yaml.exists() {
@@ -327,9 +325,9 @@ pub fn load_config(interpolate: bool) -> Result<Config> {
ref mut provider_def, ref mut provider_def,
} = p.provider_type } = p.provider_type
&& provider_def.password_file.is_none() && provider_def.password_file.is_none()
&& let Some(local_password_file) = Config::local_provider_password_file() && Config::local_provider_password_file().exists()
{ {
provider_def.password_file = Some(local_password_file); provider_def.password_file = Some(Config::local_provider_password_file());
} }
}); });
@@ -337,7 +335,7 @@ pub fn load_config(interpolate: bool) -> Result<Config> {
} }
fn load_confy_config(interpolate: bool) -> Result<Config> { fn load_confy_config(interpolate: bool) -> Result<Config> {
let load_path = confy::get_configuration_file_path("gman", "config")?; let load_path = confy::get_configuration_file_path(&calling_app_name(), "config")?;
let mut content = fs::read_to_string(&load_path) let mut content = fs::read_to_string(&load_path)
.with_context(|| format!("failed to read config file '{}'", load_path.display()))?; .with_context(|| format!("failed to read config file '{}'", load_path.display()))?;
if interpolate { if interpolate {
@@ -352,7 +350,7 @@ fn load_confy_config(interpolate: bool) -> Result<Config> {
/// Returns the configuration file path that `confy` will use /// Returns the configuration file path that `confy` will use
pub fn get_config_file_path() -> Result<PathBuf> { pub fn get_config_file_path() -> Result<PathBuf> {
if let Some(base) = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from) { if let Some(base) = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from) {
let dir = base.join("gman"); let dir = base.join(calling_app_name());
let yml = dir.join("config.yml"); let yml = dir.join("config.yml");
let yaml = dir.join("config.yaml"); let yaml = dir.join("config.yaml");
if yml.exists() || yaml.exists() { if yml.exists() || yaml.exists() {
@@ -360,7 +358,10 @@ pub fn get_config_file_path() -> Result<PathBuf> {
} }
return Ok(dir.join("config.yml")); return Ok(dir.join("config.yml"));
} }
Ok(confy::get_configuration_file_path("gman", "config")?) Ok(confy::get_configuration_file_path(
&calling_app_name(),
"config",
)?)
} }
pub fn interpolate_env_vars(s: &str) -> String { pub fn interpolate_env_vars(s: &str) -> String {
+10
View File
@@ -20,6 +20,7 @@
//! The `config` and `providers` modules power the CLI. They can be embedded //! The `config` and `providers` modules power the CLI. They can be embedded
//! in other programs, but many functions interact with the user or the //! in other programs, but many functions interact with the user or the
//! filesystem. Prefer `no_run` doctests for those. //! filesystem. Prefer `no_run` doctests for those.
use anyhow::{Context, Result, anyhow, bail}; use anyhow::{Context, Result, anyhow, bail};
use argon2::{ use argon2::{
Algorithm, Argon2, Params, Version, Algorithm, Argon2, Params, Version,
@@ -31,6 +32,7 @@ use chacha20poly1305::{
aead::{Aead, KeyInit, OsRng}, aead::{Aead, KeyInit, OsRng},
}; };
use secrecy::{ExposeSecret, SecretString}; use secrecy::{ExposeSecret, SecretString};
use std::path::PathBuf;
use zeroize::Zeroize; use zeroize::Zeroize;
/// Configuration structures and helpers used by the CLI and library. /// Configuration structures and helpers used by the CLI and library.
pub mod config; pub mod config;
@@ -207,6 +209,14 @@ pub fn decrypt_string(password: impl Into<SecretString>, envelope: &str) -> Resu
Ok(s) Ok(s)
} }
pub(crate) fn calling_app_name() -> String {
let exe: PathBuf = std::env::current_exe().expect("unable to get current exe path");
exe.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_owned())
.expect("executable name not valid UTF-8")
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+3 -2
View File
@@ -1,3 +1,4 @@
use crate::calling_app_name;
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result, anyhow};
use chrono::Utc; use chrono::Utc;
use dialoguer::Confirm; use dialoguer::Confirm;
@@ -25,7 +26,7 @@ pub fn sync_and_push(opts: &SyncOpts<'_>) -> Result<()> {
opts.validate() opts.validate()
.with_context(|| "invalid git sync options")?; .with_context(|| "invalid git sync options")?;
let commit_message = format!("chore: sync @ {}", Utc::now().to_rfc3339()); let commit_message = format!("chore: sync @ {}", Utc::now().to_rfc3339());
let config_dir = confy::get_configuration_file_path("gman", "vault") let config_dir = confy::get_configuration_file_path(&calling_app_name(), "vault")
.with_context(|| "get config dir")? .with_context(|| "get config dir")?
.parent() .parent()
.map(Path::to_path_buf) .map(Path::to_path_buf)
@@ -37,7 +38,7 @@ pub fn sync_and_push(opts: &SyncOpts<'_>) -> Result<()> {
fs::create_dir_all(&repo_dir).with_context(|| format!("create {}", repo_dir.display()))?; fs::create_dir_all(&repo_dir).with_context(|| format!("create {}", repo_dir.display()))?;
// Move the default vault into the repo dir on first sync so only vault.yml is tracked. // Move the default vault into the repo dir on first sync so only vault.yml is tracked.
let default_vault = confy::get_configuration_file_path("gman", "vault") let default_vault = confy::get_configuration_file_path(&calling_app_name(), "vault")
.with_context(|| "get default vault path")?; .with_context(|| "get default vault path")?;
let repo_vault = repo_dir.join("vault.yml"); let repo_vault = repo_dir.join("vault.yml");
if default_vault.exists() && !repo_vault.exists() { if default_vault.exists() && !repo_vault.exists() {
+12 -5
View File
@@ -13,6 +13,7 @@ use crate::providers::git_sync::{
use crate::providers::{SecretProvider, SupportedProvider}; use crate::providers::{SecretProvider, SupportedProvider};
use crate::{ use crate::{
ARGON_M_COST_KIB, ARGON_P, ARGON_T_COST, HEADER, KDF, KEY_LEN, NONCE_LEN, SALT_LEN, VERSION, ARGON_M_COST_KIB, ARGON_P, ARGON_T_COST, HEADER, KDF, KEY_LEN, NONCE_LEN, SALT_LEN, VERSION,
calling_app_name,
}; };
use anyhow::Result; use anyhow::Result;
use argon2::{Algorithm, Argon2, Params, Version}; use argon2::{Algorithm, Argon2, Params, Version};
@@ -63,8 +64,13 @@ pub struct LocalProvider {
impl Default for LocalProvider { impl Default for LocalProvider {
fn default() -> Self { fn default() -> Self {
let password_file = match Config::local_provider_password_file() {
p if p.exists() => Some(p),
_ => None,
};
Self { Self {
password_file: Config::local_provider_password_file(), password_file,
git_branch: Some("main".into()), git_branch: Some("main".into()),
git_remote_url: None, git_remote_url: None,
git_user_name: None, git_user_name: None,
@@ -286,7 +292,7 @@ impl LocalProvider {
let s = serde_yaml::to_string(&cfg)?; let s = serde_yaml::to_string(&cfg)?;
fs::write(&path, s).with_context(|| format!("failed to write {}", path.display()))?; fs::write(&path, s).with_context(|| format!("failed to write {}", path.display()))?;
} else { } else {
confy::store("gman", "config", &cfg) confy::store(&calling_app_name(), "config", &cfg)
.with_context(|| "failed to save updated config via confy")?; .with_context(|| "failed to save updated config via confy")?;
} }
@@ -335,10 +341,11 @@ fn default_vault_path() -> Result<PathBuf> {
let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from); let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from);
if let Some(xdg) = xdg_path { if let Some(xdg) = xdg_path {
return Ok(xdg.join("gman").join("vault.yml")); return Ok(xdg.join(calling_app_name()).join("vault.yml"));
} }
confy::get_configuration_file_path("gman", "vault").with_context(|| "get config dir") confy::get_configuration_file_path(&calling_app_name(), "vault")
.with_context(|| "get config dir")
} }
fn base_config_dir() -> Result<PathBuf> { fn base_config_dir() -> Result<PathBuf> {
@@ -560,7 +567,7 @@ mod tests {
fn persist_only_target_local_provider_git_settings() { fn persist_only_target_local_provider_git_settings() {
let td = tempdir().unwrap(); let td = tempdir().unwrap();
let xdg = td.path().join("xdg"); let xdg = td.path().join("xdg");
let app_dir = xdg.join("gman"); let app_dir = xdg.join(calling_app_name());
fs::create_dir_all(&app_dir).unwrap(); fs::create_dir_all(&app_dir).unwrap();
unsafe { unsafe {
std_env::set_var("XDG_CONFIG_HOME", &xdg); std_env::set_var("XDG_CONFIG_HOME", &xdg);
+8 -10
View File
@@ -252,16 +252,14 @@ mod tests {
#[test] #[test]
fn test_config_local_provider_password_file() { fn test_config_local_provider_password_file() {
let path = Config::local_provider_password_file(); let path = Config::local_provider_password_file();
let expected_path = dirs::home_dir().map(|p| p.join(".gman_password")); // Derive expected filename based on current test executable name
if let Some(p) = &expected_path { let exe = std::env::current_exe().expect("current_exe");
if !p.exists() { let stem = exe
assert_eq!(path, None); .file_stem()
} else { .and_then(|s| s.to_str())
assert_eq!(path, expected_path); .expect("utf-8 file stem");
} let expected = dirs::home_dir().map(|p| p.join(format!(".{}_password", stem)));
} else { assert_eq!(Some(path), expected);
assert_eq!(path, None);
}
} }
#[test] #[test]
+5 -4
View File
@@ -58,10 +58,11 @@ fn test_local_provider_invalid_email() {
#[test] #[test]
fn test_local_provider_default() { fn test_local_provider_default() {
let provider = LocalProvider::default(); let provider = LocalProvider::default();
assert_eq!( let expected_pw = {
provider.password_file, let p = Config::local_provider_password_file();
Config::local_provider_password_file() if p.exists() { Some(p) } else { None }
); };
assert_eq!(provider.password_file, expected_pw);
assert_eq!(provider.git_branch, Some("main".into())); assert_eq!(provider.git_branch, Some("main".into()));
assert_eq!(provider.git_remote_url, None); assert_eq!(provider.git_remote_url, None);
assert_eq!(provider.git_user_name, None); assert_eq!(provider.git_user_name, None);