Compare commits

...

7 commits

Author SHA1 Message Date
chowyi d0bf36bacc
Merge bca1963cdd into ea35b92596 2024-04-30 02:51:42 +08:00
dependabot[bot] ea35b92596 build(deps): bump github/codeql-action from 3.25.1 to 3.25.2
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.25.1 to 3.25.2.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](c7f9125735...8f596b4ae3)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-04-25 01:40:21 -03:00
dependabot[bot] 3d32668ee5 build(deps): bump actions/checkout from 4.1.3 to 4.1.4
Bumps [actions/checkout](https://github.com/actions/checkout) from 4.1.3 to 4.1.4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](1d96c772d1...0ad4b8fada)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-04-25 01:40:07 -03:00
chowyi bca1963cdd
Merge branch 'spf13:master' into master 2023-07-31 19:50:15 +08:00
chowyi 59e479f6b8 feat: add a new API StopWatching() to stop the groutine created by WatchConfig() manually 2023-07-31 19:47:29 +08:00
chowyi e40c5633a5
Merge branch 'spf13:master' into master 2023-07-27 14:25:39 +08:00
chowyi 6fb91e06ef feat: add a cancel function return from WatchConfig() to stop watching manually 2023-07-07 16:24:44 +08:00
4 changed files with 69 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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) }

View file

@ -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", "")