From a43b66c8389093d47116fc647d2600675f03f18d Mon Sep 17 00:00:00 2001 From: jlanzarotta Date: Wed, 26 Feb 2025 16:13:42 -0500 Subject: [PATCH] fix: if xdg_config_home is set, it takes presidence If XDG_CONFIG_HOME is set, it now takes presidence for reading/writing of the Khronos configuration file. --- README.adoc | 19 +++++++++++-------- cmd/root.go | 17 ++++++++++------- constants/constants.go | 1 + 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/README.adoc b/README.adoc index 2e1ac51..ff5d7cd 100644 --- a/README.adoc +++ b/README.adoc @@ -47,16 +47,19 @@ To install Khronos, simply unzip the archive for your specific operating system When Khronos starts up, it checks to make sure there is a default configuration file. If it does not exist, it is automatically created for you. -Khronos uses the following precedence order when determining where the configuration file is located. Each item listed, takes precedence over the item below it: +NOTE: Khronos support the use of the _XDG Base Directory Specification_. For more information https://specifications.freedesktop.org/basedir-spec/latest/ -NOTE: For Microsoft Windows(R), the default directory is `%USERPROFILE%`, while under Unix (FreeBSD(R), Linux, macOS(R), etc.) the directory is `$HOME`. +Khronos uses the following precedence order when determining where the configuration file is located. Each item listed, takes precedence over the item below it: -. per-user configuration file -.. %USERPROFILE%/.khronos.yaml -.. $HOME/.khronos.yaml -. XDG_CONFIG_HOME configuration file -.. %XDG_CONFIG_HOME%/khronos/.khronos.yaml -.. $XDG_CONFIG_HOME/khronos/.khronos.yaml +[cols="1,1a"] +|=== +|Microsoft Windows(R) +|. %XDG_CONFIG_HOME%/khronos/.khronos.yaml +. %USERPROFILE%/.khronos.yaml +|FreeBSD(R), Linux, macOS(R), etc. +|. $XDG_CONFIG_HOME/khronos/.khronos.yaml +. $HOME/.khronos.yaml +|=== === Default Configuration diff --git a/cmd/root.go b/cmd/root.go index b261741..a746062 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,7 +80,7 @@ func init() { re := regexp.MustCompile(`(?m)^Flags:\s*$`) usageTemplate = re.ReplaceAllLiteralString(usageTemplate, `{{StyleHeading "Flags:"}}`) rootCmd.SetUsageTemplate(usageTemplate) - rootCmd.SetOutput(color.Output) + rootCmd.SetOut(color.Output) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, @@ -103,9 +103,6 @@ func initConfig() { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - // Add the user's home directory to the search path. - viper.AddConfigPath(home) - // Add the XDG_CONFIG_HOME directory to the search path, if configured. xdgConfigHome, found := os.LookupEnv("XDG_CONFIG_HOME") if found { @@ -118,6 +115,9 @@ func initConfig() { } } + // Add the user's home directory to the search path. + viper.AddConfigPath(home) + // Add the Khronos configuration file and extension type. viper.SetConfigType("yaml") viper.SetConfigName(".khronos") @@ -163,10 +163,13 @@ func initConfig() { if _, ok := err.(viper.ConfigFileNotFoundError); ok { // No config file, just use defaults. viper.SafeWriteConfig() - writeDefaultFavorites(home) - log.Printf("Unable to load config file, using/writing default values to [%s].\n", viper.ConfigFileUsed()) + //writeDefaultFavorites(home) + writeDefaultFavorites(viper.ConfigFileUsed()) + log.Printf("%s: Unable to load config file, using/writing default values to [%s].\n\n", + color.HiBlueString(constants.INFO_NORMAL_CASE), viper.ConfigFileUsed()) } else { - log.Fatalf("%s: Error reading config file: %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error()) + log.Fatalf("%s: Error reading config file: %s\n", + color.RedString(constants.FATAL_NORMAL_CASE), err.Error()) os.Exit(1) } } diff --git a/constants/constants.go b/constants/constants.go index 5734dba..a2a3479 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -88,6 +88,7 @@ const HELLO_LONG_DESCRIPTION = "In order to have khronos start tracking time is const HELLO_SHORT_DESCRIPTION = "Start time tracking for the day" const HELP_SHORT_DESCRIPTION = "Show help for command" const INDENT_AMOUNT int = 4 +const INFO_NORMAL_CASE string = "Info" const MAY_BE_OVERRIDDEN_BY_GLOBAL_CONFIGURATION_SETTING = "* May be overridden by global configuration setting" const NATURAL_LANGUAGE_DESCRIPTION string = "Natural Language Time, e.g., '18 minutes ago' or '9:45am'" const NOTE string = "note"