From 7b690cf47c2d2ff26d1eead86aebf7e00ea02e82 Mon Sep 17 00:00:00 2001 From: Pavel Valodzka Date: Tue, 9 Oct 2018 21:14:32 +0300 Subject: [PATCH] add support for config file reading --- main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 4f2b53d..1426c3b 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" + "io/ioutil" "github.com/mitchellh/mapstructure" "github.com/pelletier/go-toml" flag "github.com/spf13/pflag" @@ -209,7 +210,18 @@ type Config struct { } func makeConfig() *Config { - defaults, commandSpecs := parseTomlConfig(defaultTomlConfig) + var configContent = defaultTomlConfig + + if config.ConfigPath != "" { + if b, err := ioutil.ReadFile(config.ConfigPath); err != nil { + fmt.Fprintf(os.Stderr, "Error while reading config file '%s': %s\n", config.ConfigPath, err) + os.Exit(1) + } else { + configContent = string(b) + } + } + + defaults, commandSpecs := parseTomlConfig(configContent) config := Config{ BindAddr: defaults.Get("listen-addr").(string), @@ -225,13 +237,15 @@ func makeConfig() *Config { var config = &Config{} func main() { + flag.StringVarP(&config.ConfigPath, "config", "c", "", "") + flag.Parse() + config = makeConfig() printHelp := flag.BoolP("help", "h", false, "Show this help message and exit") printConfigHelp := flag.BoolP("help-config", "e", false, "Show configuration file help and exit") flag.StringVarP(&config.BindAddr, "bind", "b", config.BindAddr, "Listen on the specified address and port") - flag.StringVarP(&config.ConfigPath, "config", "c", "", "") flag.StringVarP(&config.RelativeRoot, "relative-root", "r", config.RelativeRoot, "webapp relative root") flag.BoolVarP(&config.AllowDownload, "allow-download", "a", config.AllowDownload, "allow file downloads") flag.Parse()