Expand environment variables in config

This commit is contained in:
morganamilo 2018-08-31 21:19:40 +01:00
parent b757d0abd3
commit 8c1658df0b
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
3 changed files with 38 additions and 4 deletions

5
cmd.go
View file

@ -132,6 +132,8 @@ func handleCmd() (err error) {
config.saveConfig()
}
config.expandEnv()
if cmdArgs.existsArg("h", "help") {
err = handleHelp()
return
@ -196,7 +198,8 @@ func handlePrint() (err error) {
switch {
case cmdArgs.existsArg("d", "defaultconfig"):
var tmpConfig Configuration
defaultSettings(&tmpConfig)
tmpConfig.defaultSettings()
tmpConfig.expandEnv()
fmt.Printf("%v", tmpConfig)
case cmdArgs.existsArg("g", "currentconfig"):
fmt.Printf("%v", config)

View file

@ -146,9 +146,14 @@ func (config *Configuration) saveConfig() error {
return err
}
func defaultSettings(config *Configuration) {
func (config *Configuration) defaultSettings() {
buildDir := "$HOME/.config/yay"
if os.Getenv("XDG_CONFIG_HOME") != "" {
buildDir = "$XDG_CONFIG_HOME/yay"
}
config.AURURL = "https://aur.archlinux.org"
config.BuildDir = cacheHome
config.BuildDir = buildDir
config.CleanAfter = false
config.Editor = ""
config.EditorFlags = ""
@ -188,6 +193,32 @@ func defaultSettings(config *Configuration) {
config.CombinedUpgrade = false
}
func (config *Configuration) expandEnv() {
config.AURURL = os.ExpandEnv(config.AURURL)
config.BuildDir = os.ExpandEnv(config.BuildDir)
config.Editor = os.ExpandEnv(config.Editor)
config.EditorFlags = os.ExpandEnv(config.EditorFlags)
config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
config.PacmanBin = os.ExpandEnv(config.PacmanBin)
config.PacmanConf = os.ExpandEnv(config.PacmanConf)
config.GpgFlags = os.ExpandEnv(config.GpgFlags)
config.MFlags = os.ExpandEnv(config.MFlags)
config.GitFlags = os.ExpandEnv(config.GitFlags)
config.SortBy = os.ExpandEnv(config.SortBy)
config.TarBin = os.ExpandEnv(config.TarBin)
config.GitBin = os.ExpandEnv(config.GitBin)
config.GpgBin = os.ExpandEnv(config.GpgBin)
config.ReDownload = os.ExpandEnv(config.ReDownload)
config.ReBuild = os.ExpandEnv(config.ReBuild)
config.AnswerClean = os.ExpandEnv(config.AnswerClean)
config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
config.RemoveMake = os.ExpandEnv(config.RemoveMake)
}
// Editor returns the preferred system editor.
func editor() (string, []string) {
switch {

View file

@ -198,7 +198,7 @@ func main() {
}
exitOnError(setPaths())
defaultSettings(&config)
config.defaultSettings()
exitOnError(initHomeDirs())
exitOnError(initConfig())
exitOnError(cmdArgs.parseCommandLine())