diff --git a/README.md b/README.md index cebe16a..bec7397 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,35 @@ func main() { } ``` +### Provide variable defaults by using a file + +Given you have a file `~/.myapp.yml` containing some secrets or usernames (for the example below username is assumed to be "luzifer") as a default configuration for your application you can use this source code to load the defaults from that file using the `vardefault` tag in your configuration struct. + +The order of the directives (lower number = higher precedence): + +1. Flags provided in command line +1. Environment variables +1. Variable defaults (`vardefault` tag in the struct) +1. `default` tag in the struct + +```go +type config struct { + Username string `vardefault:"username" flag:"username" description:"Your username"` +} + +var cfg = config{} + +func init() { + rconfig.SetVariableDefaults(VarDefaultsFromYAMLFile("~/.myapp.yml")) + rconfig.Parse(&cfg) +} + +func main() { + fmt.Printf("Username = %s", cfg.Username) + // Output: Username = luzifer +} +``` + ## More info You can see the full reference documentation of the rconfig package [at godoc.org](https://godoc.org/github.com/Luzifer/rconfig), or through go's standard documentation system by running `godoc -http=:6060` and browsing to [http://localhost:6060/pkg/github.com/Luzifer/rconfig](http://localhost:6060/pkg/github.com/Luzifer/rconfig) after installation. diff --git a/config.go b/config.go index 7d30147..a97a221 100644 --- a/config.go +++ b/config.go @@ -91,8 +91,8 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { continue } - value := envDefault(typeField.Tag.Get("env"), typeField.Tag.Get("default")) - value = varDefault(typeField.Tag.Get("vardefault"), value) + value := varDefault(typeField.Tag.Get("vardefault"), typeField.Tag.Get("default")) + value = envDefault(typeField.Tag.Get("env"), value) parts := strings.Split(typeField.Tag.Get("flag"), ",") switch typeField.Type.Kind() { diff --git a/precedence_test.go b/precedence_test.go new file mode 100644 index 0000000..6d87ca0 --- /dev/null +++ b/precedence_test.go @@ -0,0 +1,87 @@ +package rconfig + +import ( + "os" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Precedence", func() { + + type t struct { + A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"` + } + + var ( + err error + cfg t + args []string + vardefaults map[string]string + ) + + JustBeforeEach(func() { + cfg = t{} + SetVariableDefaults(vardefaults) + err = parse(&cfg, args) + }) + + Context("Provided: Flag, Env, Default, VarDefault", func() { + BeforeEach(func() { + args = []string{"-a", "5"} + os.Setenv("a", "8") + vardefaults = map[string]string{ + "a": "3", + } + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the flag value", func() { + Expect(cfg.A).To(Equal(5)) + }) + }) + + Context("Provided: Env, Default, VarDefault", func() { + BeforeEach(func() { + args = []string{} + os.Setenv("a", "8") + vardefaults = map[string]string{ + "a": "3", + } + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the env value", func() { + Expect(cfg.A).To(Equal(8)) + }) + }) + + Context("Provided: Default, VarDefault", func() { + BeforeEach(func() { + args = []string{} + os.Unsetenv("a") + vardefaults = map[string]string{ + "a": "3", + } + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the vardefault value", func() { + Expect(cfg.A).To(Equal(3)) + }) + }) + + Context("Provided: Default", func() { + BeforeEach(func() { + args = []string{} + os.Unsetenv("a") + vardefaults = map[string]string{} + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the default value", func() { + Expect(cfg.A).To(Equal(1)) + }) + }) + +})