From 7cb1e0ee85363caa14d0f7148a638bd47ea31cf3 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 8 Sep 2015 22:07:57 +0200 Subject: [PATCH] Added VarDefault functionality --- config.go | 29 +++++++++- vardefault_providers.go | 27 +++++++++ vardefault_test.go | 122 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 vardefault_providers.go create mode 100644 vardefault_test.go diff --git a/config.go b/config.go index ceaebed..7d30147 100644 --- a/config.go +++ b/config.go @@ -14,7 +14,14 @@ import ( "github.com/spf13/pflag" ) -var fs *pflag.FlagSet +var ( + fs *pflag.FlagSet + variableDefaults map[string]string +) + +func init() { + variableDefaults = make(map[string]string) +} // Parse takes the pointer to a struct filled with variables which should be read // from ENV, default or flag. The precedence in this is flag > ENV > default. So @@ -25,6 +32,7 @@ var fs *pflag.FlagSet // the behavior of rconfig: // // default: Set a default value +// vardefault: Read the default value from the variable defaults // env: Read the value from this environment variable // flag: Flag to read in format "long,short" (for example "listen,l") // description: A help text for Usage output to guide your users @@ -45,6 +53,12 @@ func Usage() { } } +// SetVariableDefaults presets the parser with a map of default values to be used +// when specifying the vardefault tag +func SetVariableDefaults(defaults map[string]string) { + variableDefaults = defaults +} + func parse(in interface{}, args []string) error { if args == nil { args = os.Args @@ -78,6 +92,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { } value := envDefault(typeField.Tag.Get("env"), typeField.Tag.Get("default")) + value = varDefault(typeField.Tag.Get("vardefault"), value) parts := strings.Split(typeField.Tag.Get("flag"), ",") switch typeField.Type.Kind() { @@ -280,3 +295,15 @@ func envDefault(env, def string) string { return value } + +func varDefault(name, def string) string { + value := def + + if name != "" { + if v, ok := variableDefaults[name]; ok { + value = v + } + } + + return value +} diff --git a/vardefault_providers.go b/vardefault_providers.go new file mode 100644 index 0000000..2199cfa --- /dev/null +++ b/vardefault_providers.go @@ -0,0 +1,27 @@ +package rconfig + +import ( + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +// VarDefaultsFromYAMLFile reads contents of a file and calls VarDefaultsFromYAML +func VarDefaultsFromYAMLFile(filename string) map[string]string { + data, err := ioutil.ReadFile(filename) + if err != nil { + return make(map[string]string) + } + + return VarDefaultsFromYAML(data) +} + +// VarDefaultsFromYAML creates a vardefaults map from YAML raw data +func VarDefaultsFromYAML(in []byte) map[string]string { + out := make(map[string]string) + err := yaml.Unmarshal(in, &out) + if err != nil { + return make(map[string]string) + } + return out +} diff --git a/vardefault_test.go b/vardefault_test.go new file mode 100644 index 0000000..8328919 --- /dev/null +++ b/vardefault_test.go @@ -0,0 +1,122 @@ +package rconfig + +import ( + "io/ioutil" + "os" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing variable defaults", func() { + + type t struct { + MySecretValue string `default:"secret" env:"foo" vardefault:"my_secret_value"` + MyUsername string `default:"luzifer" vardefault:"username"` + SomeVar string `flag:"var" description:"some variable"` + IntVar int64 `vardefault:"int_var" default:"23"` + } + + var ( + err error + cfg t + args = []string{} + vardefaults = map[string]string{ + "my_secret_value": "veryverysecretkey", + "unkownkey": "hi there", + "int_var": "42", + } + ) + + BeforeEach(func() { + cfg = t{} + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + Context("With manually provided variables", func() { + BeforeEach(func() { + SetVariableDefaults(vardefaults) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.IntVar).To(Equal(int64(42))) + Expect(cfg.MySecretValue).To(Equal("veryverysecretkey")) + Expect(cfg.MyUsername).To(Equal("luzifer")) + Expect(cfg.SomeVar).To(Equal("")) + }) + }) + + Context("With defaults from YAML data", func() { + BeforeEach(func() { + yamlData := []byte("---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n") + SetVariableDefaults(VarDefaultsFromYAML(yamlData)) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.IntVar).To(Equal(int64(42))) + Expect(cfg.MySecretValue).To(Equal("veryverysecretkey")) + Expect(cfg.MyUsername).To(Equal("luzifer")) + Expect(cfg.SomeVar).To(Equal("")) + }) + }) + + Context("With defaults from YAML file", func() { + var tmp *os.File + + BeforeEach(func() { + tmp, _ = ioutil.TempFile("", "") + yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n" + tmp.WriteString(yamlData) + SetVariableDefaults(VarDefaultsFromYAMLFile(tmp.Name())) + }) + + AfterEach(func() { + tmp.Close() + os.Remove(tmp.Name()) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.IntVar).To(Equal(int64(42))) + Expect(cfg.MySecretValue).To(Equal("veryverysecretkey")) + Expect(cfg.MyUsername).To(Equal("luzifer")) + Expect(cfg.SomeVar).To(Equal("")) + }) + }) + + Context("With defaults from invalid YAML data", func() { + BeforeEach(func() { + yamlData := []byte("---\nmy_secret_value = veryverysecretkey\nunknownkey = hi there\nint_var = 42\n") + SetVariableDefaults(VarDefaultsFromYAML(yamlData)) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.IntVar).To(Equal(int64(23))) + Expect(cfg.MySecretValue).To(Equal("secret")) + Expect(cfg.MyUsername).To(Equal("luzifer")) + Expect(cfg.SomeVar).To(Equal("")) + }) + }) + + Context("With defaults from non existent YAML file", func() { + BeforeEach(func() { + file := "/tmp/this_file_should_not_exist_146e26723r" + SetVariableDefaults(VarDefaultsFromYAMLFile(file)) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.IntVar).To(Equal(int64(23))) + Expect(cfg.MySecretValue).To(Equal("secret")) + Expect(cfg.MyUsername).To(Equal("luzifer")) + Expect(cfg.SomeVar).To(Equal("")) + }) + }) + +})