diff --git a/logger.go b/logger.go index 0115067ae6..a64e1446cc 100644 --- a/logger.go +++ b/logger.go @@ -7,8 +7,8 @@ import ( ) // Logger is a unified interface for various logging use cases and practices, including: -// - leveled logging -// - structured logging +// - leveled logging +// - structured logging type Logger interface { // Trace logs a Trace event. // diff --git a/viper.go b/viper.go index d6bbe62eae..af6ee6925c 100644 --- a/viper.go +++ b/viper.go @@ -132,10 +132,10 @@ type DecoderConfigOption func(*mapstructure.DecoderConfig) // DecodeHook returns a DecoderConfigOption which overrides the default // DecoderConfig.DecodeHook value, the default is: // -// mapstructure.ComposeDecodeHookFunc( -// mapstructure.StringToTimeDurationHookFunc(), -// mapstructure.StringToSliceHookFunc(","), -// ) +// mapstructure.ComposeDecodeHookFunc( +// mapstructure.StringToTimeDurationHookFunc(), +// mapstructure.StringToSliceHookFunc(","), +// ) func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption { return func(c *mapstructure.DecoderConfig) { c.DecodeHook = hook @@ -156,18 +156,18 @@ func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption { // // For example, if values from the following sources were loaded: // -// Defaults : { -// "secret": "", -// "user": "default", -// "endpoint": "https://localhost" -// } -// Config : { -// "user": "root" -// "secret": "defaultsecret" -// } -// Env : { -// "secret": "somesecretkey" -// } +// Defaults : { +// "secret": "", +// "user": "default", +// "endpoint": "https://localhost" +// } +// Config : { +// "user": "root" +// "secret": "defaultsecret" +// } +// Env : { +// "secret": "somesecretkey" +// } // // The resulting config will have the following values: // @@ -785,7 +785,8 @@ func (v *Viper) searchMapWithPathPrefixes( // isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere // on its path in the map. // e.g., if "foo.bar" has a value in the given map, it “shadows” -// "foo.bar.baz" in a lower-priority map +// +// "foo.bar.baz" in a lower-priority map func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string { var parentVal interface{} for i := 1; i < len(path); i++ { @@ -810,7 +811,8 @@ func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) // isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere // in a sub-path of the map. // e.g., if "foo.bar" has a value in the given map, it “shadows” -// "foo.bar.baz" in a lower-priority map +// +// "foo.bar.baz" in a lower-priority map func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string { // unify input map var m map[string]interface{} @@ -835,7 +837,8 @@ func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string { // isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere // in the environment, when automatic env is on. // e.g., if "foo.bar" has a value in the environment, it “shadows” -// "foo.bar.baz" in a lower-priority map +// +// "foo.bar.baz" in a lower-priority map func (v *Viper) isPathShadowedInAutoEnv(path []string) string { var parentKey string for i := 1; i < len(path); i++ { @@ -856,11 +859,11 @@ func (v *Viper) isPathShadowedInAutoEnv(path []string) string { // would return a string slice for the key if the key's type is inferred by // the default value and the Get function would return: // -// []string {"a", "b", "c"} +// []string {"a", "b", "c"} // // Otherwise the Get function would return: // -// "a b c" +// "a b c" func SetTypeByDefaultValue(enable bool) { v.SetTypeByDefaultValue(enable) } func (v *Viper) SetTypeByDefaultValue(enable bool) { @@ -1137,9 +1140,8 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error { // BindPFlag binds a specific key to a pflag (as used by cobra). // Example (where serverCmd is a Cobra instance): // -// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") -// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) -// +// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") +// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) } func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error { @@ -1972,9 +1974,10 @@ func (v *Viper) AllKeys() []string { // flattenAndMergeMap recursively flattens the given map into a map[string]bool // of key paths (used as a set, easier to manipulate than a []string): -// - each path is merged into a single key string, delimited with v.keyDelim -// - if a path is shadowed by an earlier value in the initial shadow map, -// it is skipped. +// - each path is merged into a single key string, delimited with v.keyDelim +// - if a path is shadowed by an earlier value in the initial shadow map, +// it is skipped. +// // The resulting set of paths is merged to the given shadow set at the same time. func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool { if shadow != nil && prefix != "" && shadow[prefix] { diff --git a/viper_test.go b/viper_test.go index 2a3dabc52e..1325f83adc 100644 --- a/viper_test.go +++ b/viper_test.go @@ -9,7 +9,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "os" "os/exec" "path" @@ -246,7 +245,7 @@ func initDirs(t *testing.T) (string, string, func()) { testDirs = append(testDirs, `d\d`) } - root, err := ioutil.TempDir("", "") + root, err := os.MkdirTemp("", "") require.NoError(t, err, "Failed to create temporary directory") cleanup := true @@ -266,7 +265,7 @@ func initDirs(t *testing.T) (string, string, func()) { err = os.Mkdir(dir, 0o750) assert.Nil(t, err) - err = ioutil.WriteFile( + err = os.WriteFile( path.Join(dir, config+".toml"), []byte("key = \"value is "+dir+"\"\n"), 0o640) @@ -998,7 +997,7 @@ func TestBindPFlags(t *testing.T) { } } -// nolint: dupl +//nolint:dupl func TestBindPFlagsStringSlice(t *testing.T) { tests := []struct { Expected []string @@ -1046,7 +1045,7 @@ func TestBindPFlagsStringSlice(t *testing.T) { } } -// nolint: dupl +//nolint:dupl func TestBindPFlagsStringArray(t *testing.T) { tests := []struct { Expected []string @@ -1094,7 +1093,7 @@ func TestBindPFlagsStringArray(t *testing.T) { } } -// nolint: dupl +//nolint:dupl func TestBindPFlagsIntSlice(t *testing.T) { tests := []struct { Expected []int @@ -1432,7 +1431,7 @@ func TestDirsSearch(t *testing.T) { v.SetConfigName(config) v.SetDefault(`key`, `default`) - entries, err := ioutil.ReadDir(root) + entries, err := os.ReadDir(root) assert.Nil(t, err) for _, e := range entries { if e.IsDir() { @@ -2325,10 +2324,10 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) { } func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) { - watchDir, err := ioutil.TempDir("", "") + watchDir, err := os.MkdirTemp("", "") require.Nil(t, err) configFile := path.Join(watchDir, "config.yaml") - err = ioutil.WriteFile(configFile, []byte("foo: bar\n"), 0o640) + err = os.WriteFile(configFile, []byte("foo: bar\n"), 0o640) require.Nil(t, err) cleanup := func() { os.RemoveAll(watchDir) @@ -2342,14 +2341,14 @@ func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) { } func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func()) { - watchDir, err := ioutil.TempDir("", "") + watchDir, err := os.MkdirTemp("", "") require.Nil(t, err) dataDir1 := path.Join(watchDir, "data1") err = os.Mkdir(dataDir1, 0o777) require.Nil(t, err) realConfigFile := path.Join(dataDir1, "config.yaml") t.Logf("Real config file location: %s\n", realConfigFile) - err = ioutil.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640) + err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640) require.Nil(t, err) cleanup := func() { os.RemoveAll(watchDir) @@ -2393,7 +2392,7 @@ func TestWatchFile(t *testing.T) { }) v.WatchConfig() // when overwriting the file and waiting for the custom change notification handler to be triggered - err = ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0o640) + err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640) wg.Wait() // then the config value should have changed require.Nil(t, err) @@ -2419,7 +2418,7 @@ func TestWatchFile(t *testing.T) { err := os.Mkdir(dataDir2, 0o777) require.Nil(t, err) configFile2 := path.Join(dataDir2, "config.yaml") - err = ioutil.WriteFile(configFile2, []byte("foo: baz\n"), 0o640) + err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640) require.Nil(t, err) // change the symlink using the `ln -sfn` command err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()