-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from netlify/add-root-args
RootArg structure for easier config
- Loading branch information
Showing
5 changed files
with
203 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package nconf | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type RootArgs struct { | ||
Prefix string | ||
EnvFile string | ||
} | ||
|
||
func (args *RootArgs) Setup(config interface{}, version string) (logrus.FieldLogger, error) { | ||
// first load the logger | ||
logConfig := &struct { | ||
Log *LoggingConfig | ||
}{} | ||
if err := LoadFromEnv(args.Prefix, args.EnvFile, logConfig); err != nil { | ||
return nil, errors.Wrap(err, "Failed to load the logging configuration") | ||
} | ||
|
||
log, err := ConfigureLogging(logConfig.Log) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Failed to create the logger") | ||
} | ||
if version == "" { | ||
version = "unknown" | ||
} | ||
log = log.WithField("version", version) | ||
|
||
if config != nil { | ||
// second load the config for this project | ||
if err := LoadFromEnv(args.Prefix, args.EnvFile, config); err != nil { | ||
return log, errors.Wrap(err, "Failed to load the config object") | ||
} | ||
log.Debug("Loaded configuration") | ||
} | ||
return log, nil | ||
} | ||
|
||
func (args *RootArgs) MustSetup(config interface{}, version string) logrus.FieldLogger { | ||
logger, err := args.Setup(config, version) | ||
if err != nil { | ||
if logger != nil { | ||
logger.WithError(err).Fatal("Failed to setup configuration") | ||
} else { | ||
panic(fmt.Sprintf("Failed to setup configuratio: %s", err.Error())) | ||
} | ||
} | ||
|
||
return logger | ||
} | ||
|
||
func (args *RootArgs) ConfigFlag() *pflag.Flag { | ||
return &pflag.Flag{ | ||
Name: "config", | ||
Shorthand: "c", | ||
Usage: "A .env file to load configuration from", | ||
Value: newStringValue("", &args.EnvFile), | ||
} | ||
} | ||
|
||
func (args *RootArgs) PrefixFlag() *pflag.Flag { | ||
return &pflag.Flag{ | ||
Name: "prefix", | ||
Shorthand: "p", | ||
Usage: "A prefix to search for when looking for env vars", | ||
Value: newStringValue("", &args.Prefix), | ||
} | ||
} | ||
|
||
type stringValue string | ||
|
||
func newStringValue(val string, p *string) *stringValue { | ||
*p = val | ||
return (*stringValue)(p) | ||
} | ||
|
||
func (s *stringValue) Set(val string) error { | ||
*s = stringValue(val) | ||
return nil | ||
} | ||
func (s *stringValue) Type() string { return "string" } | ||
func (s *stringValue) String() string { return string(*s) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package nconf | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestArgsLoad(t *testing.T) { | ||
cfg := &struct { | ||
Something string | ||
Other int | ||
Overridden string | ||
}{ | ||
Something: "default", | ||
Overridden: "this should change", | ||
} | ||
|
||
tmp, err := ioutil.TempFile("", "something") | ||
require.NoError(t, err) | ||
cfgStr := ` | ||
PF_OTHER=10 | ||
PF_OVERRIDDEN=not-that | ||
PF_LOG_LEVEL=debug | ||
PF_LOG_QUOTE_EMPTY_FIELDS=true | ||
` | ||
require.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(cfgStr), 0644)) | ||
|
||
args := RootArgs{ | ||
Prefix: "pf", | ||
EnvFile: tmp.Name(), | ||
} | ||
|
||
log, err := args.Setup(cfg, "") | ||
require.NoError(t, err) | ||
|
||
// check that we did call configure the logger | ||
assert.NotNil(t, log) | ||
entry := log.(*logrus.Entry) | ||
assert.Equal(t, logrus.DebugLevel, entry.Logger.Level) | ||
assert.True(t, entry.Logger.Formatter.(*logrus.TextFormatter).QuoteEmptyFields) | ||
|
||
assert.Equal(t, "default", cfg.Something) | ||
assert.Equal(t, 10, cfg.Other) | ||
assert.Equal(t, "not-that", cfg.Overridden) | ||
} | ||
|
||
func TestArgsAddToCmd(t *testing.T) { | ||
args := new(RootArgs) | ||
var called int | ||
cmd := cobra.Command{ | ||
Run: func(_ *cobra.Command, _ []string) { | ||
assert.Equal(t, "PF", args.Prefix) | ||
assert.Equal(t, "file.env", args.EnvFile) | ||
called++ | ||
}, | ||
} | ||
cmd.PersistentFlags().AddFlag(args.ConfigFlag()) | ||
cmd.PersistentFlags().AddFlag(args.PrefixFlag()) | ||
cmd.SetArgs([]string{"--config", "file.env", "--prefix", "PF"}) | ||
require.NoError(t, cmd.Execute()) | ||
assert.Equal(t, 1, called) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nconf | ||
|
||
import ( | ||
"github.com/bugsnag/bugsnag-go" | ||
logrus_bugsnag "github.com/shopify/logrus-bugsnag" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type BugSnagConfig struct { | ||
Environment string | ||
APIKey string `envconfig:"api_key"` | ||
} | ||
|
||
func AddBugSnagHook(config *BugSnagConfig) error { | ||
if config == nil || config.APIKey == "" { | ||
return nil | ||
} | ||
|
||
bugsnag.Configure(bugsnag.Configuration{ | ||
APIKey: config.APIKey, | ||
ReleaseStage: config.Environment, | ||
PanicHandler: func() {}, // this is to disable panic handling. The lib was forking and restarting the process (causing races) | ||
}) | ||
hook, err := logrus_bugsnag.NewBugsnagHook() | ||
if err != nil { | ||
return err | ||
} | ||
logrus.AddHook(hook) | ||
logrus.Debug("Added bugsnag hook") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters