Skip to content

Commit

Permalink
refactor(config): Improve config file paths and docsx
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjwarren committed Aug 21, 2018
1 parent 0afcc6f commit d6b25ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
22 changes: 22 additions & 0 deletions {{cookiecutter.name}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
9 changes: 7 additions & 2 deletions {{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 3 additions & 4 deletions {{cookiecutter.name}}/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d6b25ca

Please sign in to comment.