Skip to content

Commit

Permalink
Add ContinueOnError (#161)
Browse files Browse the repository at this point in the history
so watcher can continue watching even the loader fails to load the
configuration
  • Loading branch information
ktong authored Feb 22, 2024
1 parent 3145592 commit a966b2b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 23 deletions.
28 changes: 10 additions & 18 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,16 @@ jobs:
name: "v${{ steps.query-release-info.outputs.version }}",
body: `${{ steps.query-release-info.outputs.release-notes }}`
})
tag:
strategy:
matrix:
module: [ 'provider/file', 'provider/pflag', 'provider/appconfig', 'provider/azappconfig', 'provider/secretmanager' ]
name: Submodules
needs: release
if: ${{ needs.release.steps.create-release.outcome == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Create tag
- name: Tag Submodules
uses: actions/github-script@v7
with:
script: |
ref = "refs/tags/${{ matrix.module }}/v${{ needs.release.steps.query-release-info.outputs.version }}"
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: ref,
sha: context.sha
})
const modules = [ 'provider/file', 'provider/pflag', 'provider/appconfig', 'provider/azappconfig', 'provider/secretmanager' ]
for (const module of modules) {
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/${{ module }}/v${{ steps.query-release-info.outputs.version }}",
sha: context.sha
})
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.6.2] - 2024-02-21

### Fixed

- Add ContinueOnError so watcher can continue watching even the loader fails to load the configuration (#161).

## [0.6.1] - 2024-02-21

### Changed

- merge loader into providers even it fails the loading. Developers can ignore the loading error
and wait for the watching to get latest configuration (#159).

Expand Down
18 changes: 13 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,25 @@ func New(opts ...Option) Config {
//
// This method can be called multiple times but it is not concurrency-safe.
// It panics if loader is nil.
func (c Config) Load(loader Loader) error {
func (c Config) Load(loader Loader, opts ...LoadOption) error {
if loader == nil {
panic("cannot load config from nil loader")
}

loadOption := &loadOptions{}
for _, opt := range opts {
opt(loadOption)
}

values, err := loader.Load()
if err != nil {
if !loadOption.continueOnError {
return fmt.Errorf("load configuration: %w", err)
}
c.logger.Warn("failed to load configuration", "loader", loader, "error", err)
}
maps.Merge(c.values, values)

// Merged to empty map to convert to lower case.
providerValues := make(map[string]any)
maps.Merge(providerValues, values)
Expand All @@ -97,10 +109,6 @@ func (c Config) Load(loader Loader) error {
values: providerValues,
})

if err != nil {
return fmt.Errorf("load configuration: %w", err)
}

return nil
}

Expand Down
35 changes: 35 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@ import (
"github.com/nil-go/konf/provider/env"
)

func TestConfig_Load_error(t *testing.T) {
t.Parallel()

testcases := []struct {
description string
opts []konf.LoadOption
err string
}{
{
description: "error",
err: "load configuration: load error",
},
{
description: "continue on error",
opts: []konf.LoadOption{konf.ContinueOnError()},
},
}

for _, testcase := range testcases {
testcase := testcase

t.Run(testcase.description, func(t *testing.T) {
t.Parallel()

config := konf.New()
err := config.Load(&errorLoader{}, testcase.opts...)
if testcase.err == "" {
assert.NoError(t, err)
} else {
assert.Equal(t, testcase.err, err.Error())
}
})
}
}

func TestConfig_Load_panic(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/nil-go/konf
go 1.21

require github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1

retract v0.6.1 // It does not work while file is not exist
14 changes: 14 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ type (
options Config
)

func ContinueOnError() LoadOption {
return func(options *loadOptions) {
options.continueOnError = true
}
}

type (
// LoadOption configures Config.Load with specific options.
LoadOption func(*loadOptions)
loadOptions struct {
continueOnError bool
}
)

// WithValueFormatter provides the value formatter for Config.Explain.
// It's for hiding sensitive information (e.g. password, secret) which should not be exposed.
//
Expand Down

0 comments on commit a966b2b

Please sign in to comment.