Skip to content

Commit

Permalink
Expand environment variables in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Morganamilo committed Aug 31, 2018
1 parent b757d0a commit 8c1658d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func handleCmd() (err error) {
config.saveConfig()
}

config.expandEnv()

if cmdArgs.existsArg("h", "help") {
err = handleHelp()
return
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 33 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func main() {
}

exitOnError(setPaths())
defaultSettings(&config)
config.defaultSettings()
exitOnError(initHomeDirs())
exitOnError(initConfig())
exitOnError(cmdArgs.parseCommandLine())
Expand Down

0 comments on commit 8c1658d

Please sign in to comment.