mirror of
https://github.com/spf13/viper
synced 2024-05-18 02:36:43 +00:00
Compare commits
7 commits
c40d5f58c2
...
d0bf36bacc
Author | SHA1 | Date | |
---|---|---|---|
d0bf36bacc | |||
ea35b92596 | |||
3d32668ee5 | |||
bca1963cdd | |||
59e479f6b8 | |||
e40c5633a5 | |||
6fb91e06ef |
10
.github/workflows/ci.yaml
vendored
10
.github/workflows/ci.yaml
vendored
|
@ -21,7 +21,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
|
@ -49,7 +49,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
|
@ -70,7 +70,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
|
@ -88,7 +88,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
- name: Set up Nix
|
||||
uses: cachix/install-nix-action@8887e596b4ee1134dae06b98d573bd674693f47c # v26
|
||||
|
@ -109,7 +109,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
- name: Dependency Review
|
||||
uses: actions/dependency-review-action@5bbc3ba658137598168acb2ab73b21c432dd411b # v4.2.5
|
||||
|
|
8
.github/workflows/codeql-analysis.yaml
vendored
8
.github/workflows/codeql-analysis.yaml
vendored
|
@ -39,11 +39,11 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
|
||||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@c7f9125735019aa87cfc361530512d50ea439c71 # v3.25.1
|
||||
uses: github/codeql-action/init@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v3.25.2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
|
@ -54,7 +54,7 @@ jobs:
|
|||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@c7f9125735019aa87cfc361530512d50ea439c71 # v3.25.1
|
||||
uses: github/codeql-action/autobuild@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v3.25.2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
@ -68,5 +68,5 @@ jobs:
|
|||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@c7f9125735019aa87cfc361530512d50ea439c71 # v3.25.1
|
||||
uses: github/codeql-action/analyze@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v3.25.2
|
||||
|
||||
|
|
21
viper.go
21
viper.go
|
@ -21,6 +21,7 @@ package viper
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -217,7 +218,8 @@ type Viper struct {
|
|||
aliases map[string]string
|
||||
typeByDefValue bool
|
||||
|
||||
onConfigChange func(fsnotify.Event)
|
||||
onConfigChange func(fsnotify.Event)
|
||||
stopWatchingFunc func()
|
||||
|
||||
logger *slog.Logger
|
||||
|
||||
|
@ -458,6 +460,10 @@ func (v *Viper) WatchConfig() {
|
|||
configDir, _ := filepath.Split(configFile)
|
||||
realConfigFile, _ := filepath.EvalSymlinks(filename)
|
||||
|
||||
// init the stopWatchingFunc
|
||||
watchingCtx, cancel := context.WithCancel(context.Background())
|
||||
v.stopWatchingFunc = cancel
|
||||
|
||||
eventsWG := sync.WaitGroup{}
|
||||
eventsWG.Add(1)
|
||||
go func() {
|
||||
|
@ -494,6 +500,9 @@ func (v *Viper) WatchConfig() {
|
|||
}
|
||||
eventsWG.Done()
|
||||
return
|
||||
case <-watchingCtx.Done(): // StopWatching function called
|
||||
eventsWG.Done()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@ -504,6 +513,16 @@ func (v *Viper) WatchConfig() {
|
|||
initWG.Wait() // make sure that the go routine above fully ended before returning
|
||||
}
|
||||
|
||||
// StopWatching stop watching a config file for changes.
|
||||
func StopWatching() { v.StopWatching() }
|
||||
|
||||
// StopWatching stop watching a config file for changes.
|
||||
func (v *Viper) StopWatching() {
|
||||
if v.stopWatchingFunc != nil {
|
||||
v.stopWatchingFunc()
|
||||
}
|
||||
}
|
||||
|
||||
// SetConfigFile explicitly defines the path, name and extension of the config file.
|
||||
// Viper will use this and not check any of the config paths.
|
||||
func SetConfigFile(in string) { v.SetConfigFile(in) }
|
||||
|
|
|
@ -2489,6 +2489,46 @@ func TestWatchFile(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestStopWatching(t *testing.T) {
|
||||
t.Run(
|
||||
"file content changed after stop watching", func(t *testing.T) {
|
||||
// given a `config.yaml` file being watched
|
||||
v, configFile, cleanup := newViperWithConfigFile(t)
|
||||
defer cleanup()
|
||||
_, err := os.Stat(configFile)
|
||||
require.NoError(t, err)
|
||||
t.Logf("test config file: %s\n", configFile)
|
||||
|
||||
v.WatchConfig()
|
||||
v.StopWatching()
|
||||
|
||||
// overwriting the file after StopWatching called
|
||||
err = ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
|
||||
time.Sleep(time.Second) // wait for file changed event
|
||||
// then the config value should not be changed
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "bar", v.Get("foo"))
|
||||
|
||||
// watch again
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
var wgDoneOnce sync.Once // OnConfigChange is called twice on Windows
|
||||
v.OnConfigChange(
|
||||
func(in fsnotify.Event) {
|
||||
t.Logf("config file changed again")
|
||||
wgDoneOnce.Do(func() { wg.Done() })
|
||||
},
|
||||
)
|
||||
v.WatchConfig()
|
||||
// overwriting the file after StopWatching and Watch again
|
||||
err = ioutil.WriteFile(configFile, []byte("foo: qux\n"), 0o640)
|
||||
wg.Wait()
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "qux", v.Get("foo"))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) {
|
||||
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
flags.String("foo.bar", "cobra_flag", "")
|
||||
|
|
Loading…
Reference in a new issue