1package config 2 3import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 "github.com/gopasspw/gopass/pkg/appdir" 9 "github.com/gopasspw/gopass/pkg/debug" 10 "github.com/gopasspw/gopass/pkg/fsutil" 11 12 homedir "github.com/mitchellh/go-homedir" 13) 14 15// Homedir returns the users home dir or an empty string if the lookup fails 16func Homedir() string { 17 if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" { 18 return hd 19 } 20 hd, err := homedir.Dir() 21 if err != nil { 22 debug.Log("Failed to get homedir: %s\n", err) 23 return "" 24 } 25 return hd 26} 27 28// configLocation returns the location of the config file 29// (a YAML file that contains values such as the path to the password store) 30func configLocation() string { 31 // First, check for the "GOPASS_CONFIG" environment variable 32 if cf := os.Getenv("GOPASS_CONFIG"); cf != "" { 33 return cf 34 } 35 36 // Second, check for the "XDG_CONFIG_HOME" environment variable 37 // (which is part of the XDG Base Directory Specification for Linux and 38 // other Unix-like operating sytstems) 39 return filepath.Join(appdir.UserConfig(), "config.yml") 40} 41 42// configLocations returns the possible locations of gopass config files, 43// in decreasing priority 44func configLocations() []string { 45 l := []string{} 46 if cf := os.Getenv("GOPASS_CONFIG"); cf != "" { 47 l = append(l, cf) 48 } 49 l = append(l, filepath.Join(appdir.UserConfig(), "config.yml")) 50 l = append(l, filepath.Join(Homedir(), ".config", "gopass", "config.yml")) 51 l = append(l, filepath.Join(Homedir(), ".gopass.yml")) 52 return l 53} 54 55// PwStoreDir reads the password store dir from the environment 56// or returns the default location if the env is not set 57func PwStoreDir(mount string) string { 58 if mount != "" { 59 cleanName := strings.Replace(mount, string(filepath.Separator), "-", -1) 60 return fsutil.CleanPath(filepath.Join(appdir.UserData(), "stores", cleanName)) 61 } 62 // PASSWORD_STORE_DIR support is discouraged 63 if d := os.Getenv("PASSWORD_STORE_DIR"); d != "" { 64 return fsutil.CleanPath(d) 65 } 66 if ld := filepath.Join(appdir.UserHome(), ".password-store"); fsutil.IsDir(ld) { 67 debug.Log("re-using existing legacy dir for root store: %s", ld) 68 return ld 69 } 70 return fsutil.CleanPath(filepath.Join(appdir.UserData(), "stores", "root")) 71} 72 73// Directory returns the configuration directory for the gopass config file 74func Directory() string { 75 return filepath.Dir(configLocation()) 76} 77