Skip to content

Commit

Permalink
fix: if xdg_config_home is set, it takes presidence
Browse files Browse the repository at this point in the history
If XDG_CONFIG_HOME is set, it now takes presidence for reading/writing of the Khronos configuration file.
  • Loading branch information
jlanzarotta committed Feb 26, 2025
1 parent 85ffb33 commit a43b66c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
19 changes: 11 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 10 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a43b66c

Please sign in to comment.