Skip to content

Jacket of spf13/viper: Simplified go configuration for wire-jacket.

License

Notifications You must be signed in to change notification settings

bang9211/viper-jacket

Repository files navigation

Viper-Jacket: viper config for Wire-Jacket

GoDoc Github release Build Status Coverage Status Go Report Card

Jacket of spf13/viper: config for wire-jacket. Simplified env-based go configuration package using viper.

Installation

Install Wire-Jacket by running:

go get github.com/bang9211/viper-jacket

and ensuring that $GOPATH/bin is added to your $PATH.

Example

Just call GetOrCreate().

Viper-Jacket uses app.conf by default, or you can specify config file with '--conf' flag explicitly.

It can get config value of the file or environment variable using get-or-else style getter.

cfg := GetOrCreate()
defer cfg.Close()

cfg.GetBool("test_viper_config_bool_value", true)
cfg.GetString("test_viper_config_string_value", "default value")
cfg.GetInt("test_viper_config_int_value", 12345)
cfg.GetInt32("test_viper_config_int32_value", 12345)
cfg.GetInt64("test_viper_config_int64_value", 12345)
cfg.GetUint("test_viper_config_uint_value", 12345)
cfg.GetUint32("test_viper_config_uint32_value", 12345)
cfg.GetUint64("test_viper_config_uint64_value", 12345)
cfg.GetFloat64("test_viper_config_float64_value", 123.456)
cfg.GetTime("test_viper_config_time_value", time.Date(2021, 9, 15, 15, 31, 48, 123, time.UTC))
cfg.GetDuration("test_viper_config_duration_value", 12 * time.Second)
cfg.GetIntSlice("test_viper_config_intslice_value", []int{1, 1, 2, 2, 3, 3})
cfg.GetStringSlice("test_viper_config_stringslice_value", []string{"Hello", "World!", "Nice Day!"})
cfg.GetStringMap("test_viper_config_stringmap_value", map[string]interface{}{
  "hello": "World!",
  "Nice":  float64(112233),
  "Day":   true,
})
cfg.GetStringMapString("test_viper_config_stringmapstring_value", map[string]string{
  "Hello":   "World!",
  "Nice To": "Meet You",
  "Me":      "Too",
})
cfg.GetStringMapSlice("test_viper_config_stringmapslice_value", map[string][]string{
  "test1": {"Hello", "World!"},
  "test2": {"Nice", "To"},
  "test3": {"Meet", "You"},
})
os.Setenv("WHAT", "bigger")
cfg.GetString("no_exist", "white cow is $WHAT than black cow.")

If the string values of GetString(), GetStringSlice(), GetStringSliceMap() contain environment variables, these will be expanded automatically.

For example $ENV_VAR=viperjacket

  • /home/test/$ENV_VAR => /home/test/viperjacket
  • /home/test/${ENV_VAR}/temp => /home/test/viperjacket/temp

Some complex Getter failed because viper doesn't support yet. It can be checked in viperjacket_test.go.

About

Jacket of spf13/viper: Simplified go configuration for wire-jacket.

Resources

License

Stars

Watchers

Forks

Packages

No packages published