Skip to content

feat: XDG_CONFIG_HOME support #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Run `bootdev login` to authenticate with your Boot.dev account. After authentica

## Configuration

The Boot.dev CLI offers a couple of configuration options that are stored in a config file (default is `~/.bootdev.yaml`).
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`).

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

Expand Down
26 changes: 23 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

var cfgFile string
var xdgCfgHome string

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

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml)")
xdgCfgHome = os.Getenv("XDG_CONFIG_HOME")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml or $XDG_CONFIG_HOME/bootdev/config.yaml)")
}

func readViperConfig(paths []string) error {
Expand Down Expand Up @@ -68,10 +70,28 @@ func initConfig() {
// viper's built in config path thing sucks, let's do it ourselves
defaultPath := path.Join(home, ".bootdev.yaml")
configPaths := []string{}
configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml"))

// If `$HOME/.bootdev.yaml` already exists, prefer that.
configPaths = append(configPaths, defaultPath)

if xdgCfgHome != "" {
// If $XDG_CONFIG_HOME is set by the user, consider that our primary path.
defaultPath = path.Join(xdgCfgHome, "bootdev", "config.yaml")

// Create the config directory if it doesn't exist yet
err = os.MkdirAll(path.Dir(defaultPath), 0755)
cobra.CheckErr(err)

// Add the new XDG-based defaultPath to the config paths as well.
configPaths = append(configPaths, defaultPath)
} else {
// Add search path but do not create it and do not consider it the default.
configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml"))
}

if err := readViperConfig(configPaths); err != nil {
viper.SafeWriteConfigAs(defaultPath)
err = viper.SafeWriteConfigAs(defaultPath)
cobra.CheckErr(err)
viper.SetConfigFile(defaultPath)
err = viper.ReadInConfig()
cobra.CheckErr(err)
Expand Down