From f46434ca442087278aec73e99b28e97be47502bd Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 12 Sep 2025 13:21:50 -0600 Subject: [PATCH] feat: Added two new flags to output where gman writes logs to and where it expects the config file to live --- README.md | 9 +++++++++ src/bin/gman/command.rs | 1 + src/bin/gman/main.rs | 25 ++++++++++++++++++++++--- src/bin/gman/utils.rs | 10 +++------- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d9cf9db..9e24986 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,15 @@ $HOME/Library/Application Support/rs.gman/config.yml %APPDATA%/Roaming/gman/config.yml ``` +### Discover paths (helpful for debugging) + +You can ask `gman` where it writes its log file and where it expects the config file to live: + +```shell +gman --show-log-path +gman --show-config-path +``` + ### Default Configuration `gman` supports multiple providers. Select one as the default and then list provider configurations. diff --git a/src/bin/gman/command.rs b/src/bin/gman/command.rs index 187cd2c..dd02689 100644 --- a/src/bin/gman/command.rs +++ b/src/bin/gman/command.rs @@ -1,3 +1,4 @@ +#[cfg(not(windows))] use std::borrow::Cow; use std::ffi::OsStr; use std::process::Command; diff --git a/src/bin/gman/main.rs b/src/bin/gman/main.rs index f8643d4..f37873f 100644 --- a/src/bin/gman/main.rs +++ b/src/bin/gman/main.rs @@ -7,7 +7,7 @@ use anyhow::{Context, Result}; use clap::Subcommand; use crossterm::execute; use crossterm::terminal::{LeaveAlternateScreen, disable_raw_mode}; -use gman::config::load_config; +use gman::config::{get_config_file_path, load_config}; use heck::ToSnakeCase; use std::io::{self, IsTerminal, Read, Write}; use std::panic::PanicHookInfo; @@ -31,6 +31,7 @@ enum OutputFormat { author = crate_authors!(), version = crate_version!(), about = crate_description!(), + arg_required_else_help = true, help_template = "\ {before-help}{name} {version} {author-with-newline} @@ -56,8 +57,16 @@ struct Cli { #[arg(long, global = true)] dry_run: bool, + /// Print the log file path and exit + #[arg(long, global = true)] + show_log_path: bool, + + /// Print the config file path and exit + #[arg(long, global = true)] + show_config_path: bool, + #[command(subcommand)] - command: Commands, + command: Option, } #[derive(Subcommand, Clone, Debug)] @@ -115,11 +124,21 @@ fn main() -> Result<()> { panic_hook(info); })); let cli = Cli::parse(); + + if cli.show_log_path { + println!("{}", utils::get_log_path().display()); + return Ok(()); + } + if cli.show_config_path { + println!("{}", get_config_file_path()?.display()); + return Ok(()); + } + let config = load_config()?; let mut provider_config = config.extract_provider_config(cli.provider.clone())?; let secrets_provider = provider_config.extract_provider(); - match cli.command { + match cli.command.with_context(|| "no command provided")? { Commands::Add { name } => { let plaintext = read_all_stdin().with_context(|| "unable to read plaintext from stdin")?; diff --git a/src/bin/gman/utils.rs b/src/bin/gman/utils.rs index 65d3ff9..de6bfef 100644 --- a/src/bin/gman/utils.rs +++ b/src/bin/gman/utils.rs @@ -3,7 +3,7 @@ use log4rs::append::console::ConsoleAppender; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Root}; use log4rs::encode::pattern::PatternEncoder; -use std::fs; +use std::{env, fs}; use std::path::PathBuf; pub fn init_logging_config() -> log4rs::Config { @@ -11,7 +11,6 @@ pub fn init_logging_config() -> log4rs::Config { "{d(%Y-%m-%d %H:%M:%S%.3f)(utc)} <{i}> [{l}] {f}:{L} - {m}{n}", )); - // Prefer file logging, but fall back to console if we cannot create/open the file. let file_appender = FileAppender::builder() .encoder(encoder.clone()) .build(get_log_path()); @@ -44,18 +43,16 @@ pub fn init_logging_config() -> log4rs::Config { } pub fn get_log_path() -> PathBuf { - // Use a cache directory on all platforms; fall back to temp dir as a last resort. - let base_dir = dirs::cache_dir().unwrap_or_else(std::env::temp_dir); + let base_dir = dirs::cache_dir().unwrap_or_else(env::temp_dir); let log_dir = base_dir.join("gman"); - // Best-effort: create the directory; if it fails, write directly into temp dir. let dir = if let Err(e) = fs::create_dir_all(&log_dir) { eprintln!( "Failed to create log directory '{}': {}", log_dir.display(), e ); - std::env::temp_dir() + env::temp_dir() } else { log_dir }; @@ -71,7 +68,6 @@ mod tests { fn test_get_log_path() { let log_path = get_log_path(); assert!(log_path.ends_with("gman.log")); - // Parent directory may be cache dir or temp dir; ensure it is a valid path component. assert!(log_path.parent().is_some()); } }