From 959d19cbdb40c4f3cc45a0ac313c5774c4ce206e Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 6 Oct 2021 15:50:23 +0200 Subject: [PATCH] add XDG_CONFIG_HOME to config --- cmd/root.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 3201aac..a37adfe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "os" + "path/filepath" "github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal/colors" @@ -50,11 +51,24 @@ func initConfig() { if cfgFile != "" { viper.SetConfigFile(cfgFile) } else { - home, err := homedir.Dir() - CheckErr(err) - viper.AddConfigPath(".") - viper.AddConfigPath(home) + + // Home + if home, err := homedir.Dir(); err != nil { + viper.AddConfigPath(home) + } + + // XDG_CONFIG_HOME + { + prefix, found := os.LookupEnv("XDG_CONFIG_HOME") + if !found { + if home, err := homedir.Dir(); err != nil { + prefix = filepath.Join(home, ".config") + } + } + viper.AddConfigPath(filepath.Join(prefix, "autorestic")) + } + viper.SetConfigName(".autorestic") } viper.AutomaticEnv()