Fix the behavior of the -ignore-config command line flag

This commit is contained in:
nikitar020 2019-01-02 22:49:34 +07:00 committed by Max Risuhin
parent 10a962746f
commit 417c436fe7
1 changed files with 41 additions and 14 deletions

View File

@ -9,26 +9,33 @@ import (
"path/filepath" "path/filepath"
"github.com/liamg/aminal/config" "github.com/liamg/aminal/config"
"github.com/liamg/aminal/version" "github.com/liamg/aminal/version"
"strings"
) )
func isFlagProvided(flagName string) bool {
result := false
flag.Visit(func(f *flag.Flag) {
result = strings.Compare(f.Name, flagName) == 0
})
return result
}
func getConfig() *config.Config { func getConfig() *config.Config {
showVersion := false showVersion := false
ignoreConfig := false
shell := ""
debugMode := false
slomo := false
flag.BoolVar(&showVersion, "version", showVersion, "Output version information") flag.BoolVar(&showVersion, "version", showVersion, "Output version information")
flag.BoolVar(&ignoreConfig, "ignore-config", ignoreConfig, "Ignore user config files and use defaults")
flag.StringVar(&shell, "shell", shell, "Specify the shell to use")
flag.BoolVar(&debugMode, "debug", debugMode, "Enable debug logging")
flag.BoolVar(&slomo, "slomo", slomo, "Render in slow motion (useful for debugging)")
ignore := false flag.Parse() // actual parsing and fetching flags from the command line
flag.BoolVar(&ignore, "ignore-config", ignore, "Ignore user config files and use defaults")
if ignore {
return &config.DefaultConfig
}
conf := loadConfigFile()
flag.StringVar(&conf.Shell, "shell", conf.Shell, "Specify the shell to use")
flag.BoolVar(&conf.DebugMode, "debug", conf.DebugMode, "Enable debug logging")
flag.BoolVar(&conf.Slomo, "slomo", conf.Slomo, "Render in slow motion (useful for debugging)")
flag.Parse()
if showVersion { if showVersion {
v := version.Version v := version.Version
@ -39,6 +46,26 @@ func getConfig() *config.Config {
os.Exit(0) os.Exit(0)
} }
var conf *config.Config
if ignoreConfig {
conf = &config.DefaultConfig
} else {
conf = loadConfigFile()
}
// Override values in the configuration file with the values specified in the command line, if any.
if isFlagProvided("shell") {
conf.Shell = shell
}
if isFlagProvided("debug") {
conf.DebugMode = debugMode
}
if isFlagProvided("slomo") {
conf.Slomo = slomo
}
return conf return conf
} }