Skip to content

Commit

Permalink
feat: support APPNAME_CONFIG environment variable for config file (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <kzantow@gmail.com>
  • Loading branch information
kzantow authored Aug 5, 2023
1 parent 205b14e commit 224dc42
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fangs

import (
"fmt"
"os"

"github.com/anchore/go-logger"
"github.com/anchore/go-logger/adapter/discard"
Expand All @@ -18,10 +19,15 @@ type Config struct {
var _ FlagAdder = (*Config)(nil)

func NewConfig(appName string) Config {
// check for environment variable <app_name>_CONFIG, if set this will be the default
// but will be overridden by a command-line flag
configFile := os.Getenv(envVar(appName, "CONFIG"))

return Config{
Logger: discard.New(),
AppName: appName,
TagName: "mapstructure",
File: configFile,
// search for configs in specific order
Finders: []Finder{
// 1. look for a directly configured file
Expand Down
19 changes: 18 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/anchore/go-logger/adapter/discard"
)

func Test_Config(t *testing.T) {
func Test_BasicConfig(t *testing.T) {
c := NewConfig("appName")
cmd := cobra.Command{}

Expand All @@ -27,3 +27,20 @@ func Test_Config(t *testing.T) {

require.Contains(t, flags, "config")
}

func Test_EnvVarConfig(t *testing.T) {
t.Setenv("APPNAME_CONFIG", "some/config.env")

c := NewConfig("appName")
require.Equal(t, c.File, "some/config.env")

cmd := cobra.Command{}

fs := NewPFlagSet(discard.New(), cmd.Flags())
c.AddFlags(fs)

// simulate the flag set
err := cmd.Flags().Set("config", "a/config.flag")
require.NoError(t, err)
require.Equal(t, c.File, "a/config.flag")
}
2 changes: 1 addition & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func configureViper(cfg Config, vpr *viper.Viper, v reflect.Value, flags flagRef
}

if !isStruct(t) {
envVar := envVar(cfg.AppName, path)
envVar := envVar(cfg.AppName, path...)
path := strings.Join(path, ".")

if flag, ok := flags[ptr]; ok {
Expand Down
2 changes: 1 addition & 1 deletion summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func summarize(cfg Config, descriptions DescriptionProvider, s *section, value r
name,
v,
descriptions.GetDescription(v, f),
envVar(cfg.AppName, path))
envVar(cfg.AppName, path...))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func contains(parts []string, value string) bool {

var envVarRegex = regexp.MustCompile("[^a-zA-Z0-9_]")

func envVar(appName string, parts []string) string {
func envVar(appName string, parts ...string) string {
v := strings.Join(parts, "_")
if appName != "" {
v = appName + "_" + v
Expand Down

0 comments on commit 224dc42

Please sign in to comment.