From 36be6bf91f8c02e8fd53f87205ad230706a3eea9 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 22 Apr 2021 11:35:02 +0200 Subject: [PATCH] feat: make sure Viper compiles on WASM fsnotify is not available on WASM, so config watching is not going to work. Signed-off-by: Mark Sagi-Kazar --- .github/workflows/wasm.yml | 26 ++++++++++++++++++++++++++ viper.go | 2 +- watch.go | 11 +++++++++++ watch_wasm.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/wasm.yml create mode 100644 watch.go create mode 100644 watch_wasm.go diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml new file mode 100644 index 0000000..333eb69 --- /dev/null +++ b/.github/workflows/wasm.yml @@ -0,0 +1,26 @@ +name: WASM + +on: + push: + branches: + - master + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-latest + env: + GOFLAGS: -mod=readonly + + steps: + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.16' + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Ensure Viper compiles for WASM + run: GOOS=js GOARCH=wasm go build . diff --git a/viper.go b/viper.go index 97d5b84..50b4780 100644 --- a/viper.go +++ b/viper.go @@ -344,7 +344,7 @@ func (v *Viper) WatchConfig() { initWG := sync.WaitGroup{} initWG.Add(1) go func() { - watcher, err := fsnotify.NewWatcher() + watcher, err := newWatcher() if err != nil { log.Fatal(err) } diff --git a/watch.go b/watch.go new file mode 100644 index 0000000..c433a8f --- /dev/null +++ b/watch.go @@ -0,0 +1,11 @@ +// +build !js + +package viper + +import "github.com/fsnotify/fsnotify" + +type watcher = fsnotify.Watcher + +func newWatcher() (*watcher, error) { + return fsnotify.NewWatcher() +} diff --git a/watch_wasm.go b/watch_wasm.go new file mode 100644 index 0000000..8e47e6a --- /dev/null +++ b/watch_wasm.go @@ -0,0 +1,30 @@ +// +build js,wasm + +package viper + +import ( + "errors" + + "github.com/fsnotify/fsnotify" +) + +type watcher struct { + Events chan fsnotify.Event + Errors chan error +} + +func (*watcher) Close() error { + return nil +} + +func (*watcher) Add(name string) error { + return nil +} + +func (*watcher) Remove(name string) error { + return nil +} + +func newWatcher() (*watcher, error) { + return &watcher{}, errors.New("fsnotify is not supported on WASM") +}