Skip to content

Commit 2f2748b

Browse files
committed
feat: XDG_CONFIG_HOME support
- If a user has already set the XDG_CONFIG_HOME variable, they expect config to be placed there. - This change allows users who set the variable to take advantage of it. - It should not break existing users' experience in any way.
1 parent 8ccc01e commit 2f2748b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Run `bootdev login` to authenticate with your Boot.dev account. After authentica
8686

8787
## Configuration
8888

89-
The Boot.dev CLI offers a couple of configuration options that are stored in a config file (default is `~/.bootdev.yaml`).
89+
The Boot.dev CLI offers a couple of configuration options that are stored in a config file (default is `~/.bootdev.yaml` or `$XDG_CONFIG_HOME/bootdev/config.yaml`).
9090

9191
All commands have `-h`/`--help` flags if you want to see available options on the command line.
9292

cmd/root.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
var cfgFile string
17+
var xdgCfgHome string
1718

1819
var rootCmd = &cobra.Command{
1920
Use: "bootdev",
@@ -34,7 +35,8 @@ func Execute(currentVersion string) error {
3435

3536
func init() {
3637
cobra.OnInitialize(initConfig)
37-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml)")
38+
xdgCfgHome = os.Getenv("XDG_CONFIG_HOME")
39+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml or $XDG_CONFIG_HOME/bootdev/config.yaml)")
3840
}
3941

4042
func readViperConfig(paths []string) error {
@@ -68,10 +70,28 @@ func initConfig() {
6870
// viper's built in config path thing sucks, let's do it ourselves
6971
defaultPath := path.Join(home, ".bootdev.yaml")
7072
configPaths := []string{}
71-
configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml"))
73+
74+
// If `$HOME/.bootdev.yaml` already exists, prefer that.
7275
configPaths = append(configPaths, defaultPath)
76+
77+
if xdgCfgHome != "" {
78+
// If $XDG_CONFIG_HOME is set by the user, consider that our primary path.
79+
defaultPath = path.Join(xdgCfgHome, "bootdev", "config.yaml")
80+
81+
// Create the config directory if it doesn't exist yet
82+
err = os.MkdirAll(path.Dir(defaultPath), 0755)
83+
cobra.CheckErr(err)
84+
85+
// Add the new XDG-based defaultPath to the config paths as well.
86+
configPaths = append(configPaths, defaultPath)
87+
} else {
88+
// Add search path but do not create it and do not consider it the default.
89+
configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml"))
90+
}
91+
7392
if err := readViperConfig(configPaths); err != nil {
74-
viper.SafeWriteConfigAs(defaultPath)
93+
err = viper.SafeWriteConfigAs(defaultPath)
94+
cobra.CheckErr(err)
7595
viper.SetConfigFile(defaultPath)
7696
err = viper.ReadInConfig()
7797
cobra.CheckErr(err)

0 commit comments

Comments
 (0)