From d6b25ca093edc3bf8941a574d7336a3fa28f9edd Mon Sep 17 00:00:00 2001 From: James Warren Date: Tue, 21 Aug 2018 23:35:45 +0100 Subject: [PATCH] refactor(config): Improve config file paths and docsx --- {{cookiecutter.name}}/README.md | 22 +++++++++++++++++++ .../cmd/{{cookiecutter.name}}/main.go | 9 ++++++-- .../internal/config/config.go | 7 +++--- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/{{cookiecutter.name}}/README.md b/{{cookiecutter.name}}/README.md index d08492c..fd883cf 100644 --- a/{{cookiecutter.name}}/README.md +++ b/{{cookiecutter.name}}/README.md @@ -14,3 +14,25 @@ These steps will describe how to setup this project for active development. Adju 6. Install dependencies (these live in the `vendor/` directory: `dep ensure` 7. Build: `make build` 8. 🍻 + +## Configuration + +Configuration can be provided through a toml file, these are loaded +in order from: + +{% if cookiecutter.project is not none -%} +- `/etc/{{cookiecutter.project}}/{{ cookiecutter.name }}.toml` +- `$HOME/.config/{{cookiecutter.project}}/{{ cookiecutter.name }}.toml` +{% else -%} +- `/etc/{{cookiecutter.name}}/{{ cookiecutter.name }}.toml` +- `$HOME/.config/{{ cookiecutter.name }}.toml` +{% endif -%} + +Alternatively a config file path can be provided through the +-c/--config CLI flag. + +### Example {{ cookiecutter.name }}.toml +```toml +[log] +format = "json" # [json|console|discard] +``` diff --git a/{{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go b/{{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go index 4e80d1d..4ef8277 100644 --- a/{{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go +++ b/{{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go @@ -33,16 +33,21 @@ func {{cookiecutter.name}}Cmd() *cobra.Command { // Init config if err := config.FromFile(); err != nil { log.Error().Err(err).Msg("failed to read configuration file") + } else { + log.Debug().Msg(fmt.Sprintf("using config file: %s", viper.ConfigFileUsed())) } // Reconfigure logger with config log = initLogger() - log.Debug().Msg(fmt.Sprintf("using config file: %s", viper.ConfigFileUsed())) }, Run: {{cookiecutter.name}}Run, } // Global flags pflags := cmd.PersistentFlags() - pflags.StringP("config", "c", "", "path to configuration file (default is $HOME/.config/{{cookiecutter.name}}.yaml)") + {% if cookiecutter.project is not none -%} + pflags.StringP("config", "c", "", "path to configuration file (default is $HOME/.config/{{cookiecutter.project}}/{{cookiecutter.name}}.toml)") + {% else -%} + pflags.StringP("config", "c", "", "path to configuration file (default is $HOME/.config/{{cookiecutter.name}}.toml)") + {% endif -%} pflags.String("log-format", "", "log format [console|json] (default is json)") // Bind flags to config options config.BindPFlags(map[string]*pflag.Flag{ diff --git a/{{cookiecutter.name}}/internal/config/config.go b/{{cookiecutter.name}}/internal/config/config.go index 597e004..8d45610 100644 --- a/{{cookiecutter.name}}/internal/config/config.go +++ b/{{cookiecutter.name}}/internal/config/config.go @@ -22,13 +22,12 @@ func init() { // Config file lookup locations viper.SetConfigType("toml") viper.SetConfigName("{{cookiecutter.name}}") - viper.AddConfigPath("$HOME/.config/") {% if cookiecutter.project is not none -%} - viper.AddConfigPath("/etc/{{cookiecutter.project}}/{{cookiecutter.name}}") - viper.AddConfigPath("$HOME/.config/{{cookiecutter.project}}/{{cookiecutter.name}}") + viper.AddConfigPath("/etc/{{cookiecutter.project}}") + viper.AddConfigPath("$HOME/.config/{{cookiecutter.project}}") {% else -%} viper.AddConfigPath("/etc/{{cookiecutter.name}}") - viper.AddConfigPath("$HOME/.config/{{cookiecutter.name}}") + viper.AddConfigPath("$HOME/.config") {% endif -%} // Environment variables viper.AutomaticEnv()