diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..1f664d13a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.go] +indent_style = tab + +[{Makefile,*.mk}] +indent_style = tab + +[*.nix] +indent_size = 2 diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..2e0f9f5f7 --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi +use flake . --impure diff --git a/.github/.editorconfig b/.github/.editorconfig new file mode 100644 index 000000000..0902c6ae1 --- /dev/null +++ b/.github/.editorconfig @@ -0,0 +1,2 @@ +[{*.yml,*.yaml}] +indent_size = 2 diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml new file mode 100644 index 000000000..db00ae685 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -0,0 +1,119 @@ +name: 🐛 Bug report +description: Report a bug to help us improve Viper +labels: [kind/bug] +body: +- type: markdown + attributes: + value: | + Thank you for submitting a bug report! + + Please fill out the template below to make it easier to debug your problem. + + If you are not sure if it is a bug or not, you can contact us via the available [support channels](https://github.com/spf13/viper/issues/new/choose). +- type: checkboxes + attributes: + label: Preflight Checklist + description: Please ensure you've completed all of the following. + options: + - label: I have searched the [issue tracker](https://www.github.com/spf13/viper/issues) for an issue that matches the one I want to file, without success. + required: true + - label: I am not looking for support or already pursued the available [support channels](https://github.com/spf13/viper/issues/new/choose) without success. + required: true + - label: I have checked the [troubleshooting guide](https://github.com/spf13/viper/blob/master/TROUBLESHOOTING.md) for my problem, without success. + required: true +- type: input + attributes: + label: Viper Version + description: What version of Viper are you using? + placeholder: 1.8.1 + validations: + required: true +- type: input + attributes: + label: Go Version + description: What version of Go are you using? + placeholder: "1.16" + validations: + required: true +- type: dropdown + attributes: + label: Config Source + description: What sources do you load configuration from? + options: + - Manual set + - Flags + - Environment variables + - Files + - Remove K/V stores + - Defaults + multiple: true + validations: + required: true +- type: dropdown + attributes: + label: Format + description: Which file formats do you use? + options: + - JSON + - YAML + - TOML + - Dotenv + - HCL + - Java properties + - INI + - Other (specify below) + multiple: true +- type: input + attributes: + label: Repl.it link + description: Complete example on Repl.it reproducing the issue. [Here](https://repl.it/@sagikazarmark/Viper-example) is an example you can use. + placeholder: https://repl.it/@sagikazarmark/Viper-example +- type: textarea + attributes: + label: Code reproducing the issue + description: Please provide a Repl.it link if possible. + render: go + placeholder: | + package main + + import ( + "github.com/spf13/viper" + ) + + func main() { + v := viper.New() + + // ... + + var config Config + + err = v.Unmarshal(&config) + if err != nil { + panic(err) + } + } +- type: textarea + attributes: + label: Expected Behavior + description: A clear and concise description of what you expected to happen. + validations: + required: true +- type: textarea + attributes: + label: Actual Behavior + description: A clear description of what actually happens. + validations: + required: true +- type: textarea + attributes: + label: Steps To Reproduce + description: Steps to reproduce the behavior if it is not self-explanatory. + placeholder: | + 1. In this environment... + 2. With this config... + 3. Run '...' + 4. See error... +- type: textarea + attributes: + label: Additional Information + description: Links? References? Anything that will give us more context about the issue that you are encountering! diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 000000000..cd537e839 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,13 @@ +blank_issues_enabled: false +contact_links: + - name: ❓ Ask a question + url: https://github.com/spf13/viper/discussions/new?category=q-a + about: Ask and discuss questions with other Viper community members + + - name: 📓 Reference + url: https://pkg.go.dev/mod/github.com/spf13/viper + about: Check the Go code reference + + - name: 💬 Slack channel + url: https://gophers.slack.com/messages/viper + about: Please ask and answer questions here diff --git a/.github/ISSUE_TEMPLATE/feature_request.yaml b/.github/ISSUE_TEMPLATE/feature_request.yaml new file mode 100644 index 000000000..c84aeb276 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yaml @@ -0,0 +1,39 @@ +name: 🎉 Feature request +description: Suggest an idea for Viper +labels: [kind/enhancement] +body: +- type: markdown + attributes: + value: | + Thank you for submitting a feature request! + + Please describe what you would like to change/add and why in detail by filling out the template below. + + If you are not sure if your request fits into Viper, you can contact us via the available [support channels](https://github.com/spf13/viper/issues/new/choose). +- type: checkboxes + attributes: + label: Preflight Checklist + description: Please ensure you've completed all of the following. + options: + - label: I have searched the [issue tracker](https://www.github.com/spf13/viper/issues) for an issue that matches the one I want to file, without success. + required: true +- type: textarea + attributes: + label: Problem Description + description: A clear and concise description of the problem you are seeking to solve with this feature request. + validations: + required: true +- type: textarea + attributes: + label: Proposed Solution + description: A clear and concise description of what would you like to happen. + validations: + required: true +- type: textarea + attributes: + label: Alternatives Considered + description: A clear and concise description of any alternative solutions or features you've considered. +- type: textarea + attributes: + label: Additional Information + description: Add any other context about the problem here. diff --git a/.github/PULL_REQUEST_TEMPLATES.md b/.github/PULL_REQUEST_TEMPLATES.md new file mode 100644 index 000000000..fb6ea68ce --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATES.md @@ -0,0 +1,20 @@ + + +**Overview**: + + +**What problem does it solve?**: + + +**Special notes for a reviewer**: diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml new file mode 100644 index 000000000..2f85879b1 --- /dev/null +++ b/.github/dependabot.yaml @@ -0,0 +1,16 @@ +version: 2 + +updates: + - package-ecosystem: gomod + directory: / + labels: + - area/dependencies + schedule: + interval: daily + + - package-ecosystem: github-actions + directory: / + labels: + - area/dependencies + schedule: + interval: daily diff --git a/.github/logo.png b/.github/logo.png new file mode 100644 index 000000000..af7587105 Binary files /dev/null and b/.github/logo.png differ diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 000000000..e8353e877 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,30 @@ +changelog: + exclude: + labels: + - release-note/ignore + categories: + - title: Exciting New Features 🎉 + labels: + - kind/feature + - release-note/new-feature + - title: Enhancements 🚀 + labels: + - kind/enhancement + - release-note/enhancement + - title: Bug Fixes 🐛 + labels: + - kind/bug + - release-note/bug-fix + - title: Breaking Changes 🛠 + labels: + - release-note/breaking-change + - title: Deprecations ❌ + labels: + - release-note/deprecation + - title: Dependency Updates ⬆️ + labels: + - area/dependencies + - release-note/dependency-update + - title: Other Changes + labels: + - "*" diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml new file mode 100644 index 000000000..3faf18299 --- /dev/null +++ b/.github/workflows/checks.yaml @@ -0,0 +1,30 @@ +name: PR Checks + +on: + pull_request: + types: [opened, labeled, unlabeled, synchronize] + +jobs: + release-label: + name: Release note label + runs-on: ubuntu-latest + + steps: + - name: Check minimum labels + uses: mheap/github-action-required-labels@5847eef68201219cf0a4643ea7be61e77837bbce # v5.4.1 + with: + mode: minimum + count: 1 + labels: | + release-note/ignore + kind/feature + release-note/new-feature + kind/enhancement + release-note/enhancement + kind/bug + release-note/bug-fix + release-note/breaking-change + release-note/deprecation + area/dependencies + release-note/dependency-update + release-note/misc diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 000000000..23727dda6 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,115 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + include: + - goos: js + goarch: wasm + - goos: aix + goarch: ppc64 + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Set up Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 + with: + go-version: "1.23" + + - name: Build + run: go build . + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + + test: + name: Test + runs-on: ${{ matrix.os }} + + strategy: + # Fail fast is disabled because there are Go version specific features and tests + # that should be able to fail independently. + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + go: ["1.21", "1.22", "1.23"] + tags: ["", "viper_finder", "viper_bind_struct"] + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Set up Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 + with: + go-version: ${{ matrix.go }} + + - name: Test + run: go test -race -v -tags '${{ matrix.tags }}' -shuffle=on ./... + if: runner.os != 'Windows' + + - name: Test (without race detector) + run: go test -v -tags '${{ matrix.tags }}' -shuffle=on ./... + if: runner.os == 'Windows' + + lint: + name: Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Set up Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 + with: + go-version: "1.23" + + - name: Lint + uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 + with: + version: v1.60.3 + + dev: + name: Developer environment + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Set up Nix + uses: cachix/install-nix-action@ba0dd844c9180cbf77aa72a116d6fbc515d0e87b # v27 + with: + extra_nix_config: | + access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} + + - name: Check + run: nix flake check --impure + + - name: Dev shell + run: nix develop --impure + + dependency-review: + name: Dependency review + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Dependency Review + uses: actions/dependency-review-action@5a2ce3f5b92ee19cbb1541a4984c76d921601d7c # v4.3.4 diff --git a/.github/workflows/codeql-analysis.yaml b/.github/workflows/codeql-analysis.yaml new file mode 100644 index 000000000..d5ea636d2 --- /dev/null +++ b/.github/workflows/codeql-analysis.yaml @@ -0,0 +1,72 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '22 16 * * 3' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@4dd16135b69a43b6c8efb853346f8437d92d3c93 # v3.26.6 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # 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@4dd16135b69a43b6c8efb853346f8437d92d3c93 # v3.26.6 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@4dd16135b69a43b6c8efb853346f8437d92d3c93 # v3.26.6 + diff --git a/.github/workflows/feedback_issue.yaml b/.github/workflows/feedback_issue.yaml new file mode 100644 index 000000000..a3fb22832 --- /dev/null +++ b/.github/workflows/feedback_issue.yaml @@ -0,0 +1,32 @@ +on: + issues: + types: [opened] + +jobs: + comment: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `👋 Thanks for reporting! + + A maintainer will take a look at your issue shortly. 👀 + + In the meantime: We are working on **Viper v2** and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. + + ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 + + 📣 If you've already given us your feedback, you can still help by spreading the news, + either by sharing the above link or telling people about this on Twitter: + + https://twitter.com/sagikazarmark/status/1306904078967074816 + + **Thank you!** ❤️ + `, + }) diff --git a/.github/workflows/feedback_pull_request.yaml b/.github/workflows/feedback_pull_request.yaml new file mode 100644 index 000000000..82e85c85e --- /dev/null +++ b/.github/workflows/feedback_pull_request.yaml @@ -0,0 +1,32 @@ +on: + pull_request_target: + types: [opened] + +jobs: + comment: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `👋 Thanks for contributing to Viper! You are awesome! 🎉 + + A maintainer will take a look at your pull request shortly. 👀 + + In the meantime: We are working on **Viper v2** and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. + + ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 + + 📣 If you've already given us your feedback, you can still help by spreading the news, + either by sharing the above link or telling people about this on Twitter: + + https://twitter.com/sagikazarmark/status/1306904078967074816 + + **Thank you!** ❤️ + `, + }) diff --git a/.gitignore b/.gitignore index 352a34a56..f1bbd4280 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1,8 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.bench \ No newline at end of file +/.devenv/ +/.direnv/ +/.idea/ +/.pre-commit-config.yaml +/bin/ +/build/ +/var/ +/vendor/ diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 000000000..1faeae42c --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,108 @@ +run: + timeout: 5m + +linters-settings: + gci: + sections: + - standard + - default + - prefix(github.com/spf13/viper) + gocritic: + # Enable multiple checks by tags. See "Tags" section in https://github.com/go-critic/go-critic#usage. + enabled-tags: + - diagnostic + - experimental + - opinionated + - style + disabled-checks: + - importShadow + - unnamedResult + golint: + min-confidence: 0 + goimports: + local-prefixes: github.com/spf13/viper + +linters: + disable-all: true + enable: + - bodyclose + - dogsled + - dupl + - durationcheck + - exhaustive + - exportloopref + - gci + - gocritic + - godot + - gofmt + - gofumpt + - goimports + - gomoddirectives + - goprintffuncname + - govet + - importas + - ineffassign + - makezero + - misspell + - nakedret + - nilerr + - noctx + - nolintlint + - prealloc + - predeclared + - revive + - rowserrcheck + - sqlclosecheck + - staticcheck + - stylecheck + - tparallel + - typecheck + - unconvert + - unparam + - unused + - wastedassign + - whitespace + + # fixme + # - cyclop + # - errcheck + # - errorlint + # - exhaustivestruct + # - forbidigo + # - forcetypeassert + # - gochecknoglobals + # - gochecknoinits + # - gocognit + # - goconst + # - gocyclo + # - gosec + # - gosimple + # - ifshort + # - lll + # - nlreturn + # - paralleltest + # - scopelint + # - thelper + # - wrapcheck + + # unused + # - depguard + # - goheader + # - gomodguard + + # deprecated + # - deadcode + # - structcheck + # - varcheck + + # don't enable: + # - asciicheck + # - funlen + # - godox + # - goerr113 + # - gomnd + # - interfacer + # - maligned + # - nestif + # - testpackage + # - wsl diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d4c2559c2..000000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -go_import_path: github.com/spf13/viper - -language: go -go: - - 1.7.5 - - 1.8 - - tip - -os: - - linux - - osx - -matrix: - allow_failures: - - go: tip - fast_finish: true - -script: - - go install ./... - - go test -v ./... - -after_success: - - go get -u -d github.com/spf13/hugo - - cd $GOPATH/src/github.com/spf13/hugo && make && ./hugo -s docs && cd - - -sudo: false diff --git a/.yamlignore b/.yamlignore new file mode 100644 index 000000000..c04c4dead --- /dev/null +++ b/.yamlignore @@ -0,0 +1,2 @@ +# TODO: FIXME +/.github/ diff --git a/.yamllint.yaml b/.yamllint.yaml new file mode 100644 index 000000000..bac19ce18 --- /dev/null +++ b/.yamllint.yaml @@ -0,0 +1,6 @@ +ignore-from-file: [.gitignore, .yamlignore] + +extends: default + +rules: + line-length: disable diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..a77b9c81c --- /dev/null +++ b/Makefile @@ -0,0 +1,87 @@ +# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html + +OS = $(shell uname | tr A-Z a-z) +export PATH := $(abspath bin/):${PATH} + +# Build variables +BUILD_DIR ?= build +export CGO_ENABLED ?= 0 +export GOOS = $(shell go env GOOS) +ifeq (${VERBOSE}, 1) +ifeq ($(filter -v,${GOARGS}),) + GOARGS += -v +endif +TEST_FORMAT = short-verbose +endif + +# Dependency versions +GOTESTSUM_VERSION = 1.9.0 +GOLANGCI_VERSION = 1.53.3 + +# Add the ability to override some variables +# Use with care +-include override.mk + +.PHONY: clear +clear: ## Clear the working area and the project + rm -rf bin/ + +.PHONY: check +check: test lint ## Run tests and linters + + +TEST_PKGS ?= ./... +.PHONY: test +test: TEST_FORMAT ?= short +test: SHELL = /bin/bash +test: export CGO_ENABLED=1 +test: bin/gotestsum ## Run tests + @mkdir -p ${BUILD_DIR} + bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/coverage.xml --format ${TEST_FORMAT} -- -race -coverprofile=${BUILD_DIR}/coverage.txt -covermode=atomic $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...) + +.PHONY: lint +lint: lint-go lint-yaml +lint: ## Run linters + +.PHONY: lint-go +lint-go: + golangci-lint run $(if ${CI},--out-format github-actions,) + +.PHONY: lint-yaml +lint-yaml: + yamllint $(if ${CI},-f github,) --no-warnings . + +.PHONY: fmt +fmt: ## Format code + golangci-lint run --fix + +deps: bin/golangci-lint bin/gotestsum yamllint +deps: ## Install dependencies + +bin/gotestsum: + @mkdir -p bin + curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum && chmod +x ./bin/gotestsum + +bin/golangci-lint: + @mkdir -p bin + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_VERSION} + +.PHONY: yamllint +yamllint: + pip3 install --user yamllint + +# Add custom targets here +-include custom.mk + +.PHONY: list +list: ## List all make targets + @${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort + +.PHONY: help +.DEFAULT_GOAL := help +help: + @grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +# Variable outputting/exporting rules +var-%: ; @echo $($*) +varexport-%: ; @echo $*=$($*) diff --git a/README.md b/README.md index 25181dff1..c2b2237c2 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,55 @@ -![viper logo](https://cloud.githubusercontent.com/assets/173412/10886745/998df88a-8151-11e5-9448-4736db51020d.png) +> ## Viper v2 feedback +> Viper is heading towards v2 and we would love to hear what _**you**_ would like to see in it. Share your thoughts here: https://forms.gle/R6faU74qPRPAzchZ9 +> +> **Thank you!** -Go configuration with fangs! +![Viper](.github/logo.png?raw=true) + +[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go#configuration) +[![run on repl.it](https://repl.it/badge/github/sagikazarmark/Viper-example)](https://repl.it/@sagikazarmark/Viper-example#main.go) + +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/viper/ci.yaml?branch=master&style=flat-square)](https://github.com/spf13/viper/actions?query=workflow%3ACI) +[![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/viper?style=flat-square)](https://goreportcard.com/report/github.com/spf13/viper) +![Go Version](https://img.shields.io/badge/go%20version-%3E=1.21-61CFDD.svg?style=flat-square) +[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/spf13/viper)](https://pkg.go.dev/mod/github.com/spf13/viper) + +**Go configuration with fangs!** Many Go projects are built using Viper including: * [Hugo](http://gohugo.io) * [EMC RexRay](http://rexray.readthedocs.org/en/stable/) -* [Imgur's Incus](https://github.com/Imgur/incus) +* [Imgur’s Incus](https://github.com/Imgur/incus) * [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack) * [Docker Notary](https://github.com/docker/Notary) * [BloomApi](https://www.bloomapi.com/) * [doctl](https://github.com/digitalocean/doctl) +* [Clairctl](https://github.com/jgsqware/clairctl) +* [Mercure](https://mercure.rocks) +* [Meshery](https://github.com/meshery/meshery) +* [Bearer](https://github.com/bearer/bearer) +* [Coder](https://github.com/coder/coder) +* [Vitess](https://vitess.io/) -[![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper) + +## Install + +```shell +go get github.com/spf13/viper +``` + +**Note:** Viper uses [Go Modules](https://go.dev/wiki/Modules) to manage dependencies. ## What is Viper? -Viper is a complete configuration solution for go applications including 12 factor apps. It is designed -to work within an application, and can handle all types of configuration needs +Viper is a complete configuration solution for Go applications including [12-Factor apps](https://12factor.net/#the_twelve_factors). +It is designed to work within an application, and can handle all types of configuration needs and formats. It supports: * setting defaults -* reading from JSON, TOML, YAML, HCL, and Java properties config files +* reading from JSON, TOML, YAML, HCL, envfile and Java properties config files * live watching and re-reading of config files (optional) * reading from environment variables * reading from remote config systems (etcd or Consul), and watching changes @@ -30,8 +57,8 @@ and formats. It supports: * reading from buffer * setting explicit values -Viper can be thought of as a registry for all of your applications -configuration needs. +Viper can be thought of as a registry for all of your applications configuration needs. + ## Why Viper? @@ -41,34 +68,31 @@ Viper is here to help with that. Viper does the following for you: -1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats. -2. Provide a mechanism to set default values for your different - configuration options. -3. Provide a mechanism to set override values for options specified through - command line flags. -4. Provide an alias system to easily rename parameters without breaking existing - code. -5. Make it easy to tell the difference between when a user has provided a - command line or config file which is the same as the default. +1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, INI, envfile or Java properties formats. +2. Provide a mechanism to set default values for your different configuration options. +3. Provide a mechanism to set override values for options specified through command line flags. +4. Provide an alias system to easily rename parameters without breaking existing code. +5. Make it easy to tell the difference between when a user has provided a command line or config file which is the same as the default. -Viper uses the following precedence order. Each item takes precedence over the -item below it: +Viper uses the following precedence order. Each item takes precedence over the item below it: - * explicit call to Set + * explicit call to `Set` * flag * env * config * key/value store * default -Viper configuration keys are case insensitive. +**Important:** Viper configuration keys are case insensitive. +There are ongoing discussions about making that optional. + ## Putting Values into Viper ### Establishing Defaults A good configuration system will support default values. A default value is not -required for a key, but it's useful in the event that a key hasn’t been set via +required for a key, but it’s useful in the event that a key hasn't been set via config file, environment variable, remote configuration or flag. Examples: @@ -82,7 +106,7 @@ viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "cat ### Reading Config Files Viper requires minimal configuration so it knows where to look for config files. -Viper supports JSON, TOML, YAML, HCL, and Java Properties files. Viper can search multiple paths, but +Viper supports JSON, TOML, YAML, HCL, INI, envfile and Java Properties files. Viper can search multiple paths, but currently a single Viper instance only supports a single configuration file. Viper does not default to any configuration search paths leaving defaults decision to an application. @@ -93,13 +117,52 @@ where a configuration file is expected. ```go viper.SetConfigName("config") // name of config file (without extension) +viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath("/etc/appname/") // path to look for the config file in viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths viper.AddConfigPath(".") // optionally look for config in the working directory err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file - panic(fmt.Errorf("Fatal error config file: %s \n", err)) + panic(fmt.Errorf("fatal error config file: %w", err)) +} +``` + +You can handle the specific case where no config file is found like this: + +```go +if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // Config file not found; ignore error if desired + } else { + // Config file was found but another error was produced + } } + +// Config file found and successfully parsed +``` + +*NOTE [since 1.6]:* You can also have a file without an extension and specify the format programmatically. For those configuration files that lie in the home of the user without any extension like `.bashrc` + +### Writing Config Files + +Reading from config files is useful, but at times you want to store all modifications made at run time. +For that, a bunch of commands are available, each with its own purpose: + +* WriteConfig - writes the current viper configuration to the predefined path, if exists. Errors if no predefined path. Will overwrite the current config file, if it exists. +* SafeWriteConfig - writes the current viper configuration to the predefined path. Errors if no predefined path. Will not overwrite the current config file, if it exists. +* WriteConfigAs - writes the current viper configuration to the given filepath. Will overwrite the given file, if it exists. +* SafeWriteConfigAs - writes the current viper configuration to the given filepath. Will not overwrite the given file, if it exists. + +As a rule of the thumb, everything marked with safe won't overwrite any file, but just create if not existent, whilst the default behavior is to create or truncate. + +A small examples section: + +```go +viper.WriteConfig() // writes current config to predefined path set by 'viper.AddConfigPath()' and 'viper.SetConfigName' +viper.SafeWriteConfig() +viper.WriteConfigAs("/path/to/my/.config") +viper.SafeWriteConfigAs("/path/to/my/.config") // will error since it has already been written +viper.SafeWriteConfigAs("/path/to/my/.other_config") ``` ### Watching and re-reading config files @@ -116,10 +179,10 @@ Optionally you can provide a function for Viper to run each time a change occurs **Make sure you add all of the configPaths prior to calling `WatchConfig()`** ```go - viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - fmt.Println("Config file changed:", e.Name) - }) +viper.OnConfigChange(func(e fsnotify.Event) { + fmt.Println("Config file changed:", e.Name) +}) +viper.WatchConfig() ``` ### Reading Config from io.Reader @@ -159,6 +222,7 @@ These could be from a command line flag, or from your own application logic. ```go viper.Set("Verbose", true) viper.Set("LogFile", LogFile) +viper.Set("host.port", 5899) // set subset ``` ### Registering and Using Aliases @@ -178,28 +242,30 @@ viper.GetBool("verbose") // true ### Working with Environment Variables Viper has full support for environment variables. This enables 12 factor -applications out of the box. There are four methods that exist to aid working +applications out of the box. There are five methods that exist to aid working with ENV: * `AutomaticEnv()` * `BindEnv(string...) : error` * `SetEnvPrefix(string)` - * `SetEnvReplacer(string...) *strings.Replacer` + * `SetEnvKeyReplacer(string...) *strings.Replacer` + * `AllowEmptyEnv(bool)` _When working with ENV variables, it’s important to recognize that Viper treats ENV variables as case sensitive._ Viper provides a mechanism to try to ensure that ENV variables are unique. By -using `SetEnvPrefix`, you can tell Viper to use add a prefix while reading from +using `SetEnvPrefix`, you can tell Viper to use a prefix while reading from the environment variables. Both `BindEnv` and `AutomaticEnv` will use this prefix. -`BindEnv` takes one or two parameters. The first parameter is the key name, the -second is the name of the environment variable. The name of the environment -variable is case sensitive. If the ENV variable name is not provided, then -Viper will automatically assume that the key name matches the ENV variable name, -but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV -variable name, it **does not** automatically add the prefix. +`BindEnv` takes one or more parameters. The first parameter is the key name, the +rest are the name of the environment variables to bind to this key. If more than +one are provided, they will take precedence in the specified order. The name of +the environment variable is case sensitive. If the ENV variable name is not provided, then +Viper will automatically assume that the ENV variable matches the following format: prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV variable name (the second parameter), +it **does not** automatically add the prefix. For example if the second parameter is "id", +Viper will look for the ENV variable "ID". One important thing to recognize when working with ENV variables is that the value will be read each time it is accessed. Viper does not fix the value when @@ -208,14 +274,21 @@ the `BindEnv` is called. `AutomaticEnv` is a powerful helper especially when combined with `SetEnvPrefix`. When called, Viper will check for an environment variable any time a `viper.Get` request is made. It will apply the following rules. It will -check for a environment variable with a name matching the key uppercased and +check for an environment variable with a name matching the key uppercased and prefixed with the `EnvPrefix` if set. -`SetEnvReplacer` allows you to use a `strings.Replacer` object to rewrite Env +`SetEnvKeyReplacer` allows you to use a `strings.Replacer` object to rewrite Env keys to an extent. This is useful if you want to use `-` or something in your `Get()` calls, but want your environmental variables to use `_` delimiters. An example of using it can be found in `viper_test.go`. +Alternatively, you can use `EnvKeyReplacer` with `NewWithOptions` factory function. +Unlike `SetEnvKeyReplacer`, it accepts a `StringReplacer` interface allowing you to write custom string replacing logic. + +By default empty environment variables are considered unset and will fall back to +the next configuration source. To treat empty environment variables as set, use +the `AllowEmptyEnv` method. + #### Env example ```go @@ -236,7 +309,7 @@ Like `BindEnv`, the value is not set when the binding method is called, but when it is accessed. This means you can bind as early as you want, even in an `init()` function. -The `BindPFlag()` method provides this functionality. +For individual flags, the `BindPFlag()` method provides this functionality. Example: @@ -245,6 +318,19 @@ serverCmd.Flags().Int("port", 1138, "Port to run Application server on") viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) ``` +You can also bind an existing set of pflags (pflag.FlagSet): + +Example: + +```go +pflag.Int("flagname", 1234, "help message for flagname") + +pflag.Parse() +viper.BindPFlags(pflag.CommandLine) + +i := viper.GetInt("flagname") // retrieve values from viper instead of pflag +``` + The use of [pflag](https://github.com/spf13/pflag/) in Viper does not preclude the use of other packages that use the [flag](https://golang.org/pkg/flag/) package from the standard library. The pflag package can handle the flags @@ -263,15 +349,23 @@ import ( ) func main() { + + // using standard library "flag" package + flag.Int("flagname", 1234, "help message for flagname") + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - ... + viper.BindPFlags(pflag.CommandLine) + + i := viper.GetInt("flagname") // retrieve value from viper + + // ... } ``` #### Flag interfaces -Viper provides two Go interfaces to bind other flag systems if you don't use `Pflags`. +Viper provides two Go interfaces to bind other flag systems if you don’t use `Pflags`. `FlagValue` represents a single flag. This is a very simple example on how to implement this interface: @@ -319,12 +413,14 @@ package: `import _ "github.com/spf13/viper/remote"` -Viper will read a config string (as JSON, TOML, YAML or HCL) retrieved from a path +Viper will read a config string (as JSON, TOML, YAML, HCL or envfile) retrieved from a path in a Key/Value store such as etcd or Consul. These values take precedence over default values, but are overridden by configuration values retrieved from disk, flags, or environment variables. -Viper uses [crypt](https://github.com/xordataexchange/crypt) to retrieve +Viper supports multiple hosts. To use, pass a list of endpoints separated by `;`. For example `http://127.0.0.1:4001;http://127.0.0.1:4002`. + +Viper uses [crypt](https://github.com/sagikazarmark/crypt) to retrieve configuration from the K/V store, which means that you can store your configuration values encrypted and have them automatically decrypted if you have the correct gpg keyring. Encryption is optional. @@ -336,7 +432,7 @@ independently of it. K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001. ```bash -$ go get github.com/xordataexchange/crypt/bin/crypt +$ go get github.com/sagikazarmark/crypt/bin/crypt $ crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json ``` @@ -351,9 +447,56 @@ how to use Consul. ### Remote Key/Value Store Example - Unencrypted +#### etcd ```go viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") -viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" +err := viper.ReadRemoteConfig() +``` + +#### etcd3 +```go +viper.AddRemoteProvider("etcd3", "http://127.0.0.1:4001","/config/hugo.json") +viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" +err := viper.ReadRemoteConfig() +``` + +#### Consul +You need to set a key to Consul key/value storage with JSON value containing your desired config. +For example, create a Consul key/value store key `MY_CONSUL_KEY` with value: + +```json +{ + "port": 8080, + "hostname": "myhostname.com" +} +``` + +```go +viper.AddRemoteProvider("consul", "localhost:8500", "MY_CONSUL_KEY") +viper.SetConfigType("json") // Need to explicitly set this to json +err := viper.ReadRemoteConfig() + +fmt.Println(viper.Get("port")) // 8080 +fmt.Println(viper.Get("hostname")) // myhostname.com +``` + +#### Firestore + +```go +viper.AddRemoteProvider("firestore", "google-cloud-project-id", "collection/document") +viper.SetConfigType("json") // Config's format: "json", "toml", "yaml", "yml" +err := viper.ReadRemoteConfig() +``` + +Of course, you're allowed to use `SecureRemoteProvider` also + + +#### NATS + +```go +viper.AddRemoteProvider("nats", "nats://127.0.0.1:4222", "myapp.config") +viper.SetConfigType("json") err := viper.ReadRemoteConfig() ``` @@ -361,7 +504,7 @@ err := viper.ReadRemoteConfig() ```go viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg") -viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" err := viper.ReadRemoteConfig() ``` @@ -372,7 +515,7 @@ err := viper.ReadRemoteConfig() var runtime_viper = viper.New() runtime_viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001", "/config/hugo.yml") -runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" // read from remote config the first time. err := runtime_viper.ReadRemoteConfig() @@ -383,48 +526,53 @@ runtime_viper.Unmarshal(&runtime_conf) // open a goroutine to watch remote changes forever go func(){ for { - time.Sleep(time.Second * 5) // delay after each request - - // currently, only tested with etcd support - err := runtime_viper.WatchRemoteConfig() - if err != nil { - log.Errorf("unable to read remote config: %v", err) - continue - } - - // unmarshal new config into our runtime config struct. you can also use channel - // to implement a signal to notify the system of the changes - runtime_viper.Unmarshal(&runtime_conf) + time.Sleep(time.Second * 5) // delay after each request + + // currently, only tested with etcd support + err := runtime_viper.WatchRemoteConfig() + if err != nil { + log.Errorf("unable to read remote config: %v", err) + continue + } + + // unmarshal new config into our runtime config struct. you can also use channel + // to implement a signal to notify the system of the changes + runtime_viper.Unmarshal(&runtime_conf) } }() ``` ## Getting Values From Viper -In Viper, there are a few ways to get a value depending on the value's type. +In Viper, there are a few ways to get a value depending on the value’s type. The following functions and methods exist: - * `Get(key string) : interface{}` + * `Get(key string) : any` * `GetBool(key string) : bool` * `GetFloat64(key string) : float64` * `GetInt(key string) : int` + * `GetIntSlice(key string) : []int` * `GetString(key string) : string` - * `GetStringMap(key string) : map[string]interface{}` + * `GetStringMap(key string) : map[string]any` * `GetStringMapString(key string) : map[string]string` * `GetStringSlice(key string) : []string` * `GetTime(key string) : time.Time` * `GetDuration(key string) : time.Duration` * `IsSet(key string) : bool` + * `AllSettings() : map[string]any` One important thing to recognize is that each Get function will return a zero value if it’s not found. To check if a given key exists, the `IsSet()` method has been provided. +The zero value will also be returned if the value is set, but fails to parse +as the requested type. + Example: ```go viper.GetString("logfile") // case-insensitive Setting & Getting if viper.GetBool("verbose") { - fmt.Println("verbose enabled") + fmt.Println("verbose enabled") } ``` ### Accessing nested keys @@ -470,10 +618,37 @@ the `Set()` method, …) with an immediate value, then all sub-keys of `datastore.metric` become undefined, they are “shadowed” by the higher-priority configuration level. +Viper can access array indices by using numbers in the path. For example: + +```jsonc +{ + "host": { + "address": "localhost", + "ports": [ + 5799, + 6029 + ] + }, + "datastore": { + "metric": { + "host": "127.0.0.1", + "port": 3099 + }, + "warehouse": { + "host": "198.0.0.1", + "port": 2112 + } + } +} + +GetInt("host.ports.1") // returns 6029 + +``` + Lastly, if there exists a key that matches the delimited key path, its value will be returned instead. E.g. -```json +```jsonc { "datastore.metric.host": "0.0.0.0", "host": { @@ -495,14 +670,15 @@ will be returned instead. E.g. GetString("datastore.metric.host") // returns "0.0.0.0" ``` -### Extract sub-tree +### Extracting a sub-tree -Extract sub-tree from Viper. +When developing reusable modules, it's often useful to extract a subset of the configuration +and pass it to a module. This way the module can be instantiated more than once, with different configurations. -For example, `viper` represents: +For example, an application might use multiple different cache stores for different purposes: -```json -app: +```yaml +cache: cache1: max-items: 100 item-size: 64 @@ -511,35 +687,36 @@ app: item-size: 80 ``` -After executing: +We could pass the cache name to a module (eg. `NewCache("cache1")`), +but it would require weird concatenation for accessing config keys and would be less separated from the global config. -```go -subv := viper.Sub("app.cache1") -``` +So instead of doing that let's pass a Viper instance to the constructor that represents a subset of the configuration: -`subv` represents: +```go +cache1Config := viper.Sub("cache.cache1") +if cache1Config == nil { // Sub returns nil if the key cannot be found + panic("cache configuration not found") +} -```json -max-items: 100 -item-size: 64 +cache1 := NewCache(cache1Config) ``` -Suppose we have: +**Note:** Always check the return value of `Sub`. It returns `nil` if a key cannot be found. + +Internally, the `NewCache` function can address `max-items` and `item-size` keys directly: ```go -func NewCache(cfg *Viper) *Cache {...} +func NewCache(v *Viper) *Cache { + return &Cache{ + MaxItems: v.GetInt("max-items"), + ItemSize: v.GetInt("item-size"), + } +} ``` -which creates a cache based on config information formatted as `subv`. -Now it's easy to create these 2 caches separately as: +The resulting code is easy to test, since it's decoupled from the main config structure, +and easier to reuse (for the same reason). -```go -cfg1 := viper.Sub("app.cache1") -cache1 := NewCache(cfg1) - -cfg2 := viper.Sub("app.cache2") -cache2 := NewCache(cfg2) -``` ### Unmarshaling @@ -548,8 +725,8 @@ etc. There are two methods to do this: - * `Unmarshal(rawVal interface{}) : error` - * `UnmarshalKey(key string, rawVal interface{}) : error` + * `Unmarshal(rawVal any) : error` + * `UnmarshalKey(key string, rawVal any) : error` Example: @@ -562,26 +739,117 @@ type config struct { var C config -err := Unmarshal(&C) +err := viper.Unmarshal(&C) +if err != nil { + t.Fatalf("unable to decode into struct, %v", err) +} +``` + +If you want to unmarshal configuration where the keys themselves contain dot (the default key delimiter), +you have to change the delimiter: + +```go +v := viper.NewWithOptions(viper.KeyDelimiter("::")) + +v.SetDefault("chart::values", map[string]any{ + "ingress": map[string]any{ + "annotations": map[string]any{ + "traefik.frontend.rule.type": "PathPrefix", + "traefik.ingress.kubernetes.io/ssl-redirect": "true", + }, + }, +}) + +type config struct { + Chart struct{ + Values map[string]any + } +} + +var C config + +v.Unmarshal(&C) +``` + +Viper also supports unmarshaling into embedded structs: + +```go +/* +Example config: + +module: + enabled: true + token: 89h3f98hbwf987h3f98wenf89ehf +*/ +type config struct { + Module struct { + Enabled bool + + moduleConfig `mapstructure:",squash"` + } +} + +// moduleConfig could be in a module specific package +type moduleConfig struct { + Token string +} + +var C config + +err := viper.Unmarshal(&C) if err != nil { t.Fatalf("unable to decode into struct, %v", err) } ``` +Viper uses [github.com/go-viper/mapstructure](https://github.com/go-viper/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. + +### Decoding custom formats + +A frequently requested feature for Viper is adding more value formats and decoders. +For example, parsing character (dot, comma, semicolon, etc) separated strings into slices. + +This is already available in Viper using mapstructure decode hooks. + +Read more about the details in [this blog post](https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/). + +### Marshalling to string + +You may need to marshal all the settings held in viper into a string rather than write them to a file. +You can use your favorite format's marshaller with the config returned by `AllSettings()`. + +```go +import ( + yaml "gopkg.in/yaml.v2" + // ... +) + +func yamlStringSettings() string { + c := viper.AllSettings() + bs, err := yaml.Marshal(c) + if err != nil { + log.Fatalf("unable to marshal config to YAML: %v", err) + } + return string(bs) +} +``` + ## Viper or Vipers? -Viper comes ready to use out of the box. There is no configuration or -initialization needed to begin using Viper. Since most applications will want -to use a single central repository for their configuration, the viper package -provides this. It is similar to a singleton. +Viper comes with a global instance (singleton) out of the box. + +Although it makes setting up configuration easy, +using it is generally discouraged as it makes testing harder and can lead to unexpected behavior. + +The best practice is to initialize a Viper instance and pass that around when necessary. -In all of the examples above, they demonstrate using viper in it's singleton -style approach. +The global instance _MAY_ be deprecated in the future. +See [#1855](https://github.com/spf13/viper/issues/1855) for more details. ### Working with multiple vipers You can also create many different vipers for use in your application. Each will -have it’s own unique set of configurations and values. Each can read from a +have its own unique set of configurations and values. Each can read from a different config file, key value store, etc. All of the functions that viper package supports are mirrored as methods on a viper. @@ -600,22 +868,63 @@ y.SetDefault("ContentDir", "foobar") When working with multiple vipers, it is up to the user to keep track of the different vipers. -## Q & A - -Q: Why not INI files? -A: Ini files are pretty awful. There’s no standard format, and they are hard to -validate. Viper is designed to work with JSON, TOML or YAML files. If someone -really wants to add this feature, I’d be happy to merge it. It’s easy to specify -which formats your application will permit. +## Q & A -Q: Why is it called “Viper”? +### Why is it called “Viper”? A: Viper is designed to be a [companion](http://en.wikipedia.org/wiki/Viper_(G.I._Joe)) to [Cobra](https://github.com/spf13/cobra). While both can operate completely independently, together they make a powerful pair to handle much of your application foundation needs. -Q: Why is it called “Cobra”? +### Why is it called “Cobra”? + +Is there a better name for a [commander](http://en.wikipedia.org/wiki/Cobra_Commander)? + +### Does Viper support case sensitive keys? + +**tl;dr:** No. + +Viper merges configuration from various sources, many of which are either case insensitive or uses different casing than the rest of the sources (eg. env vars). +In order to provide the best experience when using multiple sources, the decision has been made to make all keys case insensitive. + +There has been several attempts to implement case sensitivity, but unfortunately it's not that trivial. We might take a stab at implementing it in [Viper v2](https://github.com/spf13/viper/issues/772), but despite the initial noise, it does not seem to be requested that much. + +You can vote for case sensitivity by filling out this feedback form: https://forms.gle/R6faU74qPRPAzchZ9 + +### Is it safe to concurrently read and write to a viper? + +No, you will need to synchronize access to the viper yourself (for example by using the `sync` package). Concurrent reads and writes can cause a panic. + +## Troubleshooting + +See [TROUBLESHOOTING.md](TROUBLESHOOTING.md). + +## Development + +**For an optimal developer experience, it is recommended to install [Nix](https://nixos.org/download.html) and [direnv](https://direnv.net/docs/installation.html).** + +_Alternatively, install [Go](https://go.dev/dl/) on your computer then run `make deps` to install the rest of the dependencies._ + +Run the test suite: + +```shell +make test +``` + +Run linters: + +```shell +make lint # pass -j option to run them in parallel +``` + +Some linter violations can automatically be fixed: + +```shell +make fmt +``` + +## License -A: Is there a better name for a [commander](http://en.wikipedia.org/wiki/Cobra_Commander)? +The project is licensed under the [MIT License](LICENSE). diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md new file mode 100644 index 000000000..b68993d41 --- /dev/null +++ b/TROUBLESHOOTING.md @@ -0,0 +1,32 @@ +# Troubleshooting + +## Unmarshaling doesn't work + +The most common reason for this issue is improper use of struct tags (eg. `yaml` or `json`). Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. Please refer to the library's documentation for using other struct tags. + +## Cannot find package + +Viper installation seems to fail a lot lately with the following (or a similar) error: + +``` +cannot find package "github.com/hashicorp/hcl/tree/hcl1" in any of: +/usr/local/Cellar/go/1.15.7_1/libexec/src/github.com/hashicorp/hcl/tree/hcl1 (from $GOROOT) +/Users/user/go/src/github.com/hashicorp/hcl/tree/hcl1 (from $GOPATH) +``` + +As the error message suggests, Go tries to look up dependencies in `GOPATH` mode (as it's commonly called) from the `GOPATH`. +Viper opted to use [Go Modules](https://go.dev/wiki/Modules) to manage its dependencies. While in many cases the two methods are interchangeable, once a dependency releases new (major) versions, `GOPATH` mode is no longer able to decide which version to use, so it'll either use one that's already present or pick a version (usually the `master` branch). + +The solution is easy: switch to using Go Modules. +Please refer to the [wiki](https://go.dev/wiki/Modules) on how to do that. + +**tl;dr* `export GO111MODULE=on` + +## Unquoted 'y' and 'n' characters get replaced with _true_ and _false_ when reading a YAML file + +This is a YAML 1.1 feature according to [go-yaml/yaml#740](https://github.com/go-yaml/yaml/issues/740). + +Potential solutions are: + +1. Quoting values resolved as boolean +1. Upgrading to YAML v3 (for the time being this is possible by passing the `viper_yaml3` tag to your build) diff --git a/encoding.go b/encoding.go new file mode 100644 index 000000000..a7da55860 --- /dev/null +++ b/encoding.go @@ -0,0 +1,181 @@ +package viper + +import ( + "errors" + "strings" + "sync" + + "github.com/spf13/viper/internal/encoding/dotenv" + "github.com/spf13/viper/internal/encoding/json" + "github.com/spf13/viper/internal/encoding/toml" + "github.com/spf13/viper/internal/encoding/yaml" +) + +// Encoder encodes Viper's internal data structures into a byte representation. +// It's primarily used for encoding a map[string]any into a file format. +type Encoder interface { + Encode(v map[string]any) ([]byte, error) +} + +// Decoder decodes the contents of a byte slice into Viper's internal data structures. +// It's primarily used for decoding contents of a file into a map[string]any. +type Decoder interface { + Decode(b []byte, v map[string]any) error +} + +// Codec combines [Encoder] and [Decoder] interfaces. +type Codec interface { + Encoder + Decoder +} + +// TODO: consider adding specific errors for not found scenarios + +// EncoderRegistry returns an [Encoder] for a given format. +// +// Format is case-insensitive. +// +// [EncoderRegistry] returns an error if no [Encoder] is registered for the format. +type EncoderRegistry interface { + Encoder(format string) (Encoder, error) +} + +// DecoderRegistry returns an [Decoder] for a given format. +// +// Format is case-insensitive. +// +// [DecoderRegistry] returns an error if no [Decoder] is registered for the format. +type DecoderRegistry interface { + Decoder(format string) (Decoder, error) +} + +// [CodecRegistry] combines [EncoderRegistry] and [DecoderRegistry] interfaces. +type CodecRegistry interface { + EncoderRegistry + DecoderRegistry +} + +// WithEncoderRegistry sets a custom [EncoderRegistry]. +func WithEncoderRegistry(r EncoderRegistry) Option { + return optionFunc(func(v *Viper) { + if r == nil { + return + } + + v.encoderRegistry = r + }) +} + +// WithDecoderRegistry sets a custom [DecoderRegistry]. +func WithDecoderRegistry(r DecoderRegistry) Option { + return optionFunc(func(v *Viper) { + if r == nil { + return + } + + v.decoderRegistry = r + }) +} + +// WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry]. +func WithCodecRegistry(r CodecRegistry) Option { + return optionFunc(func(v *Viper) { + if r == nil { + return + } + + v.encoderRegistry = r + v.decoderRegistry = r + }) +} + +// DefaultCodecRegistry is a simple implementation of [CodecRegistry] that allows registering custom [Codec]s. +type DefaultCodecRegistry struct { + codecs map[string]Codec + + mu sync.RWMutex + once sync.Once +} + +// NewCodecRegistry returns a new [CodecRegistry], ready to accept custom [Codec]s. +func NewCodecRegistry() *DefaultCodecRegistry { + r := &DefaultCodecRegistry{} + + r.init() + + return r +} + +func (r *DefaultCodecRegistry) init() { + r.once.Do(func() { + r.codecs = map[string]Codec{} + }) +} + +// RegisterCodec registers a custom [Codec]. +// +// Format is case-insensitive. +func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error { + r.init() + + r.mu.Lock() + defer r.mu.Unlock() + + r.codecs[strings.ToLower(format)] = codec + + return nil +} + +// Encoder implements the [EncoderRegistry] interface. +// +// Format is case-insensitive. +func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) { + encoder, ok := r.codec(format) + if !ok { + return nil, errors.New("encoder not found for this format") + } + + return encoder, nil +} + +// Decoder implements the [DecoderRegistry] interface. +// +// Format is case-insensitive. +func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) { + decoder, ok := r.codec(format) + if !ok { + return nil, errors.New("decoder not found for this format") + } + + return decoder, nil +} + +func (r *DefaultCodecRegistry) codec(format string) (Codec, bool) { + r.mu.Lock() + defer r.mu.Unlock() + + format = strings.ToLower(format) + + if r.codecs != nil { + codec, ok := r.codecs[format] + if ok { + return codec, true + } + } + + switch format { + case "yaml", "yml": + return yaml.Codec{}, true + + case "json": + return json.Codec{}, true + + case "toml": + return toml.Codec{}, true + + case "dotenv", "env": + return &dotenv.Codec{}, true + } + + return nil, false +} diff --git a/encoding_test.go b/encoding_test.go new file mode 100644 index 000000000..3e67a30fa --- /dev/null +++ b/encoding_test.go @@ -0,0 +1,105 @@ +package viper + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type codec struct{} + +func (codec) Encode(_ map[string]any) ([]byte, error) { + return nil, nil +} + +func (codec) Decode(_ []byte, _ map[string]any) error { + return nil +} + +func TestDefaultCodecRegistry(t *testing.T) { + t.Run("OK", func(t *testing.T) { + registry := NewCodecRegistry() + + c := codec{} + + err := registry.RegisterCodec("myformat", c) + require.NoError(t, err) + + encoder, err := registry.Encoder("myformat") + require.NoError(t, err) + + assert.Equal(t, c, encoder) + + decoder, err := registry.Decoder("myformat") + require.NoError(t, err) + + assert.Equal(t, c, decoder) + }) + + t.Run("CodecNotFound", func(t *testing.T) { + registry := NewCodecRegistry() + + _, err := registry.Encoder("myformat") + require.Error(t, err) + + _, err = registry.Decoder("myformat") + require.Error(t, err) + }) + + t.Run("FormatIsCaseInsensitive", func(t *testing.T) { + registry := NewCodecRegistry() + + c := codec{} + + err := registry.RegisterCodec("MYFORMAT", c) + require.NoError(t, err) + + { + encoder, err := registry.Encoder("myformat") + require.NoError(t, err) + + assert.Equal(t, c, encoder) + } + + { + encoder, err := registry.Encoder("MYFORMAT") + require.NoError(t, err) + + assert.Equal(t, c, encoder) + } + + { + decoder, err := registry.Decoder("myformat") + require.NoError(t, err) + + assert.Equal(t, c, decoder) + } + + { + decoder, err := registry.Decoder("MYFORMAT") + require.NoError(t, err) + + assert.Equal(t, c, decoder) + } + }) + + t.Run("OverrideDefault", func(t *testing.T) { + registry := NewCodecRegistry() + + c := codec{} + + err := registry.RegisterCodec("yaml", c) + require.NoError(t, err) + + encoder, err := registry.Encoder("yaml") + require.NoError(t, err) + + assert.Equal(t, c, encoder) + + decoder, err := registry.Decoder("yaml") + require.NoError(t, err) + + assert.Equal(t, c, decoder) + }) +} diff --git a/experimental.go b/experimental.go new file mode 100644 index 000000000..6e19e8a10 --- /dev/null +++ b/experimental.go @@ -0,0 +1,8 @@ +package viper + +// ExperimentalBindStruct tells Viper to use the new bind struct feature. +func ExperimentalBindStruct() Option { + return optionFunc(func(v *Viper) { + v.experimentalBindStruct = true + }) +} diff --git a/file.go b/file.go new file mode 100644 index 000000000..50a40581d --- /dev/null +++ b/file.go @@ -0,0 +1,104 @@ +package viper + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/sagikazarmark/locafero" + "github.com/spf13/afero" +) + +// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files. +func ExperimentalFinder() Option { + return optionFunc(func(v *Viper) { + v.experimentalFinder = true + }) +} + +// Search for a config file. +func (v *Viper) findConfigFile() (string, error) { + finder := v.finder + + if finder == nil && v.experimentalFinder { + var names []string + + if v.configType != "" { + names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...) + } else { + names = locafero.NameWithExtensions(v.configName, SupportedExts...) + } + + finder = locafero.Finder{ + Paths: v.configPaths, + Names: names, + Type: locafero.FileTypeFile, + } + } + + if finder != nil { + return v.findConfigFileWithFinder(finder) + } + + return v.findConfigFileOld() +} + +func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) { + results, err := finder.Find(v.fs) + if err != nil { + return "", err + } + + if len(results) == 0 { + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} + } + + // We call clean on the final result to ensure that the path is in its canonical form. + // This is mostly for consistent path handling and to make sure tests pass. + return results[0], nil +} + +// Search all configPaths for any config file. +// Returns the first path that exists (and is a config file). +func (v *Viper) findConfigFileOld() (string, error) { + v.logger.Info("searching for config in paths", "paths", v.configPaths) + + for _, cp := range v.configPaths { + file := v.searchInPath(cp) + if file != "" { + return file, nil + } + } + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} +} + +func (v *Viper) searchInPath(in string) (filename string) { + v.logger.Debug("searching for config in path", "path", in) + for _, ext := range SupportedExts { + v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext)) + if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b { + v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext)) + return filepath.Join(in, v.configName+"."+ext) + } + } + + if v.configType != "" { + if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { + return filepath.Join(in, v.configName) + } + } + + return "" +} + +// exists checks if file exists. +func exists(fs afero.Fs, path string) (bool, error) { + stat, err := fs.Stat(path) + if err == nil { + return !stat.IsDir(), nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} diff --git a/finder.go b/finder.go new file mode 100644 index 000000000..9b203ea69 --- /dev/null +++ b/finder.go @@ -0,0 +1,55 @@ +package viper + +import ( + "errors" + + "github.com/spf13/afero" +) + +// WithFinder sets a custom [Finder]. +func WithFinder(f Finder) Option { + return optionFunc(func(v *Viper) { + if f == nil { + return + } + + v.finder = f + }) +} + +// Finder looks for files and directories in an [afero.Fs] filesystem. +type Finder interface { + Find(fsys afero.Fs) ([]string, error) +} + +// Finders combines multiple finders into one. +func Finders(finders ...Finder) Finder { + return &combinedFinder{finders: finders} +} + +// combinedFinder is a Finder that combines multiple finders. +type combinedFinder struct { + finders []Finder +} + +// Find implements the [Finder] interface. +func (c *combinedFinder) Find(fsys afero.Fs) ([]string, error) { + var results []string + var errs []error + + for _, finder := range c.finders { + if finder == nil { + continue + } + + r, err := finder.Find(fsys) + if err != nil { + errs = append(errs, err) + continue + } + + results = append(results, r...) + } + + return results, errors.Join(errs...) +} diff --git a/finder_example_test.go b/finder_example_test.go new file mode 100644 index 000000000..8f094520f --- /dev/null +++ b/finder_example_test.go @@ -0,0 +1,73 @@ +package viper_test + +import ( + "fmt" + + "github.com/sagikazarmark/locafero" + "github.com/spf13/afero" + + "github.com/spf13/viper" +) + +func ExampleFinder() { + fs := afero.NewMemMapFs() + + fs.Mkdir("/home/user", 0o777) + + f, _ := fs.Create("/home/user/myapp.yaml") + f.WriteString("foo: bar") + f.Close() + + // HCL will have a "lower" priority in the search order + fs.Create("/home/user/myapp.hcl") + + finder := locafero.Finder{ + Paths: []string{"/home/user"}, + Names: locafero.NameWithExtensions("myapp", viper.SupportedExts...), + Type: locafero.FileTypeFile, // This is important! + } + + v := viper.NewWithOptions(viper.WithFinder(finder)) + v.SetFs(fs) + v.ReadInConfig() + + fmt.Println(v.GetString("foo")) + + // Output: + // bar +} + +func ExampleFinders() { + fs := afero.NewMemMapFs() + + fs.Mkdir("/home/user", 0o777) + f, _ := fs.Create("/home/user/myapp.yaml") + f.WriteString("foo: bar") + f.Close() + + fs.Mkdir("/etc/myapp", 0o777) + fs.Create("/etc/myapp/config.yaml") + + // Combine multiple finders to search for files in multiple locations with different criteria + finder := viper.Finders( + locafero.Finder{ + Paths: []string{"/home/user"}, + Names: locafero.NameWithExtensions("myapp", viper.SupportedExts...), + Type: locafero.FileTypeFile, // This is important! + }, + locafero.Finder{ + Paths: []string{"/etc/myapp"}, + Names: []string{"config.yaml"}, // Only accept YAML files in the system config directory + Type: locafero.FileTypeFile, // This is important! + }, + ) + + v := viper.NewWithOptions(viper.WithFinder(finder)) + v.SetFs(fs) + v.ReadInConfig() + + fmt.Println(v.GetString("foo")) + + // Output: + // bar +} diff --git a/finder_test.go b/finder_test.go new file mode 100644 index 000000000..d57a413d7 --- /dev/null +++ b/finder_test.go @@ -0,0 +1,42 @@ +package viper + +import ( + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type finderStub struct { + results []string +} + +func (f finderStub) Find(_ afero.Fs) ([]string, error) { + return f.results, nil +} + +func TestFinders(t *testing.T) { + finder := Finders( + finderStub{ + results: []string{ + "/home/user/.viper.yaml", + }, + }, + finderStub{ + results: []string{ + "/etc/viper/config.yaml", + }, + }, + ) + + results, err := finder.Find(afero.NewMemMapFs()) + require.NoError(t, err) + + expected := []string{ + "/home/user/.viper.yaml", + "/etc/viper/config.yaml", + } + + assert.Equal(t, expected, results) +} diff --git a/flags.go b/flags.go index dd32f4e1c..de033ed58 100644 --- a/flags.go +++ b/flags.go @@ -30,13 +30,13 @@ func (p pflagValueSet) VisitAll(fn func(flag FlagValue)) { }) } -// pflagValue is a wrapper aroung *pflag.flag -// that implements FlagValue +// pflagValue is a wrapper around *pflag.flag +// that implements FlagValue. type pflagValue struct { flag *pflag.Flag } -// HasChanges returns whether the flag has changes or not. +// HasChanged returns whether the flag has changes or not. func (p pflagValue) HasChanged() bool { return p.flag.Changed } diff --git a/flags_test.go b/flags_test.go index 5bffca36c..18a5403c6 100644 --- a/flags_test.go +++ b/flags_test.go @@ -5,18 +5,20 @@ import ( "github.com/spf13/pflag" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBindFlagValueSet(t *testing.T) { + Reset() flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) - var testValues = map[string]*string{ + testValues := map[string]*string{ "host": nil, "port": nil, "endpoint": nil, } - var mutatedTestValues = map[string]string{ + mutatedTestValues := map[string]string{ "host": "localhost", "port": "6060", "endpoint": "/public", @@ -29,9 +31,7 @@ func TestBindFlagValueSet(t *testing.T) { flagValueSet := pflagValueSet{flagSet} err := BindFlagValues(flagValueSet) - if err != nil { - t.Fatalf("error binding flag set, %v", err) - } + require.NoError(t, err, "error binding flag set") flagSet.VisitAll(func(flag *pflag.Flag) { flag.Value.Set(mutatedTestValues[flag.Name]) @@ -39,13 +39,13 @@ func TestBindFlagValueSet(t *testing.T) { }) for name, expected := range mutatedTestValues { - assert.Equal(t, Get(name), expected) + assert.Equal(t, expected, Get(name)) } } func TestBindFlagValue(t *testing.T) { - var testString = "testing" - var testValue = newStringValue(testString, &testString) + testString := "testing" + testValue := newStringValue(testString, &testString) flag := &pflag.Flag{ Name: "testflag", @@ -59,8 +59,7 @@ func TestBindFlagValue(t *testing.T) { assert.Equal(t, testString, Get("testvalue")) flag.Value.Set("testing_mutate") - flag.Changed = true //hack for pflag usage + flag.Changed = true // hack for pflag usage assert.Equal(t, "testing_mutate", Get("testvalue")) - } diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..d76dfbddd --- /dev/null +++ b/flake.lock @@ -0,0 +1,472 @@ +{ + "nodes": { + "cachix": { + "inputs": { + "devenv": "devenv_2", + "flake-compat": [ + "devenv", + "flake-compat" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "pre-commit-hooks": [ + "devenv", + "pre-commit-hooks" + ] + }, + "locked": { + "lastModified": 1712055811, + "narHash": "sha256-7FcfMm5A/f02yyzuavJe06zLa9hcMHsagE28ADcmQvk=", + "owner": "cachix", + "repo": "cachix", + "rev": "02e38da89851ec7fec3356a5c04bc8349cae0e30", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "cachix", + "type": "github" + } + }, + "devenv": { + "inputs": { + "cachix": "cachix", + "flake-compat": "flake-compat_2", + "nix": "nix_2", + "nixpkgs": "nixpkgs_2", + "pre-commit-hooks": "pre-commit-hooks" + }, + "locked": { + "lastModified": 1724763216, + "narHash": "sha256-oW2bwCrJpIzibCNK6zfIDaIQw765yMAuMSG2gyZfGv0=", + "owner": "cachix", + "repo": "devenv", + "rev": "1e4ef61205b9aa20fe04bf1c468b6a316281c4f1", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "devenv_2": { + "inputs": { + "flake-compat": [ + "devenv", + "cachix", + "flake-compat" + ], + "nix": "nix", + "nixpkgs": "nixpkgs", + "poetry2nix": "poetry2nix", + "pre-commit-hooks": [ + "devenv", + "cachix", + "pre-commit-hooks" + ] + }, + "locked": { + "lastModified": 1708704632, + "narHash": "sha256-w+dOIW60FKMaHI1q5714CSibk99JfYxm0CzTinYWr+Q=", + "owner": "cachix", + "repo": "devenv", + "rev": "2ee4450b0f4b95a1b90f2eb5ffea98b90e48c196", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "python-rewrite", + "repo": "devenv", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1722555600, + "narHash": "sha256-XOQkdLafnb/p9ij77byFQjDf5m5QYl9b2REiVClC+x4=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "8471fe90ad337a8074e957b69ca4d0089218391d", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1689068808, + "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "devenv", + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nix": { + "inputs": { + "flake-compat": "flake-compat", + "nixpkgs": [ + "devenv", + "cachix", + "devenv", + "nixpkgs" + ], + "nixpkgs-regression": "nixpkgs-regression" + }, + "locked": { + "lastModified": 1712911606, + "narHash": "sha256-BGvBhepCufsjcUkXnEEXhEVjwdJAwPglCC2+bInc794=", + "owner": "domenkozar", + "repo": "nix", + "rev": "b24a9318ea3f3600c1e24b4a00691ee912d4de12", + "type": "github" + }, + "original": { + "owner": "domenkozar", + "ref": "devenv-2.21", + "repo": "nix", + "type": "github" + } + }, + "nix-github-actions": { + "inputs": { + "nixpkgs": [ + "devenv", + "cachix", + "devenv", + "poetry2nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1688870561, + "narHash": "sha256-4UYkifnPEw1nAzqqPOTL2MvWtm3sNGw1UTYTalkTcGY=", + "owner": "nix-community", + "repo": "nix-github-actions", + "rev": "165b1650b753316aa7f1787f3005a8d2da0f5301", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nix-github-actions", + "type": "github" + } + }, + "nix_2": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-regression": "nixpkgs-regression_2" + }, + "locked": { + "lastModified": 1712911606, + "narHash": "sha256-BGvBhepCufsjcUkXnEEXhEVjwdJAwPglCC2+bInc794=", + "owner": "domenkozar", + "repo": "nix", + "rev": "b24a9318ea3f3600c1e24b4a00691ee912d4de12", + "type": "github" + }, + "original": { + "owner": "domenkozar", + "ref": "devenv-2.21", + "repo": "nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1692808169, + "narHash": "sha256-x9Opq06rIiwdwGeK2Ykj69dNc2IvUH1fY55Wm7atwrE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9201b5ff357e781bf014d0330d18555695df7ba8", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1722555339, + "narHash": "sha256-uFf2QeW7eAHlYXuDktm9c25OxOyCoUOQmh5SZ9amE5Q=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz" + } + }, + "nixpkgs-regression": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs-regression_2": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1710695816, + "narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "614b4613980a522ba49f0d194531beddbb7220d3", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1713361204, + "narHash": "sha256-TA6EDunWTkc5FvDCqU3W2T3SFn0gRZqh6D/hJnM02MM=", + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "285676e87ad9f0ca23d8714a6ab61e7e027020c6", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1724748588, + "narHash": "sha256-NlpGA4+AIf1dKNq76ps90rxowlFXUsV9x7vK/mN37JM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a6292e34000dc93d43bccf78338770c1c5ec8a99", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "poetry2nix": { + "inputs": { + "flake-utils": "flake-utils", + "nix-github-actions": "nix-github-actions", + "nixpkgs": [ + "devenv", + "cachix", + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1692876271, + "narHash": "sha256-IXfZEkI0Mal5y1jr6IRWMqK8GW2/f28xJenZIPQqkY0=", + "owner": "nix-community", + "repo": "poetry2nix", + "rev": "d5006be9c2c2417dafb2e2e5034d83fabd207ee3", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "poetry2nix", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "flake-utils": "flake-utils_2", + "gitignore": "gitignore", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1713775815, + "narHash": "sha256-Wu9cdYTnGQQwtT20QQMg7jzkANKQjwBD9iccfGKkfls=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "2ac4dcbf55ed43f3be0bae15e181f08a57af24a4", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs_3" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..52ad7d581 --- /dev/null +++ b/flake.nix @@ -0,0 +1,57 @@ +{ + description = "Viper"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + devenv.url = "github:cachix/devenv"; + }; + + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ + inputs.devenv.flakeModule + ]; + + systems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" ]; + + perSystem = { config, self', inputs', pkgs, system, ... }: rec { + devenv.shells = { + default = { + languages = { + go.enable = true; + go.package = pkgs.go_1_23; + }; + + pre-commit.hooks = { + nixpkgs-fmt.enable = true; + yamllint.enable = true; + }; + + packages = with pkgs; [ + gnumake + + golangci-lint + yamllint + ]; + + scripts = { + versions.exec = '' + go version + golangci-lint version + ''; + }; + + enterShell = '' + versions + ''; + + # https://github.com/cachix/devenv/issues/528#issuecomment-1556108767 + containers = pkgs.lib.mkForce { }; + }; + + ci = devenv.shells.default; + }; + }; + }; +} diff --git a/go.mod b/go.mod new file mode 100644 index 000000000..59f07388b --- /dev/null +++ b/go.mod @@ -0,0 +1,27 @@ +module github.com/spf13/viper + +go 1.21.0 + +require ( + github.com/fsnotify/fsnotify v1.7.0 + github.com/go-viper/mapstructure/v2 v2.1.0 + github.com/pelletier/go-toml/v2 v2.2.3 + github.com/sagikazarmark/locafero v0.6.0 + github.com/spf13/afero v1.11.0 + github.com/spf13/cast v1.7.0 + github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.9.0 + github.com/subosito/gotenv v1.6.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 000000000..6bb7a6857 --- /dev/null +++ b/go.sum @@ -0,0 +1,50 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-viper/mapstructure/v2 v2.1.0 h1:gHnMa2Y/pIxElCH2GlZZ1lZSsn6XMtufpGyP1XxdC/w= +github.com/go-viper/mapstructure/v2 v2.1.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= +github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= +github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/encoding/dotenv/codec.go b/internal/encoding/dotenv/codec.go new file mode 100644 index 000000000..3ebc76f02 --- /dev/null +++ b/internal/encoding/dotenv/codec.go @@ -0,0 +1,61 @@ +package dotenv + +import ( + "bytes" + "fmt" + "sort" + "strings" + + "github.com/subosito/gotenv" +) + +const keyDelimiter = "_" + +// Codec implements the encoding.Encoder and encoding.Decoder interfaces for encoding data containing environment variables +// (commonly called as dotenv format). +type Codec struct{} + +func (Codec) Encode(v map[string]any) ([]byte, error) { + flattened := map[string]any{} + + flattened = flattenAndMergeMap(flattened, v, "", keyDelimiter) + + keys := make([]string, 0, len(flattened)) + + for key := range flattened { + keys = append(keys, key) + } + + sort.Strings(keys) + + var buf bytes.Buffer + + for _, key := range keys { + _, err := buf.WriteString(fmt.Sprintf("%v=%v\n", strings.ToUpper(key), flattened[key])) + if err != nil { + return nil, err + } + } + + return buf.Bytes(), nil +} + +func (Codec) Decode(b []byte, v map[string]any) error { + var buf bytes.Buffer + + _, err := buf.Write(b) + if err != nil { + return err + } + + env, err := gotenv.StrictParse(&buf) + if err != nil { + return err + } + + for key, value := range env { + v[key] = value + } + + return nil +} diff --git a/internal/encoding/dotenv/codec_test.go b/internal/encoding/dotenv/codec_test.go new file mode 100644 index 000000000..ac2257b79 --- /dev/null +++ b/internal/encoding/dotenv/codec_test.go @@ -0,0 +1,55 @@ +package dotenv + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// original form of the data. +const original = `# key-value pair +KEY=value +` + +// encoded form of the data. +const encoded = `KEY=value +` + +// data is Viper's internal representation. +var data = map[string]any{ + "KEY": "value", +} + +func TestCodec_Encode(t *testing.T) { + codec := Codec{} + + b, err := codec.Encode(data) + require.NoError(t, err) + + assert.Equal(t, encoded, string(b)) +} + +func TestCodec_Decode(t *testing.T) { + t.Run("OK", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(original), v) + require.NoError(t, err) + + assert.Equal(t, data, v) + }) + + t.Run("InvalidData", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(`invalid data`), v) + require.Error(t, err) + + t.Logf("decoding failed as expected: %s", err) + }) +} diff --git a/internal/encoding/dotenv/map_utils.go b/internal/encoding/dotenv/map_utils.go new file mode 100644 index 000000000..8bfe0a9de --- /dev/null +++ b/internal/encoding/dotenv/map_utils.go @@ -0,0 +1,41 @@ +package dotenv + +import ( + "strings" + + "github.com/spf13/cast" +) + +// flattenAndMergeMap recursively flattens the given map into a new map +// Code is based on the function with the same name in the main package. +// TODO: move it to a common place. +func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any { + if shadow != nil && prefix != "" && shadow[prefix] != nil { + // prefix is shadowed => nothing more to flatten + return shadow + } + if shadow == nil { + shadow = make(map[string]any) + } + + var m2 map[string]any + if prefix != "" { + prefix += delimiter + } + for k, val := range m { + fullKey := prefix + k + switch val := val.(type) { + case map[string]any: + m2 = val + case map[any]any: + m2 = cast.ToStringMap(val) + default: + // immediate value + shadow[strings.ToLower(fullKey)] = val + continue + } + // recursively merge to shadow map + shadow = flattenAndMergeMap(shadow, m2, fullKey, delimiter) + } + return shadow +} diff --git a/internal/encoding/json/codec.go b/internal/encoding/json/codec.go new file mode 100644 index 000000000..da7546b5a --- /dev/null +++ b/internal/encoding/json/codec.go @@ -0,0 +1,17 @@ +package json + +import ( + "encoding/json" +) + +// Codec implements the encoding.Encoder and encoding.Decoder interfaces for JSON encoding. +type Codec struct{} + +func (Codec) Encode(v map[string]any) ([]byte, error) { + // TODO: expose prefix and indent in the Codec as setting? + return json.MarshalIndent(v, "", " ") +} + +func (Codec) Decode(b []byte, v map[string]any) error { + return json.Unmarshal(b, &v) +} diff --git a/internal/encoding/json/codec_test.go b/internal/encoding/json/codec_test.go new file mode 100644 index 000000000..87146a1fb --- /dev/null +++ b/internal/encoding/json/codec_test.go @@ -0,0 +1,87 @@ +package json + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// encoded form of the data. +const encoded = `{ + "key": "value", + "list": [ + "item1", + "item2", + "item3" + ], + "map": { + "key": "value" + }, + "nested_map": { + "map": { + "key": "value", + "list": [ + "item1", + "item2", + "item3" + ] + } + } +}` + +// data is Viper's internal representation. +var data = map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + "map": map[string]any{ + "key": "value", + }, + "nested_map": map[string]any{ + "map": map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +func TestCodec_Encode(t *testing.T) { + codec := Codec{} + + b, err := codec.Encode(data) + require.NoError(t, err) + + assert.Equal(t, encoded, string(b)) +} + +func TestCodec_Decode(t *testing.T) { + t.Run("OK", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(encoded), v) + require.NoError(t, err) + + assert.Equal(t, data, v) + }) + + t.Run("InvalidData", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(`invalid data`), v) + require.Error(t, err) + + t.Logf("decoding failed as expected: %s", err) + }) +} diff --git a/internal/encoding/toml/codec.go b/internal/encoding/toml/codec.go new file mode 100644 index 000000000..c70aa8d28 --- /dev/null +++ b/internal/encoding/toml/codec.go @@ -0,0 +1,16 @@ +package toml + +import ( + "github.com/pelletier/go-toml/v2" +) + +// Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding. +type Codec struct{} + +func (Codec) Encode(v map[string]any) ([]byte, error) { + return toml.Marshal(v) +} + +func (Codec) Decode(b []byte, v map[string]any) error { + return toml.Unmarshal(b, &v) +} diff --git a/internal/encoding/toml/codec_test.go b/internal/encoding/toml/codec_test.go new file mode 100644 index 000000000..91d9e130c --- /dev/null +++ b/internal/encoding/toml/codec_test.go @@ -0,0 +1,97 @@ +package toml + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// original form of the data. +const original = `# key-value pair +key = "value" +list = ["item1", "item2", "item3"] + +[map] +key = "value" + +# nested +# map +[nested_map] +[nested_map.map] +key = "value" +list = [ + "item1", + "item2", + "item3", +] +` + +// encoded form of the data. +const encoded = `key = 'value' +list = ['item1', 'item2', 'item3'] + +[map] +key = 'value' + +[nested_map] +[nested_map.map] +key = 'value' +list = ['item1', 'item2', 'item3'] +` + +// data is Viper's internal representation. +var data = map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + "map": map[string]any{ + "key": "value", + }, + "nested_map": map[string]any{ + "map": map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +func TestCodec_Encode(t *testing.T) { + codec := Codec{} + + b, err := codec.Encode(data) + require.NoError(t, err) + + assert.Equal(t, encoded, string(b)) +} + +func TestCodec_Decode(t *testing.T) { + t.Run("OK", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(original), v) + require.NoError(t, err) + + assert.Equal(t, data, v) + }) + + t.Run("InvalidData", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(`invalid data`), v) + require.Error(t, err) + + t.Logf("decoding failed as expected: %s", err) + }) +} diff --git a/internal/encoding/yaml/codec.go b/internal/encoding/yaml/codec.go new file mode 100644 index 000000000..036879249 --- /dev/null +++ b/internal/encoding/yaml/codec.go @@ -0,0 +1,14 @@ +package yaml + +import "gopkg.in/yaml.v3" + +// Codec implements the encoding.Encoder and encoding.Decoder interfaces for YAML encoding. +type Codec struct{} + +func (Codec) Encode(v map[string]any) ([]byte, error) { + return yaml.Marshal(v) +} + +func (Codec) Decode(b []byte, v map[string]any) error { + return yaml.Unmarshal(b, &v) +} diff --git a/internal/encoding/yaml/codec_test.go b/internal/encoding/yaml/codec_test.go new file mode 100644 index 000000000..2d2d8ba6a --- /dev/null +++ b/internal/encoding/yaml/codec_test.go @@ -0,0 +1,128 @@ +package yaml + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// original form of the data. +const original = `# key-value pair +key: value +list: + - item1 + - item2 + - item3 +map: + key: value + +# nested +# map +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// encoded form of the data. +const encoded = `key: value +list: + - item1 + - item2 + - item3 +map: + key: value +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// decoded form of the data. +// +// In case of YAML it's slightly different from Viper's internal representation +// (e.g. map is decoded into a map with interface key). +var decoded = map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + "map": map[string]any{ + "key": "value", + }, + "nested_map": map[string]any{ + "map": map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +// data is Viper's internal representation. +var data = map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + "map": map[string]any{ + "key": "value", + }, + "nested_map": map[string]any{ + "map": map[string]any{ + "key": "value", + "list": []any{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +func TestCodec_Encode(t *testing.T) { + codec := Codec{} + + b, err := codec.Encode(data) + require.NoError(t, err) + + assert.Equal(t, encoded, string(b)) +} + +func TestCodec_Decode(t *testing.T) { + t.Run("OK", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(original), v) + require.NoError(t, err) + + assert.Equal(t, decoded, v) + }) + + t.Run("InvalidData", func(t *testing.T) { + codec := Codec{} + + v := map[string]any{} + + err := codec.Decode([]byte(`invalid data`), v) + require.Error(t, err) + + t.Logf("decoding failed as expected: %s", err) + }) +} diff --git a/internal/features/bind_struct.go b/internal/features/bind_struct.go new file mode 100644 index 000000000..89302c216 --- /dev/null +++ b/internal/features/bind_struct.go @@ -0,0 +1,5 @@ +//go:build viper_bind_struct + +package features + +const BindStruct = true diff --git a/internal/features/bind_struct_default.go b/internal/features/bind_struct_default.go new file mode 100644 index 000000000..edfaf73b6 --- /dev/null +++ b/internal/features/bind_struct_default.go @@ -0,0 +1,5 @@ +//go:build !viper_bind_struct + +package features + +const BindStruct = false diff --git a/internal/features/finder.go b/internal/features/finder.go new file mode 100644 index 000000000..983ea3a9d --- /dev/null +++ b/internal/features/finder.go @@ -0,0 +1,5 @@ +//go:build viper_finder + +package features + +const Finder = true diff --git a/internal/features/finder_default.go b/internal/features/finder_default.go new file mode 100644 index 000000000..89bcb06ee --- /dev/null +++ b/internal/features/finder_default.go @@ -0,0 +1,5 @@ +//go:build !viper_finder + +package features + +const Finder = false diff --git a/internal/testutil/filepath.go b/internal/testutil/filepath.go new file mode 100644 index 000000000..e49be7ea8 --- /dev/null +++ b/internal/testutil/filepath.go @@ -0,0 +1,18 @@ +package testutil + +import ( + "path/filepath" + "testing" +) + +// AbsFilePath calls filepath.Abs on path. +func AbsFilePath(t *testing.T, path string) string { + t.Helper() + + s, err := filepath.Abs(path) + if err != nil { + t.Fatal(err) + } + + return s +} diff --git a/logger.go b/logger.go new file mode 100644 index 000000000..828042f29 --- /dev/null +++ b/logger.go @@ -0,0 +1,31 @@ +package viper + +import ( + "context" + "log/slog" +) + +// WithLogger sets a custom logger. +func WithLogger(l *slog.Logger) Option { + return optionFunc(func(v *Viper) { + v.logger = l + }) +} + +type discardHandler struct{} + +func (n *discardHandler) Enabled(_ context.Context, _ slog.Level) bool { + return false +} + +func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error { + return nil +} + +func (n *discardHandler) WithAttrs(_ []slog.Attr) slog.Handler { + return n +} + +func (n *discardHandler) WithGroup(_ string) slog.Handler { + return n +} diff --git a/nohup.out b/nohup.out deleted file mode 100644 index 8973bf27b..000000000 --- a/nohup.out +++ /dev/null @@ -1 +0,0 @@ -QProcess::start: Process is already running diff --git a/overrides_test.go b/overrides_test.go index dd2aa9b0d..42da3ba37 100644 --- a/overrides_test.go +++ b/overrides_test.go @@ -1,7 +1,6 @@ package viper import ( - "fmt" "strings" "testing" @@ -46,11 +45,11 @@ func TestNestedOverrides(t *testing.T) { deepCheckValue(assert, v, overrideLayer, []string{"tom", "size"}, 4) // Case 4: key:value overridden by a map - v = overrideDefault(assert, "tom.size", 4, "tom", map[string]interface{}{"age": 10}) // "tom.size" is first given "4" as default value, then "tom" is overridden by map{"age":10} - assert.Equal(4, v.Get("tom.size")) // "tom.size" should still be reachable - assert.Equal(10, v.Get("tom.age")) // new value should be there - deepCheckValue(assert, v, overrideLayer, []string{"tom", "age"}, 10) // new value should be there - v = override(assert, "tom.size", 4, "tom", map[string]interface{}{"age": 10}) + v = overrideDefault(assert, "tom.size", 4, "tom", map[string]any{"age": 10}) // "tom.size" is first given "4" as default value, then "tom" is overridden by map{"age":10} + assert.Equal(4, v.Get("tom.size")) // "tom.size" should still be reachable + assert.Equal(10, v.Get("tom.age")) // new value should be there + deepCheckValue(assert, v, overrideLayer, []string{"tom", "age"}, 10) // new value should be there + v = override(assert, "tom.size", 4, "tom", map[string]any{"age": 10}) assert.Nil(v.Get("tom.size")) assert.Equal(10, v.Get("tom.age")) deepCheckValue(assert, v, overrideLayer, []string{"tom", "age"}, 10) @@ -75,10 +74,11 @@ func TestNestedOverrides(t *testing.T) { } } -func overrideDefault(assert *assert.Assertions, firstPath string, firstValue interface{}, secondPath string, secondValue interface{}) *Viper { +func overrideDefault(assert *assert.Assertions, firstPath string, firstValue any, secondPath string, secondValue any) *Viper { return overrideFromLayer(defaultLayer, assert, firstPath, firstValue, secondPath, secondValue) } -func override(assert *assert.Assertions, firstPath string, firstValue interface{}, secondPath string, secondValue interface{}) *Viper { + +func override(assert *assert.Assertions, firstPath string, firstValue any, secondPath string, secondValue any) *Viper { return overrideFromLayer(overrideLayer, assert, firstPath, firstValue, secondPath, secondValue) } @@ -93,11 +93,11 @@ func override(assert *assert.Assertions, firstPath string, firstValue interface{ // // After each assignment, the value is checked, retrieved both by its full path // and by its key sequence (successive maps). -func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, firstValue interface{}, secondPath string, secondValue interface{}) *Viper { +func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, firstValue any, secondPath string, secondValue any) *Viper { v := New() firstKeys := strings.Split(firstPath, v.keyDelim) if assert == nil || - len(firstKeys) == 0 || len(firstKeys[0]) == 0 { + len(firstKeys) == 0 || firstKeys[0] == "" { return v } @@ -115,7 +115,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir // Override and check new value secondKeys := strings.Split(secondPath, v.keyDelim) - if len(secondKeys) == 0 || len(secondKeys[0]) == 0 { + if len(secondKeys) == 0 || secondKeys[0] == "" { return v } v.Set(secondPath, secondValue) @@ -126,15 +126,15 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir } // deepCheckValue checks that all given keys correspond to a valid path in the -// configuration map of the given layer, and that the final value equals the one given -func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value interface{}) { +// configuration map of the given layer, and that the final value equals the one given. +func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value any) { if assert == nil || v == nil || - len(keys) == 0 || len(keys[0]) == 0 { + len(keys) == 0 || keys[0] == "" { return } // init - var val interface{} + var val any var ms string switch l { case defaultLayer: @@ -146,28 +146,25 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, } // loop through map - var m map[string]interface{} - err := false + var m map[string]any for _, k := range keys { if val == nil { - assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms)) + assert.Failf("%s is not a map[string]any", ms) return } // deep scan of the map to get the final value - switch val.(type) { - case map[interface{}]interface{}: + switch val := val.(type) { + case map[any]any: m = cast.ToStringMap(val) - case map[string]interface{}: - m = val.(map[string]interface{}) + case map[string]any: + m = val default: - assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms)) + assert.Failf("%s is not a map[string]any", ms) return } ms = ms + "[\"" + k + "\"]" val = m[k] } - if !err { - assert.Equal(value, val) - } + assert.Equal(value, val) } diff --git a/remote.go b/remote.go new file mode 100644 index 000000000..bdde7de26 --- /dev/null +++ b/remote.go @@ -0,0 +1,256 @@ +package viper + +import ( + "bytes" + "fmt" + "io" + "reflect" + "slices" +) + +// SupportedRemoteProviders are universally supported remote providers. +var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore", "nats"} + +func resetRemote() { + SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore", "nats"} +} + +type remoteConfigFactory interface { + Get(rp RemoteProvider) (io.Reader, error) + Watch(rp RemoteProvider) (io.Reader, error) + WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool) +} + +type RemoteResponse struct { + Value []byte + Error error +} + +// RemoteConfig is optional, see the remote package. +var RemoteConfig remoteConfigFactory + +// UnsupportedRemoteProviderError denotes encountering an unsupported remote +// provider. Currently only etcd and Consul are supported. +type UnsupportedRemoteProviderError string + +// Error returns the formatted remote provider error. +func (str UnsupportedRemoteProviderError) Error() string { + return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str)) +} + +// RemoteConfigError denotes encountering an error while trying to +// pull the configuration from the remote provider. +type RemoteConfigError string + +// Error returns the formatted remote provider error. +func (rce RemoteConfigError) Error() string { + return fmt.Sprintf("Remote Configurations Error: %s", string(rce)) +} + +type defaultRemoteProvider struct { + provider string + endpoint string + path string + secretKeyring string +} + +func (rp defaultRemoteProvider) Provider() string { + return rp.provider +} + +func (rp defaultRemoteProvider) Endpoint() string { + return rp.endpoint +} + +func (rp defaultRemoteProvider) Path() string { + return rp.path +} + +func (rp defaultRemoteProvider) SecretKeyring() string { + return rp.secretKeyring +} + +// RemoteProvider stores the configuration necessary +// to connect to a remote key/value store. +// Optional secretKeyring to unencrypt encrypted values +// can be provided. +type RemoteProvider interface { + Provider() string + Endpoint() string + Path() string + SecretKeyring() string +} + +// AddRemoteProvider adds a remote configuration source. +// Remote Providers are searched in the order they are added. +// provider is a string value: "etcd", "etcd3", "consul", "firestore" or "nats" are currently supported. +// endpoint is the url. etcd requires http://ip:port, consul requires ip:port, nats requires nats://ip:port +// path is the path in the k/v store to retrieve configuration +// To retrieve a config file called myapp.json from /configs/myapp.json +// you should set path to /configs and set config name (SetConfigName()) to +// "myapp". +func AddRemoteProvider(provider, endpoint, path string) error { + return v.AddRemoteProvider(provider, endpoint, path) +} + +func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { + if !slices.Contains(SupportedRemoteProviders, provider) { + return UnsupportedRemoteProviderError(provider) + } + if provider != "" && endpoint != "" { + v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint) + + rp := &defaultRemoteProvider{ + endpoint: endpoint, + provider: provider, + path: path, + } + if !v.providerPathExists(rp) { + v.remoteProviders = append(v.remoteProviders, rp) + } + } + return nil +} + +// AddSecureRemoteProvider adds a remote configuration source. +// Secure Remote Providers are searched in the order they are added. +// provider is a string value: "etcd", "etcd3", "consul", "firestore" or "nats" are currently supported. +// endpoint is the url. etcd requires http://ip:port consul requires ip:port +// secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg +// path is the path in the k/v store to retrieve configuration +// To retrieve a config file called myapp.json from /configs/myapp.json +// you should set path to /configs and set config name (SetConfigName()) to +// "myapp". +// Secure Remote Providers are implemented with github.com/sagikazarmark/crypt. +func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { + return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring) +} + +func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { + if !slices.Contains(SupportedRemoteProviders, provider) { + return UnsupportedRemoteProviderError(provider) + } + if provider != "" && endpoint != "" { + v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint) + + rp := &defaultRemoteProvider{ + endpoint: endpoint, + provider: provider, + path: path, + secretKeyring: secretkeyring, + } + if !v.providerPathExists(rp) { + v.remoteProviders = append(v.remoteProviders, rp) + } + } + return nil +} + +func (v *Viper) providerPathExists(p *defaultRemoteProvider) bool { + for _, y := range v.remoteProviders { + if reflect.DeepEqual(y, p) { + return true + } + } + return false +} + +// ReadRemoteConfig attempts to get configuration from a remote source +// and read it in the remote configuration registry. +func ReadRemoteConfig() error { return v.ReadRemoteConfig() } + +func (v *Viper) ReadRemoteConfig() error { + return v.getKeyValueConfig() +} + +func WatchRemoteConfig() error { return v.WatchRemoteConfig() } +func (v *Viper) WatchRemoteConfig() error { + return v.watchKeyValueConfig() +} + +func (v *Viper) WatchRemoteConfigOnChannel() error { + return v.watchKeyValueConfigOnChannel() +} + +// Retrieve the first found remote configuration. +func (v *Viper) getKeyValueConfig() error { + if RemoteConfig == nil { + return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'") + } + + if len(v.remoteProviders) == 0 { + return RemoteConfigError("No Remote Providers") + } + + for _, rp := range v.remoteProviders { + val, err := v.getRemoteConfig(rp) + if err != nil { + v.logger.Error(fmt.Errorf("get remote config: %w", err).Error()) + + continue + } + + v.kvstore = val + + return nil + } + return RemoteConfigError("No Files Found") +} + +func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]any, error) { + reader, err := RemoteConfig.Get(provider) + if err != nil { + return nil, err + } + err = v.unmarshalReader(reader, v.kvstore) + return v.kvstore, err +} + +// Retrieve the first found remote configuration. +func (v *Viper) watchKeyValueConfigOnChannel() error { + if len(v.remoteProviders) == 0 { + return RemoteConfigError("No Remote Providers") + } + + for _, rp := range v.remoteProviders { + respc, _ := RemoteConfig.WatchChannel(rp) + // Todo: Add quit channel + go func(rc <-chan *RemoteResponse) { + for { + b := <-rc + reader := bytes.NewReader(b.Value) + v.unmarshalReader(reader, v.kvstore) + } + }(respc) + return nil + } + return RemoteConfigError("No Files Found") +} + +// Retrieve the first found remote configuration. +func (v *Viper) watchKeyValueConfig() error { + if len(v.remoteProviders) == 0 { + return RemoteConfigError("No Remote Providers") + } + + for _, rp := range v.remoteProviders { + val, err := v.watchRemoteConfig(rp) + if err != nil { + v.logger.Error(fmt.Errorf("watch remote config: %w", err).Error()) + + continue + } + v.kvstore = val + return nil + } + return RemoteConfigError("No Files Found") +} + +func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]any, error) { + reader, err := RemoteConfig.Watch(provider) + if err != nil { + return nil, err + } + err = v.unmarshalReader(reader, v.kvstore) + return v.kvstore, err +} diff --git a/remote/go.mod b/remote/go.mod new file mode 100644 index 000000000..d039b7193 --- /dev/null +++ b/remote/go.mod @@ -0,0 +1,90 @@ +module github.com/spf13/viper/remote + +go 1.21.0 + +replace github.com/spf13/viper => ../ + +require ( + github.com/sagikazarmark/crypt v0.25.0 + github.com/spf13/viper v1.20.0-alpha.6 +) + +require ( + cloud.google.com/go v0.115.1 // indirect + cloud.google.com/go/auth v0.9.1 // indirect + cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect + cloud.google.com/go/compute/metadata v0.5.0 // indirect + cloud.google.com/go/firestore v1.16.0 // indirect + cloud.google.com/go/longrunning v0.5.11 // indirect + github.com/armon/go-metrics v0.4.1 // indirect + github.com/coreos/go-semver v0.3.0 // indirect + github.com/coreos/go-systemd/v22 v22.3.2 // indirect + github.com/fatih/color v1.16.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-viper/mapstructure/v2 v2.1.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/s2a-go v0.1.8 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect + github.com/googleapis/gax-go/v2 v2.13.0 // indirect + github.com/hashicorp/consul/api v1.29.4 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-cleanhttp v0.5.2 // indirect + github.com/hashicorp/go-hclog v1.5.0 // indirect + github.com/hashicorp/go-immutable-radix v1.3.1 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/hashicorp/go-rootcerts v1.0.2 // indirect + github.com/hashicorp/golang-lru v0.5.4 // indirect + github.com/hashicorp/serf v0.10.1 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.17.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/nats-io/nats.go v1.37.0 // indirect + github.com/nats-io/nkeys v0.4.7 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/sagikazarmark/locafero v0.6.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.7.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.etcd.io/etcd/api/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/v2 v2.305.15 // indirect + go.etcd.io/etcd/client/v3 v3.5.15 // indirect + go.opencensus.io v0.24.0 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + go.uber.org/zap v1.21.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/oauth2 v0.22.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + golang.org/x/time v0.6.0 // indirect + google.golang.org/api v0.194.0 // indirect + google.golang.org/genproto v0.0.0-20240814211410-ddb44dafa142 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect + google.golang.org/grpc v1.66.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/remote/go.sum b/remote/go.sum new file mode 100644 index 000000000..e6cfc46e6 --- /dev/null +++ b/remote/go.sum @@ -0,0 +1,458 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.115.1 h1:Jo0SM9cQnSkYfp44+v+NQXHpcHqlnRJk2qxh6yvxxxQ= +cloud.google.com/go v0.115.1/go.mod h1:DuujITeaufu3gL68/lOFIirVNJwQeyf5UXyi+Wbgknc= +cloud.google.com/go/auth v0.9.1 h1:+pMtLEV2k0AXKvs/tGZojuj6QaioxfUjOpMsG5Gtx+w= +cloud.google.com/go/auth v0.9.1/go.mod h1:Sw8ocT5mhhXxFklyhT12Eiy0ed6tTrPMCJjSI8KhYLk= +cloud.google.com/go/auth/oauth2adapt v0.2.4 h1:0GWE/FUsXhf6C+jAkWgYm7X9tK8cuEIfy19DBn6B6bY= +cloud.google.com/go/auth/oauth2adapt v0.2.4/go.mod h1:jC/jOpwFP6JBxhB3P5Rr0a9HLMC/Pe3eaL4NmdvqPtc= +cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= +cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= +cloud.google.com/go/firestore v1.16.0 h1:YwmDHcyrxVRErWcgxunzEaZxtNbc8QoFYA/JOEwDPgc= +cloud.google.com/go/firestore v1.16.0/go.mod h1:+22v/7p+WNBSQwdSwP57vz47aZiY+HrDkrOsJNhk7rg= +cloud.google.com/go/longrunning v0.5.11 h1:Havn1kGjz3whCfoD8dxMLP73Ph5w+ODyZB9RUsDxtGk= +cloud.google.com/go/longrunning v0.5.11/go.mod h1:rDn7//lmlfWV1Dx6IB4RatCPenTwwmqXuiP0/RgoEO4= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJA= +github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= +github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-viper/mapstructure/v2 v2.1.0 h1:gHnMa2Y/pIxElCH2GlZZ1lZSsn6XMtufpGyP1XxdC/w= +github.com/go-viper/mapstructure/v2 v2.1.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= +github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/s2a-go v0.1.8 h1:zZDs9gcbt9ZPLV0ndSyQk6Kacx2g/X+SKYovpnz3SMM= +github.com/google/s2a-go v0.1.8/go.mod h1:6iNWHTpQ+nfNRN5E00MSdfDwVesa8hhS32PhPO8deJA= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= +github.com/googleapis/gax-go/v2 v2.13.0 h1:yitjD5f7jQHhyDsnhKEBU52NdvvdSeGzlAnDPT0hH1s= +github.com/googleapis/gax-go/v2 v2.13.0/go.mod h1:Z/fvTZXF8/uw7Xu5GuslPw+bplx6SS338j1Is2S+B7A= +github.com/hashicorp/consul/api v1.29.4 h1:P6slzxDLBOxUSj3fWo2o65VuKtbtOXFi7TSSgtXutuE= +github.com/hashicorp/consul/api v1.29.4/go.mod h1:HUlfw+l2Zy68ceJavv2zAyArl2fqhGWnMycyt56sBgg= +github.com/hashicorp/consul/proto-public v0.6.2 h1:+DA/3g/IiKlJZb88NBn0ZgXrxJp2NlvCZdEyl+qxvL0= +github.com/hashicorp/consul/proto-public v0.6.2/go.mod h1:cXXbOg74KBNGajC+o8RlA502Esf0R9prcoJgiOX/2Tg= +github.com/hashicorp/consul/sdk v0.16.1 h1:V8TxTnImoPD5cj0U9Spl0TUxcytjcbbJeADFF07KdHg= +github.com/hashicorp/consul/sdk v0.16.1/go.mod h1:fSXvwxB2hmh1FMZCNl6PwX0Q/1wdWtHJcZ7Ea5tns0s= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= +github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c= +github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= +github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI= +github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= +github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc= +github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc= +github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= +github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= +github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc= +github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM= +github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0= +github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY= +github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4= +github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= +github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= +github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= +github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE= +github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8= +github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI= +github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= +github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sagikazarmark/crypt v0.25.0 h1:sWxztEUybCJaQ+HNz4qGSPKbwX8BcpMvIVJB7rjcec0= +github.com/sagikazarmark/crypt v0.25.0/go.mod h1:nu6OUdMMWMOL+yEboAvzEwCPZawZY+efpjlqKyO4vsQ= +github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= +github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= +github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.etcd.io/etcd/api/v3 v3.5.15 h1:3KpLJir1ZEBrYuV2v+Twaa/e2MdDCEZ/70H+lzEiwsk= +go.etcd.io/etcd/api/v3 v3.5.15/go.mod h1:N9EhGzXq58WuMllgH9ZvnEr7SI9pS0k0+DHZezGp7jM= +go.etcd.io/etcd/client/pkg/v3 v3.5.15 h1:fo0HpWz/KlHGMCC+YejpiCmyWDEuIpnTDzpJLB5fWlA= +go.etcd.io/etcd/client/pkg/v3 v3.5.15/go.mod h1:mXDI4NAOwEiszrHCb0aqfAYNCrZP4e9hRca3d1YK8EU= +go.etcd.io/etcd/client/v2 v2.305.15 h1:VG2xbf8Vz1KJh65Ar2V5eDmfkp1bpzkSEHlhJM3usp8= +go.etcd.io/etcd/client/v2 v2.305.15/go.mod h1:Ad5dRjPVb/n5yXgAWQ/hXzuXXkBk0Y658ocuXYaUU48= +go.etcd.io/etcd/client/v3 v3.5.15 h1:23M0eY4Fd/inNv1ZfU3AxrbbOdW79r9V9Rl62Nm6ip4= +go.etcd.io/etcd/client/v3 v3.5.15/go.mod h1:CLSJxrYjvLtHsrPKsy7LmZEE+DK2ktfd2bN4RhBMwlU= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw= +go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= +go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= +golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= +golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.194.0 h1:dztZKG9HgtIpbI35FhfuSNR/zmaMVdxNlntHj1sIS4s= +google.golang.org/api v0.194.0/go.mod h1:AgvUFdojGANh3vI+P7EVnxj3AISHllxGCJSFmggmnd0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20240814211410-ddb44dafa142 h1:oLiyxGgE+rt22duwci1+TG7bg2/L1LQsXwfjPlmuJA0= +google.golang.org/genproto v0.0.0-20240814211410-ddb44dafa142/go.mod h1:G11eXq53iI5Q+kyNOmCvnzBaxEA2Q/Ik5Tj7nqBE8j4= +google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf h1:GillM0Ef0pkZPIB+5iO6SDK+4T9pf6TpaYR6ICD5rVE= +google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf/go.mod h1:OFMYQFHJ4TM3JRlWDZhJbZfra2uqc3WLBZiaaqP4DtU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c= +google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/remote/remote.go b/remote/remote.go index f36f03519..d61f88046 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -8,10 +8,13 @@ package remote import ( "bytes" - "github.com/spf13/viper" - crypt "github.com/xordataexchange/crypt/config" "io" "os" + "strings" + + crypt "github.com/sagikazarmark/crypt/config" + + "github.com/spf13/viper" ) type remoteConfigProvider struct{} @@ -33,52 +36,84 @@ func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) if err != nil { return nil, err } - resp,err := cm.Get(rp.Path()) + resp, err := cm.Get(rp.Path()) if err != nil { return nil, err } return bytes.NewReader(resp), nil } -func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *crypt.Response, chan bool) { + +func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) { cm, err := getConfigManager(rp) if err != nil { return nil, nil } quit := make(chan bool) - return cm.Watch(rp.Path(), quit) ,quit + quitwc := make(chan bool) + viperResponsCh := make(chan *viper.RemoteResponse) + cryptoResponseCh := cm.Watch(rp.Path(), quit) + // need this function to convert the Channel response form crypt.Response to viper.Response + go func(cr <-chan *crypt.Response, vr chan<- *viper.RemoteResponse, quitwc <-chan bool, quit chan<- bool) { + for { + select { + case <-quitwc: + quit <- true + return + case resp := <-cr: + vr <- &viper.RemoteResponse{ + Error: resp.Error, + Value: resp.Value, + } + } + } + }(cryptoResponseCh, viperResponsCh, quitwc, quit) + return viperResponsCh, quitwc } - func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { - var cm crypt.ConfigManager var err error + endpoints := strings.Split(rp.Endpoint(), ";") if rp.SecretKeyring() != "" { - kr, err := os.Open(rp.SecretKeyring()) - defer kr.Close() + var kr *os.File + kr, err = os.Open(rp.SecretKeyring()) if err != nil { return nil, err } - if rp.Provider() == "etcd" { - cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr) - } else { - cm, err = crypt.NewConsulConfigManager([]string{rp.Endpoint()}, kr) + defer kr.Close() + switch rp.Provider() { + case "etcd": + cm, err = crypt.NewEtcdConfigManager(endpoints, kr) + case "etcd3": + cm, err = crypt.NewEtcdV3ConfigManager(endpoints, kr) + case "firestore": + cm, err = crypt.NewFirestoreConfigManager(endpoints, kr) + case "nats": + cm, err = crypt.NewNatsConfigManager(endpoints, kr) + default: + cm, err = crypt.NewConsulConfigManager(endpoints, kr) } } else { - if rp.Provider() == "etcd" { - cm, err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()}) - } else { - cm, err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()}) + switch rp.Provider() { + case "etcd": + cm, err = crypt.NewStandardEtcdConfigManager(endpoints) + case "etcd3": + cm, err = crypt.NewStandardEtcdV3ConfigManager(endpoints) + case "firestore": + cm, err = crypt.NewStandardFirestoreConfigManager(endpoints) + case "nats": + cm, err = crypt.NewStandardNatsConfigManager(endpoints) + default: + cm, err = crypt.NewStandardConsulConfigManager(endpoints) } } if err != nil { return nil, err } return cm, nil - } func init() { diff --git a/util.go b/util.go index 3ebada91a..2a08074bc 100644 --- a/util.go +++ b/util.go @@ -11,22 +11,15 @@ package viper import ( - "bytes" - "encoding/json" "fmt" - "io" + "log/slog" "os" "path/filepath" "runtime" "strings" "unicode" - "github.com/hashicorp/hcl" - "github.com/magiconair/properties" - toml "github.com/pelletier/go-toml" "github.com/spf13/cast" - jww "github.com/spf13/jwalterweatherman" - "gopkg.in/yaml.v2" ) // ConfigParseError denotes failing to parse configuration file. @@ -39,13 +32,18 @@ func (pe ConfigParseError) Error() string { return fmt.Sprintf("While parsing config: %s", pe.err.Error()) } +// Unwrap returns the wrapped error. +func (pe ConfigParseError) Unwrap() error { + return pe.err +} + // toCaseInsensitiveValue checks if the value is a map; // if so, create a copy and lower-case the keys recursively. -func toCaseInsensitiveValue(value interface{}) interface{} { +func toCaseInsensitiveValue(value any) any { switch v := value.(type) { - case map[interface{}]interface{}: + case map[any]any: value = copyAndInsensitiviseMap(cast.ToStringMap(v)) - case map[string]interface{}: + case map[string]any: value = copyAndInsensitiviseMap(v) } @@ -54,15 +52,15 @@ func toCaseInsensitiveValue(value interface{}) interface{} { // copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of // any map it makes case insensitive. -func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} { - nm := make(map[string]interface{}) +func copyAndInsensitiviseMap(m map[string]any) map[string]any { + nm := make(map[string]any) for key, val := range m { lkey := strings.ToLower(key) switch v := val.(type) { - case map[interface{}]interface{}: + case map[any]any: nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v)) - case map[string]interface{}: + case map[string]any: nm[lkey] = copyAndInsensitiviseMap(v) default: nm[lkey] = v @@ -72,18 +70,25 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} { return nm } -func insensitiviseMap(m map[string]interface{}) { - for key, val := range m { - switch val.(type) { - case map[interface{}]interface{}: - // nested map: cast and recursively insensitivise - val = cast.ToStringMap(val) - insensitiviseMap(val.(map[string]interface{})) - case map[string]interface{}: - // nested map: recursively insensitivise - insensitiviseMap(val.(map[string]interface{})) - } +func insensitiviseVal(val any) any { + switch v := val.(type) { + case map[any]any: + // nested map: cast and recursively insensitivise + val = cast.ToStringMap(val) + insensitiviseMap(val.(map[string]any)) + case map[string]any: + // nested map: recursively insensitivise + insensitiviseMap(v) + case []any: + // nested array: recursively insensitivise + insensitiveArray(v) + } + return val +} +func insensitiviseMap(m map[string]any) { + for key, val := range m { + val = insensitiviseVal(val) lower := strings.ToLower(key) if key != lower { // remove old key (not lower-cased) @@ -94,17 +99,20 @@ func insensitiviseMap(m map[string]interface{}) { } } -func absPathify(inPath string) string { - jww.INFO.Println("Trying to resolve absolute path to", inPath) +func insensitiveArray(a []any) { + for i, val := range a { + a[i] = insensitiviseVal(val) + } +} - if strings.HasPrefix(inPath, "$HOME") { +func absPathify(logger *slog.Logger, inPath string) string { + logger.Info("trying to resolve absolute path", "path", inPath) + + if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) { inPath = userHomeDir() + inPath[5:] } - if strings.HasPrefix(inPath, "$") { - end := strings.Index(inPath, string(os.PathSeparator)) - inPath = os.Getenv(inPath[1:end]) + inPath[end:] - } + inPath = os.ExpandEnv(inPath) if filepath.IsAbs(inPath) { return filepath.Clean(inPath) @@ -115,30 +123,9 @@ func absPathify(inPath string) string { return filepath.Clean(p) } - jww.ERROR.Println("Couldn't discover absolute path") - jww.ERROR.Println(err) - return "" -} + logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error()) -// Check if File / Directory Exists -func exists(path string) (bool, error) { - _, err := v.fs.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} - -func stringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false + return "" } func userHomeDir() string { @@ -152,61 +139,6 @@ func userHomeDir() string { return os.Getenv("HOME") } -func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { - buf := new(bytes.Buffer) - buf.ReadFrom(in) - - switch strings.ToLower(configType) { - case "yaml", "yml": - if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { - return ConfigParseError{err} - } - - case "json": - if err := json.Unmarshal(buf.Bytes(), &c); err != nil { - return ConfigParseError{err} - } - - case "hcl": - obj, err := hcl.Parse(string(buf.Bytes())) - if err != nil { - return ConfigParseError{err} - } - if err = hcl.DecodeObject(&c, obj); err != nil { - return ConfigParseError{err} - } - - case "toml": - tree, err := toml.LoadReader(buf) - if err != nil { - return ConfigParseError{err} - } - tmap := tree.ToMap() - for k, v := range tmap { - c[k] = v - } - - case "properties", "props", "prop": - var p *properties.Properties - var err error - if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { - return ConfigParseError{err} - } - for _, key := range p.Keys() { - value, _ := p.Get(key) - // recursively build nested maps - path := strings.Split(key, ".") - lastKey := strings.ToLower(path[len(path)-1]) - deepestMap := deepSearch(c, path[0:len(path)-1]) - // set innermost value - deepestMap[lastKey] = value - } - } - - insensitiviseMap(c) - return nil -} - func safeMul(a, b uint) uint { c := a * b if a > 1 && b > 1 && c/b != a { @@ -215,7 +147,7 @@ func safeMul(a, b uint) uint { return c } -// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes +// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes. func parseSizeInBytes(sizeStr string) uint { sizeStr = strings.TrimSpace(sizeStr) lastChar := len(sizeStr) - 1 @@ -257,22 +189,22 @@ func parseSizeInBytes(sizeStr string) uint { // In case intermediate keys do not exist, or map to a non-map value, // a new map is created and inserted, and the search continues from there: // the initial map "m" may be modified! -func deepSearch(m map[string]interface{}, path []string) map[string]interface{} { +func deepSearch(m map[string]any, path []string) map[string]any { for _, k := range path { m2, ok := m[k] if !ok { // intermediate key does not exist // => create it and continue from there - m3 := make(map[string]interface{}) + m3 := make(map[string]any) m[k] = m3 m = m3 continue } - m3, ok := m2.(map[string]interface{}) + m3, ok := m2.(map[string]any) if !ok { // intermediate key is a value // => replace with a new map - m3 = make(map[string]interface{}) + m3 = make(map[string]any) m[k] = m3 } // continue search from here diff --git a/util_test.go b/util_test.go index 5949e09e4..3d7b30fe9 100644 --- a/util_test.go +++ b/util_test.go @@ -11,45 +11,76 @@ package viper import ( - "reflect" + "log/slog" + "os" + "path/filepath" "testing" + + "github.com/stretchr/testify/assert" ) func TestCopyAndInsensitiviseMap(t *testing.T) { - var ( - given = map[string]interface{}{ + given = map[string]any{ "Foo": 32, - "Bar": map[interface{}]interface { - }{ + "Bar": map[any]any{ "ABc": "A", - "cDE": "B"}, + "cDE": "B", + }, } - expected = map[string]interface{}{ + expected = map[string]any{ "foo": 32, - "bar": map[string]interface { - }{ + "bar": map[string]any{ "abc": "A", - "cde": "B"}, + "cde": "B", + }, } ) got := copyAndInsensitiviseMap(given) - if !reflect.DeepEqual(got, expected) { - t.Fatalf("Got %q\nexpected\n%q", got, expected) - } + assert.Equal(t, expected, got) + _, ok := given["foo"] + assert.False(t, ok) + _, ok = given["bar"] + assert.False(t, ok) - if _, ok := given["foo"]; ok { - t.Fatal("Input map changed") - } + m := given["Bar"].(map[any]any) + _, ok = m["ABc"] + assert.True(t, ok) +} + +func TestAbsPathify(t *testing.T) { + skipWindows(t) + + home := userHomeDir() + homer := filepath.Join(home, "homer") + wd, _ := os.Getwd() + + t.Setenv("HOMER_ABSOLUTE_PATH", homer) + t.Setenv("VAR_WITH_RELATIVE_PATH", "relative") - if _, ok := given["bar"]; ok { - t.Fatal("Input map changed") + tests := []struct { + input string + output string + }{ + {"", wd}, + {"sub", filepath.Join(wd, "sub")}, + {"./", wd}, + {"./sub", filepath.Join(wd, "sub")}, + {"$HOME", home}, + {"$HOME/", home}, + {"$HOME/sub", filepath.Join(home, "sub")}, + {"$HOMER_ABSOLUTE_PATH", homer}, + {"$HOMER_ABSOLUTE_PATH/", homer}, + {"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")}, + {"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")}, + {"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")}, + {"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")}, } - m := given["Bar"].(map[interface{}]interface{}) - if _, ok := m["ABc"]; !ok { - t.Fatal("Input map changed") + for _, test := range tests { + got := absPathify(slog.Default(), test.input) + assert.Equal(t, test.output, got) } } diff --git a/viper.go b/viper.go index 56278f3a8..754eb5aac 100644 --- a/viper.go +++ b/viper.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. -// Viper is a application configuration system. +// Viper is an application configuration system. // It believes that applications can be configured a variety of ways // via flags, ENVIRONMENT variables, configuration files retrieved // from the file system, or a remote key/value store. @@ -21,39 +21,45 @@ package viper import ( "bytes" + "encoding/csv" + "errors" "fmt" "io" - "log" + "log/slog" "os" "path/filepath" "reflect" + "slices" + "strconv" "strings" + "sync" "time" "github.com/fsnotify/fsnotify" - "github.com/mitchellh/mapstructure" + "github.com/go-viper/mapstructure/v2" "github.com/spf13/afero" "github.com/spf13/cast" - jww "github.com/spf13/jwalterweatherman" "github.com/spf13/pflag" - crypt "github.com/xordataexchange/crypt/config" + + "github.com/spf13/viper/internal/features" ) +// ConfigMarshalError happens when failing to marshal the configuration. +type ConfigMarshalError struct { + err error +} + +// Error returns the formatted configuration error. +func (e ConfigMarshalError) Error() string { + return fmt.Sprintf("While marshaling config: %s", e.err.Error()) +} + var v *Viper func init() { v = New() } -type remoteConfigFactory interface { - Get(rp RemoteProvider) (io.Reader, error) - Watch(rp RemoteProvider) (io.Reader, error) - WatchChannel(rp RemoteProvider)(<-chan *crypt.Response, chan bool) -} - -// RemoteConfig is optional, see the remote package -var RemoteConfig remoteConfigFactory - // UnsupportedConfigError denotes encountering an unsupported // configuration filetype. type UnsupportedConfigError string @@ -63,25 +69,6 @@ func (str UnsupportedConfigError) Error() string { return fmt.Sprintf("Unsupported Config Type %q", string(str)) } -// UnsupportedRemoteProviderError denotes encountering an unsupported remote -// provider. Currently only etcd and Consul are -// supported. -type UnsupportedRemoteProviderError string - -// Error returns the formatted remote provider error. -func (str UnsupportedRemoteProviderError) Error() string { - return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str)) -} - -// RemoteConfigError denotes encountering an error while trying to -// pull the configuration from the remote provider. -type RemoteConfigError string - -// Error returns the formatted remote provider error -func (rce RemoteConfigError) Error() string { - return fmt.Sprintf("Remote Configurations Error: %s", string(rce)) -} - // ConfigFileNotFoundError denotes failing to find configuration file. type ConfigFileNotFoundError struct { name, locations string @@ -92,6 +79,31 @@ func (fnfe ConfigFileNotFoundError) Error() string { return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations) } +// ConfigFileAlreadyExistsError denotes failure to write new configuration file. +type ConfigFileAlreadyExistsError string + +// Error returns the formatted error when configuration already exists. +func (faee ConfigFileAlreadyExistsError) Error() string { + return fmt.Sprintf("Config File %q Already Exists", string(faee)) +} + +// A DecoderConfigOption can be passed to viper.Unmarshal to configure +// mapstructure.DecoderConfig options. +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(","), +// ) +func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption { + return func(c *mapstructure.DecoderConfig) { + c.DecodeHook = hook + } +} + // Viper is a prioritized configuration registry. It // maintains a set of configuration sources, fetches // values to populate those, and provides them according @@ -106,18 +118,18 @@ func (fnfe ConfigFileNotFoundError) Error() string { // // 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: // @@ -126,6 +138,8 @@ func (fnfe ConfigFileNotFoundError) Error() string { // "user": "root", // "endpoint": "https://localhost" // } +// +// Note: Vipers are not safe for concurrent Get() and Set() operations. type Viper struct { // Delimiter that separates a list of keys // used to access a nested value in one go @@ -137,28 +151,43 @@ type Viper struct { // The filesystem to read config from. fs afero.Fs + finder Finder + // A set of remote providers to search for the configuration remoteProviders []*defaultRemoteProvider // Name of file to look for inside the path - configName string - configFile string - configType string - envPrefix string + configName string + configFile string + configType string + configPermissions os.FileMode + envPrefix string automaticEnvApplied bool - envKeyReplacer *strings.Replacer - - config map[string]interface{} - override map[string]interface{} - defaults map[string]interface{} - kvstore map[string]interface{} + envKeyReplacer StringReplacer + allowEmptyEnv bool + + parents []string + config map[string]any + override map[string]any + defaults map[string]any + kvstore map[string]any pflags map[string]FlagValue - env map[string]string + env map[string][]string aliases map[string]string typeByDefValue bool onConfigChange func(fsnotify.Event) + + logger *slog.Logger + + encoderRegistry EncoderRegistry + decoderRegistry DecoderRegistry + + decodeHook mapstructure.DecodeHookFunc + + experimentalFinder bool + experimentalBindStruct bool } // New returns an initialized Viper instance. @@ -166,121 +195,198 @@ func New() *Viper { v := new(Viper) v.keyDelim = "." v.configName = "config" + v.configPermissions = os.FileMode(0o644) v.fs = afero.NewOsFs() - v.config = make(map[string]interface{}) - v.override = make(map[string]interface{}) - v.defaults = make(map[string]interface{}) - v.kvstore = make(map[string]interface{}) + v.config = make(map[string]any) + v.parents = []string{} + v.override = make(map[string]any) + v.defaults = make(map[string]any) + v.kvstore = make(map[string]any) v.pflags = make(map[string]FlagValue) - v.env = make(map[string]string) + v.env = make(map[string][]string) v.aliases = make(map[string]string) v.typeByDefValue = false + v.logger = slog.New(&discardHandler{}) + + codecRegistry := NewCodecRegistry() + + v.encoderRegistry = codecRegistry + v.decoderRegistry = codecRegistry + + v.experimentalFinder = features.Finder + v.experimentalBindStruct = features.BindStruct return v } -// Intended for testing, will reset all to default settings. -// In the public interface for the viper package so applications -// can use it in their testing as well. -func Reset() { - v = New() - SupportedExts = []string{"json", "toml", "yaml", "yml", "hcl"} - SupportedRemoteProviders = []string{"etcd", "consul"} +// Option configures Viper using the functional options paradigm popularized by Rob Pike and Dave Cheney. +// If you're unfamiliar with this style, +// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and +// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis. +type Option interface { + apply(v *Viper) } -type defaultRemoteProvider struct { - provider string - endpoint string - path string - secretKeyring string +type optionFunc func(v *Viper) + +func (fn optionFunc) apply(v *Viper) { + fn(v) } -func (rp defaultRemoteProvider) Provider() string { - return rp.provider +// KeyDelimiter sets the delimiter used for determining key parts. +// By default it's value is ".". +func KeyDelimiter(d string) Option { + return optionFunc(func(v *Viper) { + v.keyDelim = d + }) } -func (rp defaultRemoteProvider) Endpoint() string { - return rp.endpoint +// StringReplacer applies a set of replacements to a string. +type StringReplacer interface { + // Replace returns a copy of s with all replacements performed. + Replace(s string) string } -func (rp defaultRemoteProvider) Path() string { - return rp.path +// EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys. +func EnvKeyReplacer(r StringReplacer) Option { + return optionFunc(func(v *Viper) { + if r == nil { + return + } + + v.envKeyReplacer = r + }) } -func (rp defaultRemoteProvider) SecretKeyring() string { - return rp.secretKeyring +// WithDecodeHook sets a default decode hook for mapstructure. +func WithDecodeHook(h mapstructure.DecodeHookFunc) Option { + return optionFunc(func(v *Viper) { + if h == nil { + return + } + + v.decodeHook = h + }) } -// RemoteProvider stores the configuration necessary -// to connect to a remote key/value store. -// Optional secretKeyring to unencrypt encrypted values -// can be provided. -type RemoteProvider interface { - Provider() string - Endpoint() string - Path() string - SecretKeyring() string +// NewWithOptions creates a new Viper instance. +func NewWithOptions(opts ...Option) *Viper { + v := New() + + for _, opt := range opts { + opt.apply(v) + } + + return v } -// SupportedExts are universally supported extensions. -var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"} +// SetOptions sets the options on the global Viper instance. +// +// Be careful when using this function: subsequent calls may override options you set. +// It's always better to use a local Viper instance. +func SetOptions(opts ...Option) { + for _, opt := range opts { + opt.apply(v) + } +} + +// Reset is intended for testing, will reset all to default settings. +// In the public interface for the viper package so applications +// can use it in their testing as well. +func Reset() { + v = New() + SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} + + resetRemote() +} -// SupportedRemoteProviders are universally supported remote providers. -var SupportedRemoteProviders = []string{"etcd", "consul"} +// SupportedExts are universally supported extensions. +var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} +// OnConfigChange sets the event handler that is called when a config file changes. func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) } + +// OnConfigChange sets the event handler that is called when a config file changes. func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) { v.onConfigChange = run } +// WatchConfig starts watching a config file for changes. func WatchConfig() { v.WatchConfig() } + +// WatchConfig starts watching a config file for changes. func (v *Viper) WatchConfig() { + initWG := sync.WaitGroup{} + initWG.Add(1) go func() { watcher, err := fsnotify.NewWatcher() if err != nil { - log.Fatal(err) + v.logger.Error(fmt.Sprintf("failed to create watcher: %s", err)) + os.Exit(1) } defer watcher.Close() - // we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way filename, err := v.getConfigFile() if err != nil { - log.Println("error:", err) + v.logger.Error(fmt.Sprintf("get config file: %s", err)) + initWG.Done() return } configFile := filepath.Clean(filename) configDir, _ := filepath.Split(configFile) + realConfigFile, _ := filepath.EvalSymlinks(filename) - done := make(chan bool) + eventsWG := sync.WaitGroup{} + eventsWG.Add(1) go func() { for { select { - case event := <-watcher.Events: - // we only care about the config file - if filepath.Clean(event.Name) == configFile { - if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { - err := v.ReadInConfig() - if err != nil { - log.Println("error:", err) - } + case event, ok := <-watcher.Events: + if !ok { // 'Events' channel is closed + eventsWG.Done() + return + } + currentConfigFile, _ := filepath.EvalSymlinks(filename) + // we only care about the config file with the following cases: + // 1 - if the config file was modified or created + // 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement) + if (filepath.Clean(event.Name) == configFile && + (event.Has(fsnotify.Write) || event.Has(fsnotify.Create))) || + (currentConfigFile != "" && currentConfigFile != realConfigFile) { + realConfigFile = currentConfigFile + err := v.ReadInConfig() + if err != nil { + v.logger.Error(fmt.Sprintf("read config file: %s", err)) + } + if v.onConfigChange != nil { v.onConfigChange(event) } + } else if filepath.Clean(event.Name) == configFile && event.Has(fsnotify.Remove) { + eventsWG.Done() + return + } + + case err, ok := <-watcher.Errors: + if ok { // 'Errors' channel is not closed + v.logger.Error(fmt.Sprintf("watcher error: %s", err)) } - case err := <-watcher.Errors: - log.Println("error:", err) + eventsWG.Done() + return } } }() - watcher.Add(configDir) - <-done + initWG.Done() // done initializing the watch in this go routine, so the parent routine can move on... + eventsWG.Wait() // now, wait for event loop to end in this go-routine... }() + initWG.Wait() // make sure that the go routine above fully ended before returning } -// SetConfigFile explicitly defines the path, name and extension of the config file -// Viper will use this and not check any of the config paths +// 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) } + func (v *Viper) SetConfigFile(in string) { if in != "" { v.configFile = in @@ -288,15 +394,22 @@ func (v *Viper) SetConfigFile(in string) { } // SetEnvPrefix defines a prefix that ENVIRONMENT variables will use. -// E.g. if your prefix is "spf", the env registry -// will look for env. variables that start with "SPF_" +// E.g. if your prefix is "spf", the env registry will look for env +// variables that start with "SPF_". func SetEnvPrefix(in string) { v.SetEnvPrefix(in) } + func (v *Viper) SetEnvPrefix(in string) { if in != "" { v.envPrefix = in } } +func GetEnvPrefix() string { return v.GetEnvPrefix() } + +func (v *Viper) GetEnvPrefix() string { + return v.envPrefix +} + func (v *Viper) mergeWithEnvPrefix(in string) string { if v.envPrefix != "" { return strings.ToUpper(v.envPrefix + "_" + in) @@ -305,112 +418,59 @@ func (v *Viper) mergeWithEnvPrefix(in string) string { return strings.ToUpper(in) } +// AllowEmptyEnv tells Viper to consider set, +// but empty environment variables as valid values instead of falling back. +// For backward compatibility reasons this is false by default. +func AllowEmptyEnv(allowEmptyEnv bool) { v.AllowEmptyEnv(allowEmptyEnv) } + +func (v *Viper) AllowEmptyEnv(allowEmptyEnv bool) { + v.allowEmptyEnv = allowEmptyEnv +} + // TODO: should getEnv logic be moved into find(). Can generalize the use of // rewriting keys many things, Ex: Get('someKey') -> some_key -// (cammel case to snake case for JSON keys perhaps) +// (camel case to snake case for JSON keys perhaps) // getEnv is a wrapper around os.Getenv which replaces characters in the original -// key. This allows env vars which have different keys then the config object -// keys -func (v *Viper) getEnv(key string) string { +// key. This allows env vars which have different keys than the config object +// keys. +func (v *Viper) getEnv(key string) (string, bool) { if v.envKeyReplacer != nil { key = v.envKeyReplacer.Replace(key) } - return os.Getenv(key) + + val, ok := os.LookupEnv(key) + + return val, ok && (v.allowEmptyEnv || val != "") } -// ConfigFileUsed returns the file used to populate the config registry +// ConfigFileUsed returns the file used to populate the config registry. func ConfigFileUsed() string { return v.ConfigFileUsed() } func (v *Viper) ConfigFileUsed() string { return v.configFile } // AddConfigPath adds a path for Viper to search for the config file in. // Can be called multiple times to define multiple search paths. func AddConfigPath(in string) { v.AddConfigPath(in) } -func (v *Viper) AddConfigPath(in string) { - if in != "" { - absin := absPathify(in) - jww.INFO.Println("adding", absin, "to paths to search") - if !stringInSlice(absin, v.configPaths) { - v.configPaths = append(v.configPaths, absin) - } - } -} -// AddRemoteProvider adds a remote configuration source. -// Remote Providers are searched in the order they are added. -// provider is a string value, "etcd" or "consul" are currently supported. -// endpoint is the url. etcd requires http://ip:port consul requires ip:port -// path is the path in the k/v store to retrieve configuration -// To retrieve a config file called myapp.json from /configs/myapp.json -// you should set path to /configs and set config name (SetConfigName()) to -// "myapp" -func AddRemoteProvider(provider, endpoint, path string) error { - return v.AddRemoteProvider(provider, endpoint, path) -} -func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { - if !stringInSlice(provider, SupportedRemoteProviders) { - return UnsupportedRemoteProviderError(provider) - } - if provider != "" && endpoint != "" { - jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint) - rp := &defaultRemoteProvider{ - endpoint: endpoint, - provider: provider, - path: path, - } - if !v.providerPathExists(rp) { - v.remoteProviders = append(v.remoteProviders, rp) - } +func (v *Viper) AddConfigPath(in string) { + if v.finder != nil { + v.logger.Warn("ineffective call to function: custom finder takes precedence", slog.String("function", "AddConfigPath")) } - return nil -} -// AddSecureRemoteProvider adds a remote configuration source. -// Secure Remote Providers are searched in the order they are added. -// provider is a string value, "etcd" or "consul" are currently supported. -// endpoint is the url. etcd requires http://ip:port consul requires ip:port -// secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg -// path is the path in the k/v store to retrieve configuration -// To retrieve a config file called myapp.json from /configs/myapp.json -// you should set path to /configs and set config name (SetConfigName()) to -// "myapp" -// Secure Remote Providers are implemented with github.com/xordataexchange/crypt -func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { - return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring) -} - -func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { - if !stringInSlice(provider, SupportedRemoteProviders) { - return UnsupportedRemoteProviderError(provider) - } - if provider != "" && endpoint != "" { - jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint) - rp := &defaultRemoteProvider{ - endpoint: endpoint, - provider: provider, - path: path, - secretKeyring: secretkeyring, - } - if !v.providerPathExists(rp) { - v.remoteProviders = append(v.remoteProviders, rp) - } - } - return nil -} + if in != "" { + absin := absPathify(v.logger, in) -func (v *Viper) providerPathExists(p *defaultRemoteProvider) bool { - for _, y := range v.remoteProviders { - if reflect.DeepEqual(y, p) { - return true + v.logger.Info("adding path to search paths", "path", absin) + if !slices.Contains(v.configPaths, absin) { + v.configPaths = append(v.configPaths, absin) } } - return false } // searchMap recursively searches for a value for path in source map. // Returns nil if not found. // Note: This assumes that the path entries and map keys are lower cased. -func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} { +func (v *Viper) searchMap(source map[string]any, path []string) any { if len(path) == 0 { return source } @@ -423,13 +483,13 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac } // Nested case - switch next.(type) { - case map[interface{}]interface{}: + switch next := next.(type) { + case map[any]any: return v.searchMap(cast.ToStringMap(next), path[1:]) - case map[string]interface{}: + case map[string]any: // Type assertion is safe here since it is only reached // if the type of `next` is the same as the type being asserted - return v.searchMap(next.(map[string]interface{}), path[1:]) + return v.searchMap(next, path[1:]) default: // got a value but nested key expected, return "nil" for not found return nil @@ -438,9 +498,9 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac return nil } -// searchMapWithPathPrefixes recursively searches for a value for path in source map. +// searchIndexableWithPathPrefixes recursively searches for a value for path in source map/slice. // -// While searchMap() considers each path element as a single map key, this +// While searchMap() considers each path element as a single map key or slice index, this // function searches for, and prioritizes, merged path elements. // e.g., if in the source, "foo" is defined with a sub-key "bar", and "foo.bar" // is also defined, this latter value is returned for path ["foo", "bar"]. @@ -449,7 +509,7 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac // in their keys). // // Note: This assumes that the path entries and map keys are lower cased. -func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path []string) interface{} { +func (v *Viper) searchIndexableWithPathPrefixes(source any, path []string) any { if len(path) == 0 { return source } @@ -458,29 +518,86 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path [] for i := len(path); i > 0; i-- { prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim)) - next, ok := source[prefixKey] - if ok { - // Fast path - if i == len(path) { - return next - } - - // Nested case - var val interface{} - switch next.(type) { - case map[interface{}]interface{}: - val = v.searchMapWithPathPrefixes(cast.ToStringMap(next), path[i:]) - case map[string]interface{}: - // Type assertion is safe here since it is only reached - // if the type of `next` is the same as the type being asserted - val = v.searchMapWithPathPrefixes(next.(map[string]interface{}), path[i:]) - default: - // got a value but nested key expected, do nothing and look for next prefix - } - if val != nil { - return val - } + var val any + switch sourceIndexable := source.(type) { + case []any: + val = v.searchSliceWithPathPrefixes(sourceIndexable, prefixKey, i, path) + case map[string]any: + val = v.searchMapWithPathPrefixes(sourceIndexable, prefixKey, i, path) } + if val != nil { + return val + } + } + + // not found + return nil +} + +// searchSliceWithPathPrefixes searches for a value for path in sourceSlice +// +// This function is part of the searchIndexableWithPathPrefixes recurring search and +// should not be called directly from functions other than searchIndexableWithPathPrefixes. +func (v *Viper) searchSliceWithPathPrefixes( + sourceSlice []any, + prefixKey string, + pathIndex int, + path []string, +) any { + // if the prefixKey is not a number or it is out of bounds of the slice + index, err := strconv.Atoi(prefixKey) + if err != nil || len(sourceSlice) <= index { + return nil + } + + next := sourceSlice[index] + + // Fast path + if pathIndex == len(path) { + return next + } + + switch n := next.(type) { + case map[any]any: + return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:]) + case map[string]any, []any: + return v.searchIndexableWithPathPrefixes(n, path[pathIndex:]) + default: + // got a value but nested key expected, do nothing and look for next prefix + } + + // not found + return nil +} + +// searchMapWithPathPrefixes searches for a value for path in sourceMap +// +// This function is part of the searchIndexableWithPathPrefixes recurring search and +// should not be called directly from functions other than searchIndexableWithPathPrefixes. +func (v *Viper) searchMapWithPathPrefixes( + sourceMap map[string]any, + prefixKey string, + pathIndex int, + path []string, +) any { + next, ok := sourceMap[prefixKey] + if !ok { + return nil + } + + // Fast path + if pathIndex == len(path) { + return next + } + + // Nested case + switch n := next.(type) { + case map[any]any: + return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:]) + case map[string]any, []any: + return v.searchIndexableWithPathPrefixes(n, path[pathIndex:]) + default: + // got a value but nested key expected, do nothing and look for next prefix } // not found @@ -490,9 +607,10 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path [] // 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 -func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string { - var parentVal interface{} +// +// "foo.bar.baz" in a lower-priority map +func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]any) string { + var parentVal any for i := 1; i < len(path); i++ { parentVal = v.searchMap(m, path[0:i]) if parentVal == nil { @@ -500,9 +618,9 @@ func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) return "" } switch parentVal.(type) { - case map[interface{}]interface{}: + case map[any]any: continue - case map[string]interface{}: + case map[string]any: continue default: // parentVal is a regular value which shadows "path" @@ -515,13 +633,16 @@ 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 -func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string { +// +// "foo.bar.baz" in a lower-priority map +func (v *Viper) isPathShadowedInFlatMap(path []string, mi any) string { // unify input map var m map[string]interface{} - switch mi.(type) { - case map[string]string, map[string]FlagValue: - m = cast.ToStringMap(mi) + switch miv := mi.(type) { + case map[string]string: + m = castMapStringToMapInterface(miv) + case map[string]FlagValue: + m = castMapFlagToMapInterface(miv) default: return "" } @@ -540,13 +661,13 @@ 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 - var val string for i := 1; i < len(path); i++ { parentKey = strings.Join(path[0:i], v.keyDelim) - if val = v.getEnv(v.mergeWithEnvPrefix(parentKey)); val != "" { + if _, ok := v.getEnv(v.mergeWithEnvPrefix(parentKey)); ok { return parentKey } } @@ -562,12 +683,13 @@ 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) { v.typeByDefValue = enable } @@ -584,46 +706,61 @@ func GetViper() *Viper { // override, flag, env, config file, key/value store, default // // Get returns an interface. For a specific value use one of the Get____ methods. -func Get(key string) interface{} { return v.Get(key) } -func (v *Viper) Get(key string) interface{} { +func Get(key string) any { return v.Get(key) } + +func (v *Viper) Get(key string) any { lcaseKey := strings.ToLower(key) - val := v.find(lcaseKey) + val := v.find(lcaseKey, true) if val == nil { return nil } - valType := val if v.typeByDefValue { // TODO(bep) this branch isn't covered by a single test. + valType := val path := strings.Split(lcaseKey, v.keyDelim) defVal := v.searchMap(v.defaults, path) if defVal != nil { valType = defVal } - } - switch valType.(type) { - case bool: - return cast.ToBool(val) - case string: - return cast.ToString(val) - case int64, int32, int16, int8, int: - return cast.ToInt(val) - case float64, float32: - return cast.ToFloat64(val) - case time.Time: - return cast.ToTime(val) - case time.Duration: - return cast.ToDuration(val) - case []string: - return cast.ToStringSlice(val) + switch valType.(type) { + case bool: + return cast.ToBool(val) + case string: + return cast.ToString(val) + case int32, int16, int8, int: + return cast.ToInt(val) + case uint: + return cast.ToUint(val) + case uint32: + return cast.ToUint32(val) + case uint64: + return cast.ToUint64(val) + case int64: + return cast.ToInt64(val) + case float64, float32: + return cast.ToFloat64(val) + case time.Time: + return cast.ToTime(val) + case time.Duration: + return cast.ToDuration(val) + case []string: + return cast.ToStringSlice(val) + case []int: + return cast.ToIntSlice(val) + case []time.Duration: + return cast.ToDurationSlice(val) + } } + return val } // Sub returns new Viper instance representing a sub tree of this instance. // Sub is case-insensitive for a key. func Sub(key string) *Viper { return v.Sub(key) } + func (v *Viper) Sub(key string) *Viper { subv := New() data := v.Get(key) @@ -632,6 +769,12 @@ func (v *Viper) Sub(key string) *Viper { } if reflect.TypeOf(data).Kind() == reflect.Map { + subv.parents = append([]string(nil), v.parents...) + subv.parents = append(subv.parents, strings.ToLower(key)) + subv.automaticEnvApplied = v.automaticEnvApplied + subv.envPrefix = v.envPrefix + subv.envKeyReplacer = v.envKeyReplacer + subv.keyDelim = v.keyDelim subv.config = cast.ToStringMap(data) return subv } @@ -640,66 +783,119 @@ func (v *Viper) Sub(key string) *Viper { // GetString returns the value associated with the key as a string. func GetString(key string) string { return v.GetString(key) } + func (v *Viper) GetString(key string) string { return cast.ToString(v.Get(key)) } // GetBool returns the value associated with the key as a boolean. func GetBool(key string) bool { return v.GetBool(key) } + func (v *Viper) GetBool(key string) bool { return cast.ToBool(v.Get(key)) } // GetInt returns the value associated with the key as an integer. func GetInt(key string) int { return v.GetInt(key) } + func (v *Viper) GetInt(key string) int { return cast.ToInt(v.Get(key)) } +// GetInt32 returns the value associated with the key as an integer. +func GetInt32(key string) int32 { return v.GetInt32(key) } + +func (v *Viper) GetInt32(key string) int32 { + return cast.ToInt32(v.Get(key)) +} + // GetInt64 returns the value associated with the key as an integer. func GetInt64(key string) int64 { return v.GetInt64(key) } + func (v *Viper) GetInt64(key string) int64 { return cast.ToInt64(v.Get(key)) } +// GetUint returns the value associated with the key as an unsigned integer. +func GetUint(key string) uint { return v.GetUint(key) } + +func (v *Viper) GetUint(key string) uint { + return cast.ToUint(v.Get(key)) +} + +// GetUint16 returns the value associated with the key as an unsigned integer. +func GetUint16(key string) uint16 { return v.GetUint16(key) } + +func (v *Viper) GetUint16(key string) uint16 { + return cast.ToUint16(v.Get(key)) +} + +// GetUint32 returns the value associated with the key as an unsigned integer. +func GetUint32(key string) uint32 { return v.GetUint32(key) } + +func (v *Viper) GetUint32(key string) uint32 { + return cast.ToUint32(v.Get(key)) +} + +// GetUint64 returns the value associated with the key as an unsigned integer. +func GetUint64(key string) uint64 { return v.GetUint64(key) } + +func (v *Viper) GetUint64(key string) uint64 { + return cast.ToUint64(v.Get(key)) +} + // GetFloat64 returns the value associated with the key as a float64. func GetFloat64(key string) float64 { return v.GetFloat64(key) } + func (v *Viper) GetFloat64(key string) float64 { return cast.ToFloat64(v.Get(key)) } // GetTime returns the value associated with the key as time. func GetTime(key string) time.Time { return v.GetTime(key) } + func (v *Viper) GetTime(key string) time.Time { return cast.ToTime(v.Get(key)) } // GetDuration returns the value associated with the key as a duration. func GetDuration(key string) time.Duration { return v.GetDuration(key) } + func (v *Viper) GetDuration(key string) time.Duration { return cast.ToDuration(v.Get(key)) } +// GetIntSlice returns the value associated with the key as a slice of int values. +func GetIntSlice(key string) []int { return v.GetIntSlice(key) } + +func (v *Viper) GetIntSlice(key string) []int { + return cast.ToIntSlice(v.Get(key)) +} + // GetStringSlice returns the value associated with the key as a slice of strings. func GetStringSlice(key string) []string { return v.GetStringSlice(key) } + func (v *Viper) GetStringSlice(key string) []string { return cast.ToStringSlice(v.Get(key)) } // GetStringMap returns the value associated with the key as a map of interfaces. -func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) } -func (v *Viper) GetStringMap(key string) map[string]interface{} { +func GetStringMap(key string) map[string]any { return v.GetStringMap(key) } + +func (v *Viper) GetStringMap(key string) map[string]any { return cast.ToStringMap(v.Get(key)) } // GetStringMapString returns the value associated with the key as a map of strings. func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) } + func (v *Viper) GetStringMapString(key string) map[string]string { return cast.ToStringMapString(v.Get(key)) } // GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings. func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) } + func (v *Viper) GetStringMapStringSlice(key string) map[string][]string { return cast.ToStringMapStringSlice(v.Get(key)) } @@ -707,45 +903,114 @@ func (v *Viper) GetStringMapStringSlice(key string) map[string][]string { // GetSizeInBytes returns the size of the value associated with the given key // in bytes. func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) } + func (v *Viper) GetSizeInBytes(key string) uint { sizeStr := cast.ToString(v.Get(key)) return parseSizeInBytes(sizeStr) } // UnmarshalKey takes a single key and unmarshals it into a Struct. -func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) } -func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error { - return mapstructure.Decode(v.Get(key), rawVal) +func UnmarshalKey(key string, rawVal any, opts ...DecoderConfigOption) error { + return v.UnmarshalKey(key, rawVal, opts...) +} + +func (v *Viper) UnmarshalKey(key string, rawVal any, opts ...DecoderConfigOption) error { + return decode(v.Get(key), v.defaultDecoderConfig(rawVal, opts...)) } // Unmarshal unmarshals the config into a Struct. Make sure that the tags // on the fields of the structure are properly set. -func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) } -func (v *Viper) Unmarshal(rawVal interface{}) error { - err := decode(v.AllSettings(), defaultDecoderConfig(rawVal)) +func Unmarshal(rawVal any, opts ...DecoderConfigOption) error { + return v.Unmarshal(rawVal, opts...) +} + +func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error { + keys := v.AllKeys() + + if v.experimentalBindStruct { + // TODO: make this optional? + structKeys, err := v.decodeStructKeys(rawVal, opts...) + if err != nil { + return err + } + + keys = append(keys, structKeys...) + } + + // TODO: struct keys should be enough? + return decode(v.getSettings(keys), v.defaultDecoderConfig(rawVal, opts...)) +} + +func (v *Viper) decodeStructKeys(input any, opts ...DecoderConfigOption) ([]string, error) { + var structKeyMap map[string]any + err := decode(input, v.defaultDecoderConfig(&structKeyMap, opts...)) if err != nil { - return err + return nil, err } - v.insensitiviseMaps() + flattenedStructKeyMap := v.flattenAndMergeMap(map[string]bool{}, structKeyMap, "") - return nil + r := make([]string, 0, len(flattenedStructKeyMap)) + for v := range flattenedStructKeyMap { + r = append(r, v) + } + + return r, nil } -// defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot -// of time.Duration values -func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig { - return &mapstructure.DecoderConfig{ +// defaultDecoderConfig returns default mapstructure.DecoderConfig with support +// of time.Duration values & string slices. +func (v *Viper) defaultDecoderConfig(output any, opts ...DecoderConfigOption) *mapstructure.DecoderConfig { + decodeHook := v.decodeHook + if decodeHook == nil { + decodeHook = mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc(), + // mapstructure.StringToSliceHookFunc(","), + stringToWeakSliceHookFunc(","), + ) + } + + c := &mapstructure.DecoderConfig{ Metadata: nil, - Result: output, WeaklyTypedInput: true, - DecodeHook: mapstructure.StringToTimeDurationHookFunc(), + DecodeHook: decodeHook, + } + + for _, opt := range opts { + opt(c) + } + + // Do not allow overwriting the output + c.Result = output + + return c +} + +// As of mapstructure v2.0.0 StringToSliceHookFunc checks if the return type is a string slice. +// This function removes that check. +// TODO: implement a function that checks if the value can be converted to the return type and use it instead. +func stringToWeakSliceHookFunc(sep string) mapstructure.DecodeHookFunc { + return func( + f reflect.Type, + t reflect.Type, + data interface{}, + ) (interface{}, error) { + if f.Kind() != reflect.String || t.Kind() != reflect.Slice { + return data, nil + } + + raw := data.(string) + if raw == "" { + return []string{}, nil + } + + return strings.Split(raw, sep), nil } } -// A wrapper around mapstructure.Decode that mimics the WeakDecode functionality -func decode(input interface{}, config *mapstructure.DecoderConfig) error { +// decode is a wrapper around mapstructure.Decode that mimics the WeakDecode functionality. +func decode(input any, config *mapstructure.DecoderConfig) error { decoder, err := mapstructure.NewDecoder(config) if err != nil { return err @@ -755,24 +1020,34 @@ func decode(input interface{}, config *mapstructure.DecoderConfig) error { // UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent // in the destination struct. -func (v *Viper) UnmarshalExact(rawVal interface{}) error { - config := defaultDecoderConfig(rawVal) +func UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error { + return v.UnmarshalExact(rawVal, opts...) +} + +func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error { + config := v.defaultDecoderConfig(rawVal, opts...) config.ErrorUnused = true - err := decode(v.AllSettings(), config) + keys := v.AllKeys() - if err != nil { - return err - } + if v.experimentalBindStruct { + // TODO: make this optional? + structKeys, err := v.decodeStructKeys(rawVal, opts...) + if err != nil { + return err + } - v.insensitiviseMaps() + keys = append(keys, structKeys...) + } - return nil + // TODO: struct keys should be enough? + return decode(v.getSettings(keys), config) } // BindPFlags binds a full flag set to the configuration, using each flag's long // name as the config key. func BindPFlags(flags *pflag.FlagSet) error { return v.BindPFlags(flags) } + func (v *Viper) BindPFlags(flags *pflag.FlagSet) error { return v.BindFlagValues(pflagValueSet{flags}) } @@ -780,17 +1055,21 @@ 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 { + if flag == nil { + return fmt.Errorf("flag for %q is nil", key) + } return v.BindFlagValue(key, pflagValue{flag}) } // BindFlagValues binds a full FlagValue set to the configuration, using each flag's long // name as the config key. func BindFlagValues(flags FlagValueSet) error { return v.BindFlagValues(flags) } + func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) { flags.VisitAll(func(flag FlagValue) { if err = v.BindFlagValue(flag.Name(), flag); err != nil { @@ -801,12 +1080,8 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) { } // BindFlagValue binds a specific key to a FlagValue. -// Example(where serverCmd is a Cobra instance): -// -// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") -// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port")) -// func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) } + func (v *Viper) BindFlagValue(key string, flag FlagValue) error { if flag == nil { return fmt.Errorf("flag for %q is nil", key) @@ -818,36 +1093,50 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error { // BindEnv binds a Viper key to a ENV variable. // ENV variables are case sensitive. // If only a key is provided, it will use the env key matching the key, uppercased. +// If more arguments are provided, they will represent the env variable names that +// should bind to this key and will be taken in the specified order. // EnvPrefix will be used when set when env name is not provided. func BindEnv(input ...string) error { return v.BindEnv(input...) } + func (v *Viper) BindEnv(input ...string) error { - var key, envkey string if len(input) == 0 { - return fmt.Errorf("BindEnv missing key to bind to") + return fmt.Errorf("missing key to bind to") } - key = strings.ToLower(input[0]) + key := strings.ToLower(input[0]) if len(input) == 1 { - envkey = v.mergeWithEnvPrefix(key) + v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key)) } else { - envkey = input[1] + v.env[key] = append(v.env[key], input[1:]...) } - v.env[key] = envkey - return nil } +// MustBindEnv wraps BindEnv in a panic. +// If there is an error binding an environment variable, MustBindEnv will +// panic. +func MustBindEnv(input ...string) { v.MustBindEnv(input...) } + +func (v *Viper) MustBindEnv(input ...string) { + if err := v.BindEnv(input...); err != nil { + panic(fmt.Sprintf("error while binding environment variable: %v", err)) + } +} + // Given a key, find the value. -// Viper will check in the following order: -// flag, env, config file, key/value store, default. +// // Viper will check to see if an alias exists first. +// Viper will then check in the following order: +// flag, env, config file, key/value store. +// Lastly, if no value was found and flagDefault is true, and if the key +// corresponds to a flag, the flag's default value is returned. +// // Note: this assumes a lower-cased key given. -func (v *Viper) find(lcaseKey string) interface{} { - +func (v *Viper) find(lcaseKey string, flagDefault bool) any { var ( - val interface{} + val any exists bool path = strings.Split(lcaseKey, v.keyDelim) nested = len(path) > 1 @@ -880,9 +1169,25 @@ func (v *Viper) find(lcaseKey string) interface{} { return cast.ToInt(flag.ValueString()) case "bool": return cast.ToBool(flag.ValueString()) - case "stringSlice": + case "stringSlice", "stringArray": s := strings.TrimPrefix(flag.ValueString(), "[") - return strings.TrimSuffix(s, "]") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return res + case "intSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return cast.ToIntSlice(res) + case "durationSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + slice := strings.Split(s, ",") + return cast.ToDurationSlice(slice) + case "stringToString": + return stringToStringConv(flag.ValueString()) + case "stringToInt": + return stringToIntConv(flag.ValueString()) default: return flag.ValueString() } @@ -893,19 +1198,22 @@ func (v *Viper) find(lcaseKey string) interface{} { // Env override next if v.automaticEnvApplied { + envKey := strings.Join(append(v.parents, lcaseKey), ".") // even if it hasn't been registered, if automaticEnv is used, // check any Get request - if val = v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); val != "" { + if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok { return val } if nested && v.isPathShadowedInAutoEnv(path) != "" { return nil } } - envkey, exists := v.env[lcaseKey] + envkeys, exists := v.env[lcaseKey] if exists { - if val = v.getEnv(envkey); val != "" { - return val + for _, envkey := range envkeys { + if val, ok := v.getEnv(envkey); ok { + return val + } } } if nested && v.isPathShadowedInFlatMap(path, v.env) != "" { @@ -913,7 +1221,7 @@ func (v *Viper) find(lcaseKey string) interface{} { } // Config file next - val = v.searchMapWithPathPrefixes(v.config, path) + val = v.searchIndexableWithPathPrefixes(v.config, path) if val != nil { return val } @@ -939,38 +1247,115 @@ func (v *Viper) find(lcaseKey string) interface{} { return nil } - // last chance: if no other value is returned and a flag does exist for the value, - // get the flag's value even if the flag's value has not changed - if flag, exists := v.pflags[lcaseKey]; exists { - switch flag.ValueType() { - case "int", "int8", "int16", "int32", "int64": - return cast.ToInt(flag.ValueString()) - case "bool": - return cast.ToBool(flag.ValueString()) - case "stringSlice": - s := strings.TrimPrefix(flag.ValueString(), "[") - return strings.TrimSuffix(s, "]") - default: - return flag.ValueString() + if flagDefault { + // last chance: if no value is found and a flag does exist for the key, + // get the flag's default value even if the flag's value has not been set. + if flag, exists := v.pflags[lcaseKey]; exists { + switch flag.ValueType() { + case "int", "int8", "int16", "int32", "int64": + return cast.ToInt(flag.ValueString()) + case "bool": + return cast.ToBool(flag.ValueString()) + case "stringSlice", "stringArray": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return res + case "intSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return cast.ToIntSlice(res) + case "stringToString": + return stringToStringConv(flag.ValueString()) + case "stringToInt": + return stringToIntConv(flag.ValueString()) + case "durationSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + slice := strings.Split(s, ",") + return cast.ToDurationSlice(slice) + default: + return flag.ValueString() + } } + // last item, no need to check shadowing } - // last item, no need to check shadowing return nil } +func readAsCSV(val string) ([]string, error) { + if val == "" { + return []string{}, nil + } + stringReader := strings.NewReader(val) + csvReader := csv.NewReader(stringReader) + return csvReader.Read() +} + +// mostly copied from pflag's implementation of this operation here https://github.com/spf13/pflag/blob/master/string_to_string.go#L79 +// alterations are: errors are swallowed, map[string]any is returned in order to enable cast.ToStringMap. +func stringToStringConv(val string) any { + val = strings.Trim(val, "[]") + // An empty string would cause an empty map + if val == "" { + return map[string]any{} + } + r := csv.NewReader(strings.NewReader(val)) + ss, err := r.Read() + if err != nil { + return nil + } + out := make(map[string]any, len(ss)) + for _, pair := range ss { + k, vv, found := strings.Cut(pair, "=") + if !found { + return nil + } + out[k] = vv + } + return out +} + +// mostly copied from pflag's implementation of this operation here https://github.com/spf13/pflag/blob/d5e0c0615acee7028e1e2740a11102313be88de1/string_to_int.go#L68 +// alterations are: errors are swallowed, map[string]any is returned in order to enable cast.ToStringMap. +func stringToIntConv(val string) any { + val = strings.Trim(val, "[]") + // An empty string would cause an empty map + if val == "" { + return map[string]any{} + } + ss := strings.Split(val, ",") + out := make(map[string]any, len(ss)) + for _, pair := range ss { + k, vv, found := strings.Cut(pair, "=") + if !found { + return nil + } + var err error + out[k], err = strconv.Atoi(vv) + if err != nil { + return nil + } + } + return out +} + // IsSet checks to see if the key has been set in any of the data locations. // IsSet is case-insensitive for a key. func IsSet(key string) bool { return v.IsSet(key) } + func (v *Viper) IsSet(key string) bool { lcaseKey := strings.ToLower(key) - val := v.find(lcaseKey) + val := v.find(lcaseKey, false) return val != nil } -// AutomaticEnv has Viper check ENV variables for all. -// keys set in config, default & flags +// AutomaticEnv makes Viper check if environment variables match any of the existing keys +// (config, default or flags). If matching env vars are found, they are loaded into Viper. func AutomaticEnv() { v.AutomaticEnv() } + func (v *Viper) AutomaticEnv() { v.automaticEnvApplied = true } @@ -979,18 +1364,20 @@ func (v *Viper) AutomaticEnv() { // Useful for mapping an environmental variable to a key that does // not match it. func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) } + func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) { v.envKeyReplacer = r } -// Aliases provide another accessor for the same key. -// This enables one to change a name without breaking the application -func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) } -func (v *Viper) RegisterAlias(alias string, key string) { +// RegisterAlias creates an alias that provides another accessor for the same key. +// This enables one to change a name without breaking the application. +func RegisterAlias(alias, key string) { v.RegisterAlias(alias, key) } + +func (v *Viper) RegisterAlias(alias, key string) { v.registerAlias(alias, strings.ToLower(key)) } -func (v *Viper) registerAlias(alias string, key string) { +func (v *Viper) registerAlias(alias, key string) { alias = strings.ToLower(alias) if alias != key && alias != v.realKey(key) { _, exists := v.aliases[alias] @@ -1018,14 +1405,15 @@ func (v *Viper) registerAlias(alias string, key string) { v.aliases[alias] = key } } else { - jww.WARN.Println("Creating circular reference alias", alias, key, v.realKey(key)) + v.logger.Warn("creating circular reference alias", "alias", alias, "key", key, "real_key", v.realKey(key)) } } func (v *Viper) realKey(key string) string { newkey, exists := v.aliases[key] if exists { - jww.DEBUG.Println("Alias", key, "to", newkey) + v.logger.Debug("key is an alias", "alias", key, "to", newkey) + return v.realKey(newkey) } return key @@ -1033,19 +1421,23 @@ func (v *Viper) realKey(key string) string { // InConfig checks to see if the given key (or an alias) is in the config file. func InConfig(key string) bool { return v.InConfig(key) } + func (v *Viper) InConfig(key string) bool { + lcaseKey := strings.ToLower(key) + // if the requested key is an alias, then return the proper key - key = v.realKey(key) + lcaseKey = v.realKey(lcaseKey) + path := strings.Split(lcaseKey, v.keyDelim) - _, exists := v.config[key] - return exists + return v.searchIndexableWithPathPrefixes(v.config, path) != nil } // SetDefault sets the default value for this key. // SetDefault is case-insensitive for a key. // Default only used when no value is provided by the user via flag, config or ENV. -func SetDefault(key string, value interface{}) { v.SetDefault(key, value) } -func (v *Viper) SetDefault(key string, value interface{}) { +func SetDefault(key string, value any) { v.SetDefault(key, value) } + +func (v *Viper) SetDefault(key string, value any) { // If alias passed in, then set the proper default key = v.realKey(strings.ToLower(key)) value = toCaseInsensitiveValue(value) @@ -1058,12 +1450,13 @@ func (v *Viper) SetDefault(key string, value interface{}) { deepestMap[lastKey] = value } -// Set sets the value for the key in the override regiser. +// Set sets the value for the key in the override register. // Set is case-insensitive for a key. // Will be used instead of values obtained via // flags, config file, ENV, default, or key/value store. -func Set(key string, value interface{}) { v.Set(key, value) } -func (v *Viper) Set(key string, value interface{}) { +func Set(key string, value any) { v.Set(key, value) } + +func (v *Viper) Set(key string, value any) { // If alias passed in, then set the proper override key = v.realKey(strings.ToLower(key)) value = toCaseInsensitiveValue(value) @@ -1079,23 +1472,25 @@ func (v *Viper) Set(key string, value interface{}) { // ReadInConfig will discover and load the configuration file from disk // and key/value stores, searching in one of the defined paths. func ReadInConfig() error { return v.ReadInConfig() } + func (v *Viper) ReadInConfig() error { - jww.INFO.Println("Attempting to read in config file") + v.logger.Info("attempting to read in config file") filename, err := v.getConfigFile() if err != nil { return err } - if !stringInSlice(v.getConfigType(), SupportedExts) { + if !slices.Contains(SupportedExts, v.getConfigType()) { return UnsupportedConfigError(v.getConfigType()) } + v.logger.Debug("reading file", "file", filename) file, err := afero.ReadFile(v.fs, filename) if err != nil { return err } - config := make(map[string]interface{}) + config := make(map[string]any) err = v.unmarshalReader(bytes.NewReader(file), config) if err != nil { @@ -1108,14 +1503,15 @@ func (v *Viper) ReadInConfig() error { // MergeInConfig merges a new configuration with an existing config. func MergeInConfig() error { return v.MergeInConfig() } + func (v *Viper) MergeInConfig() error { - jww.INFO.Println("Attempting to merge in config file") + v.logger.Info("attempting to merge in config file") filename, err := v.getConfigFile() if err != nil { return err } - if !stringInSlice(v.getConfigType(), SupportedExts) { + if !slices.Contains(SupportedExts, v.getConfigType()) { return UnsupportedConfigError(v.getConfigType()) } @@ -1130,26 +1526,181 @@ func (v *Viper) MergeInConfig() error { // ReadConfig will read a configuration file, setting existing keys to nil if the // key does not exist in the file. func ReadConfig(in io.Reader) error { return v.ReadConfig(in) } + func (v *Viper) ReadConfig(in io.Reader) error { - v.config = make(map[string]interface{}) + if v.configType == "" { + return errors.New("cannot decode configuration: config type is not set") + } + + v.config = make(map[string]any) return v.unmarshalReader(in, v.config) } // MergeConfig merges a new configuration with an existing config. func MergeConfig(in io.Reader) error { return v.MergeConfig(in) } + func (v *Viper) MergeConfig(in io.Reader) error { - if v.config == nil { - v.config = make(map[string]interface{}) + if v.configType == "" { + return errors.New("cannot decode configuration: config type is not set") } - cfg := make(map[string]interface{}) + + cfg := make(map[string]any) if err := v.unmarshalReader(in, cfg); err != nil { return err } + return v.MergeConfigMap(cfg) +} + +// MergeConfigMap merges the configuration from the map given with an existing config. +// Note that the map given may be modified. +func MergeConfigMap(cfg map[string]any) error { return v.MergeConfigMap(cfg) } + +func (v *Viper) MergeConfigMap(cfg map[string]any) error { + if v.config == nil { + v.config = make(map[string]any) + } + insensitiviseMap(cfg) mergeMaps(cfg, v.config, nil) return nil } -func keyExists(k string, m map[string]interface{}) string { +// WriteConfig writes the current configuration to a file. +func WriteConfig() error { return v.WriteConfig() } + +func (v *Viper) WriteConfig() error { + filename, err := v.getConfigFile() + if err != nil { + return err + } + return v.writeConfig(filename, true) +} + +// SafeWriteConfig writes current configuration to file only if the file does not exist. +func SafeWriteConfig() error { return v.SafeWriteConfig() } + +func (v *Viper) SafeWriteConfig() error { + if len(v.configPaths) < 1 { + return errors.New("missing configuration for 'configPath'") + } + return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType)) +} + +// WriteConfigAs writes current configuration to a given filename. +func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) } + +func (v *Viper) WriteConfigAs(filename string) error { + return v.writeConfig(filename, true) +} + +// WriteConfigTo writes current configuration to an [io.Writer]. +func WriteConfigTo(w io.Writer) error { return v.WriteConfigTo(w) } + +func (v *Viper) WriteConfigTo(w io.Writer) error { + format := strings.ToLower(v.getConfigType()) + + if !slices.Contains(SupportedExts, format) { + return UnsupportedConfigError(format) + } + + return v.marshalWriter(w, format) +} + +// SafeWriteConfigAs writes current configuration to a given filename if it does not exist. +func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) } + +func (v *Viper) SafeWriteConfigAs(filename string) error { + alreadyExists, err := afero.Exists(v.fs, filename) + if alreadyExists && err == nil { + return ConfigFileAlreadyExistsError(filename) + } + return v.writeConfig(filename, false) +} + +func (v *Viper) writeConfig(filename string, force bool) error { + v.logger.Info("attempting to write configuration to file") + + var configType string + + ext := filepath.Ext(filename) + if ext != "" && ext != filepath.Base(filename) { + configType = ext[1:] + } else { + configType = v.configType + } + if configType == "" { + return fmt.Errorf("config type could not be determined for %s", filename) + } + + if !slices.Contains(SupportedExts, configType) { + return UnsupportedConfigError(configType) + } + if v.config == nil { + v.config = make(map[string]any) + } + flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY + if !force { + flags |= os.O_EXCL + } + f, err := v.fs.OpenFile(filename, flags, v.configPermissions) + if err != nil { + return err + } + defer f.Close() + + if err := v.marshalWriter(f, configType); err != nil { + return err + } + + return f.Sync() +} + +func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error { + buf := new(bytes.Buffer) + buf.ReadFrom(in) + + format := strings.ToLower(v.getConfigType()) + + if !slices.Contains(SupportedExts, format) { + return UnsupportedConfigError(format) + } + + decoder, err := v.decoderRegistry.Decoder(format) + if err != nil { + return ConfigParseError{err} + } + + err = decoder.Decode(buf.Bytes(), c) + if err != nil { + return ConfigParseError{err} + } + + insensitiviseMap(c) + return nil +} + +// Marshal a map into Writer. +func (v *Viper) marshalWriter(w io.Writer, configType string) error { + c := v.AllSettings() + + encoder, err := v.encoderRegistry.Encoder(configType) + if err != nil { + return ConfigMarshalError{err} + } + + b, err := encoder.Encode(c) + if err != nil { + return ConfigMarshalError{err} + } + + _, err = w.Write(b) + if err != nil { + return ConfigMarshalError{err} + } + + return nil +} + +func keyExists(k string, m map[string]any) string { lk := strings.ToLower(k) for mk := range m { lmk := strings.ToLower(mk) @@ -1161,24 +1712,33 @@ func keyExists(k string, m map[string]interface{}) string { } func castToMapStringInterface( - src map[interface{}]interface{}) map[string]interface{} { - tgt := map[string]interface{}{} + src map[any]any, +) map[string]any { + tgt := map[string]any{} for k, v := range src { tgt[fmt.Sprintf("%v", k)] = v } return tgt } -func castMapStringToMapInterface(src map[string]string) map[string]interface{} { - tgt := map[string]interface{}{} +func castMapStringSliceToMapInterface(src map[string][]string) map[string]any { + tgt := map[string]any{} for k, v := range src { tgt[k] = v } return tgt } -func castMapFlagToMapInterface(src map[string]FlagValue) map[string]interface{} { - tgt := map[string]interface{}{} +func castMapStringToMapInterface(src map[string]string) map[string]any { + tgt := map[string]any{} + for k, v := range src { + tgt[k] = v + } + return tgt +} + +func castMapFlagToMapInterface(src map[string]FlagValue) map[string]any { + tgt := map[string]any{} for k, v := range src { tgt[k] = v } @@ -1186,16 +1746,15 @@ func castMapFlagToMapInterface(src map[string]FlagValue) map[string]interface{} } // mergeMaps merges two maps. The `itgt` parameter is for handling go-yaml's -// insistence on parsing nested structures as `map[interface{}]interface{}` +// insistence on parsing nested structures as `map[any]any` // instead of using a `string` as the key for nest structures beyond one level // deep. Both map types are supported as there is a go-yaml fork that uses -// `map[string]interface{}` instead. -func mergeMaps( - src, tgt map[string]interface{}, itgt map[interface{}]interface{}) { +// `map[string]any` instead. +func mergeMaps(src, tgt map[string]any, itgt map[any]any) { for sk, sv := range src { tk := keyExists(sk, tgt) if tk == "" { - jww.TRACE.Printf("tk=\"\", tgt[%s]=%v", sk, sv) + v.logger.Debug("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv) tgt[sk] = sv if itgt != nil { itgt[sk] = sv @@ -1205,7 +1764,7 @@ func mergeMaps( tv, ok := tgt[tk] if !ok { - jww.TRACE.Printf("tgt[%s] != ok, tgt[%s]=%v", tk, sk, sv) + v.logger.Debug("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv) tgt[sk] = sv if itgt != nil { itgt[sk] = sv @@ -1215,28 +1774,52 @@ func mergeMaps( svType := reflect.TypeOf(sv) tvType := reflect.TypeOf(tv) - if svType != tvType { - jww.ERROR.Printf( - "svType != tvType; key=%s, st=%v, tt=%v, sv=%v, tv=%v", - sk, svType, tvType, sv, tv) - continue - } - jww.TRACE.Printf("processing key=%s, st=%v, tt=%v, sv=%v, tv=%v", - sk, svType, tvType, sv, tv) + v.logger.Debug( + "processing", + "key", sk, + "st", svType, + "tt", tvType, + "sv", sv, + "tv", tv, + ) switch ttv := tv.(type) { - case map[interface{}]interface{}: - jww.TRACE.Printf("merging maps (must convert)") - tsv := sv.(map[interface{}]interface{}) + case map[any]any: + v.logger.Debug("merging maps (must convert)") + tsv, ok := sv.(map[any]any) + if !ok { + v.logger.Error( + "Could not cast sv to map[any]any", + "key", sk, + "st", svType, + "tt", tvType, + "sv", sv, + "tv", tv, + ) + continue + } + ssv := castToMapStringInterface(tsv) stv := castToMapStringInterface(ttv) mergeMaps(ssv, stv, ttv) - case map[string]interface{}: - jww.TRACE.Printf("merging maps") - mergeMaps(sv.(map[string]interface{}), ttv, nil) + case map[string]any: + v.logger.Debug("merging maps") + tsv, ok := sv.(map[string]any) + if !ok { + v.logger.Error( + "Could not cast sv to map[string]any", + "key", sk, + "st", svType, + "tt", tvType, + "sv", sv, + "tv", tv, + ) + continue + } + mergeMaps(tsv, ttv, nil) default: - jww.TRACE.Printf("setting value") + v.logger.Debug("setting value") tgt[tk] = sv if itgt != nil { itgt[tk] = sv @@ -1245,120 +1828,23 @@ func mergeMaps( } } -// ReadRemoteConfig attempts to get configuration from a remote source -// and read it in the remote configuration registry. -func ReadRemoteConfig() error { return v.ReadRemoteConfig() } -func (v *Viper) ReadRemoteConfig() error { - return v.getKeyValueConfig() -} - -func WatchRemoteConfig() error { return v.WatchRemoteConfig() } -func (v *Viper) WatchRemoteConfig() error { - return v.watchKeyValueConfig() -} - -func (v *Viper) WatchRemoteConfigOnChannel() error { - return v.watchKeyValueConfigOnChannel() -} - -// Unmarshall a Reader into a map. -// Should probably be an unexported function. -func unmarshalReader(in io.Reader, c map[string]interface{}) error { - return v.unmarshalReader(in, c) -} - -func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { - return unmarshallConfigReader(in, c, v.getConfigType()) -} - -func (v *Viper) insensitiviseMaps() { - insensitiviseMap(v.config) - insensitiviseMap(v.defaults) - insensitiviseMap(v.override) - insensitiviseMap(v.kvstore) -} - -// Retrieve the first found remote configuration. -func (v *Viper) getKeyValueConfig() error { - if RemoteConfig == nil { - return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'") - } - - for _, rp := range v.remoteProviders { - val, err := v.getRemoteConfig(rp) - if err != nil { - continue - } - v.kvstore = val - return nil - } - return RemoteConfigError("No Files Found") -} - -func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) { - reader, err := RemoteConfig.Get(provider) - if err != nil { - return nil, err - } - err = v.unmarshalReader(reader, v.kvstore) - return v.kvstore, err -} - -// Retrieve the first found remote configuration. -func (v *Viper) watchKeyValueConfigOnChannel() error { - for _, rp := range v.remoteProviders { - respc, _ := RemoteConfig.WatchChannel(rp) - //Todo: Add quit channel - go func(rc <-chan *crypt.Response) { - for { - b := <-rc - reader := bytes.NewReader(b.Value) - v.unmarshalReader(reader, v.kvstore) - } - }(respc) - return nil - } - return RemoteConfigError("No Files Found") -} - -// Retrieve the first found remote configuration. -func (v *Viper) watchKeyValueConfig() error { - for _, rp := range v.remoteProviders { - val, err := v.watchRemoteConfig(rp) - if err != nil { - continue - } - v.kvstore = val - return nil - } - return RemoteConfigError("No Files Found") -} - -func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) { - reader, err := RemoteConfig.Watch(provider) - if err != nil { - return nil, err - } - err = v.unmarshalReader(reader, v.kvstore) - return v.kvstore, err -} - // AllKeys returns all keys holding a value, regardless of where they are set. -// Nested keys are returned with a v.keyDelim (= ".") separator +// Nested keys are returned with a v.keyDelim separator. func AllKeys() []string { return v.AllKeys() } + func (v *Viper) AllKeys() []string { m := map[string]bool{} // add all paths, by order of descending priority to ensure correct shadowing m = v.flattenAndMergeMap(m, castMapStringToMapInterface(v.aliases), "") m = v.flattenAndMergeMap(m, v.override, "") m = v.mergeFlatMap(m, castMapFlagToMapInterface(v.pflags)) - m = v.mergeFlatMap(m, castMapStringToMapInterface(v.env)) + m = v.mergeFlatMap(m, castMapStringSliceToMapInterface(v.env)) m = v.flattenAndMergeMap(m, v.config, "") m = v.flattenAndMergeMap(m, v.kvstore, "") m = v.flattenAndMergeMap(m, v.defaults, "") // convert set of paths to list - a := []string{} + a := make([]string, 0, len(m)) for x := range m { a = append(a, x) } @@ -1367,11 +1853,12 @@ 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 { +func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]any, prefix string) map[string]bool { if shadow != nil && prefix != "" && shadow[prefix] { // prefix is shadowed => nothing more to flatten return shadow @@ -1380,16 +1867,16 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac shadow = make(map[string]bool) } - var m2 map[string]interface{} + var m2 map[string]any if prefix != "" { prefix += v.keyDelim } for k, val := range m { fullKey := prefix + k - switch val.(type) { - case map[string]interface{}: - m2 = val.(map[string]interface{}) - case map[interface{}]interface{}: + switch val := val.(type) { + case map[string]any: + m2 = val + case map[any]any: m2 = cast.ToStringMap(val) default: // immediate value @@ -1404,10 +1891,10 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac // mergeFlatMap merges the given maps, excluding values of the second map // shadowed by values from the first map. -func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool { +func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]any) map[string]bool { // scan keys outer: - for k, _ := range m { + for k := range m { path := strings.Split(k, v.keyDelim) // scan intermediate paths var parentKey string @@ -1424,12 +1911,17 @@ outer: return shadow } -// AllSettings merges all settings and returns them as a map[string]interface{}. -func AllSettings() map[string]interface{} { return v.AllSettings() } -func (v *Viper) AllSettings() map[string]interface{} { - m := map[string]interface{}{} +// AllSettings merges all settings and returns them as a map[string]any. +func AllSettings() map[string]any { return v.AllSettings() } + +func (v *Viper) AllSettings() map[string]any { + return v.getSettings(v.AllKeys()) +} + +func (v *Viper) getSettings(keys []string) map[string]any { + m := map[string]any{} // start from the list of keys, and construct the map one value at a time - for _, k := range v.AllKeys() { + for _, k := range keys { value := v.Get(k) if value == nil { // should not happen, since AllKeys() returns only keys holding a value, @@ -1447,6 +1939,7 @@ func (v *Viper) AllSettings() map[string]interface{} { // SetFs sets the filesystem to use to read configuration. func SetFs(fs afero.Fs) { v.SetFs(fs) } + func (v *Viper) SetFs(fs afero.Fs) { v.fs = fs } @@ -1454,7 +1947,12 @@ func (v *Viper) SetFs(fs afero.Fs) { // SetConfigName sets name for the config file. // Does not include extension. func SetConfigName(in string) { v.SetConfigName(in) } + func (v *Viper) SetConfigName(in string) { + if v.finder != nil { + v.logger.Warn("ineffective call to function: custom finder takes precedence", slog.String("function", "SetConfigName")) + } + if in != "" { v.configName = in v.configFile = "" @@ -1464,12 +1962,20 @@ func (v *Viper) SetConfigName(in string) { // SetConfigType sets the type of the configuration returned by the // remote source, e.g. "json". func SetConfigType(in string) { v.SetConfigType(in) } + func (v *Viper) SetConfigType(in string) { if in != "" { v.configType = in } } +// SetConfigPermissions sets the permissions for the config file. +func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) } + +func (v *Viper) SetConfigPermissions(perm os.FileMode) { + v.configPermissions = perm.Perm() +} + func (v *Viper) getConfigType() string { if v.configType != "" { return v.configType @@ -1490,57 +1996,29 @@ func (v *Viper) getConfigType() string { } func (v *Viper) getConfigFile() (string, error) { - // if explicitly set, then use it - if v.configFile != "" { - return v.configFile, nil - } - - cf, err := v.findConfigFile() - if err != nil { - return "", err - } - - v.configFile = cf - return v.getConfigFile() -} - -func (v *Viper) searchInPath(in string) (filename string) { - jww.DEBUG.Println("Searching for config in ", in) - for _, ext := range SupportedExts { - jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext)) - if b, _ := exists(filepath.Join(in, v.configName+"."+ext)); b { - jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) - return filepath.Join(in, v.configName+"."+ext) - } - } - - return "" -} - -// Search all configPaths for any config file. -// Returns the first path that exists (and is a config file). -func (v *Viper) findConfigFile() (string, error) { - - jww.INFO.Println("Searching for config in ", v.configPaths) - - for _, cp := range v.configPaths { - file := v.searchInPath(cp) - if file != "" { - return file, nil + if v.configFile == "" { + cf, err := v.findConfigFile() + if err != nil { + return "", err } + v.configFile = cf } - return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} + return v.configFile, nil } // Debug prints all configuration registries for debugging // purposes. -func Debug() { v.Debug() } -func (v *Viper) Debug() { - fmt.Printf("Aliases:\n%#v\n", v.aliases) - fmt.Printf("Override:\n%#v\n", v.override) - fmt.Printf("PFlags:\n%#v\n", v.pflags) - fmt.Printf("Env:\n%#v\n", v.env) - fmt.Printf("Key/Value Store:\n%#v\n", v.kvstore) - fmt.Printf("Config:\n%#v\n", v.config) - fmt.Printf("Defaults:\n%#v\n", v.defaults) +func Debug() { v.Debug() } +func DebugTo(w io.Writer) { v.DebugTo(w) } + +func (v *Viper) Debug() { v.DebugTo(os.Stdout) } + +func (v *Viper) DebugTo(w io.Writer) { + fmt.Fprintf(w, "Aliases:\n%#v\n", v.aliases) + fmt.Fprintf(w, "Override:\n%#v\n", v.override) + fmt.Fprintf(w, "PFlags:\n%#v\n", v.pflags) + fmt.Fprintf(w, "Env:\n%#v\n", v.env) + fmt.Fprintf(w, "Key/Value Store:\n%#v\n", v.kvstore) + fmt.Fprintf(w, "Config:\n%#v\n", v.config) + fmt.Fprintf(w, "Defaults:\n%#v\n", v.defaults) } diff --git a/viper_test.go b/viper_test.go index cd7b65cbd..2eab0eae4 100644 --- a/viper_test.go +++ b/viper_test.go @@ -7,38 +7,46 @@ package viper import ( "bytes" - "fmt" + "encoding/json" "io" - "io/ioutil" "os" + "os/exec" "path" + "path/filepath" "reflect" - "sort" + "runtime" "strings" + "sync" "testing" "time" + "github.com/fsnotify/fsnotify" + "github.com/go-viper/mapstructure/v2" + "github.com/sagikazarmark/locafero" + "github.com/spf13/afero" "github.com/spf13/cast" - "github.com/spf13/pflag" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/spf13/viper/internal/testutil" ) -var yamlExample = []byte(`Hacker: true -name: steve -hobbies: -- skateboarding -- snowboarding -- go -clothing: - jacket: leather - trousers: denim - pants: - size: large -age: 35 -eyes : brown -beard: true -`) +// var yamlExample = []byte(`Hacker: true +// name: steve +// hobbies: +// - skateboarding +// - snowboarding +// - go +// clothing: +// jacket: leather +// trousers: denim +// pants: +// size: large +// age: 35 +// eyes : brown +// beard: true +// `) var yamlExampleWithExtras = []byte(`Existing: true Bogus: true @@ -56,6 +64,11 @@ organization = "MongoDB" Bio = "MongoDB Chief Developer Advocate & Hacker at Large" dob = 1979-05-27T07:32:00Z # First class dates? Why not?`) +var dotenvExample = []byte(` +TITLE_DOTENV="DotEnv Example" +TYPE_DOTENV=donut +NAME_DOTENV=Cake`) + var jsonExample = []byte(`{ "id": "0001", "type": "donut", @@ -71,156 +84,73 @@ var jsonExample = []byte(`{ } }`) -var hclExample = []byte(` -id = "0001" -type = "donut" -name = "Cake" -ppu = 0.55 -foos { - foo { - key = 1 - } - foo { - key = 2 - } - foo { - key = 3 - } - foo { - key = 4 - } -}`) - -var propertiesExample = []byte(` -p_id: 0001 -p_type: donut -p_name: Cake -p_ppu: 0.55 -p_batters.batter.type: Regular -`) - var remoteExample = []byte(`{ "id":"0002", "type":"cronut", "newkey":"remote" }`) -func initConfigs() { - Reset() +func initConfigs(v *Viper) { var r io.Reader - SetConfigType("yaml") + v.SetConfigType("yaml") r = bytes.NewReader(yamlExample) - unmarshalReader(r, v.config) + v.unmarshalReader(r, v.config) - SetConfigType("json") + v.SetConfigType("json") r = bytes.NewReader(jsonExample) - unmarshalReader(r, v.config) - - SetConfigType("hcl") - r = bytes.NewReader(hclExample) - unmarshalReader(r, v.config) - - SetConfigType("properties") - r = bytes.NewReader(propertiesExample) - unmarshalReader(r, v.config) + v.unmarshalReader(r, v.config) - SetConfigType("toml") + v.SetConfigType("toml") r = bytes.NewReader(tomlExample) - unmarshalReader(r, v.config) + v.unmarshalReader(r, v.config) - SetConfigType("json") + v.SetConfigType("env") + r = bytes.NewReader(dotenvExample) + v.unmarshalReader(r, v.config) + + v.SetConfigType("json") remote := bytes.NewReader(remoteExample) - unmarshalReader(remote, v.kvstore) + v.unmarshalReader(remote, v.kvstore) } -func initConfig(typ, config string) { - Reset() - SetConfigType(typ) +func initConfig(typ, config string, v *Viper) { + v.SetConfigType(typ) r := strings.NewReader(config) - if err := unmarshalReader(r, v.config); err != nil { + if err := v.unmarshalReader(r, v.config); err != nil { panic(err) } } -func initYAML() { - initConfig("yaml", string(yamlExample)) -} - -func initJSON() { - Reset() - SetConfigType("json") - r := bytes.NewReader(jsonExample) - - unmarshalReader(r, v.config) -} - -func initProperties() { - Reset() - SetConfigType("properties") - r := bytes.NewReader(propertiesExample) - - unmarshalReader(r, v.config) -} - -func initTOML() { - Reset() - SetConfigType("toml") - r := bytes.NewReader(tomlExample) - - unmarshalReader(r, v.config) -} - -func initHcl() { - Reset() - SetConfigType("hcl") - r := bytes.NewReader(hclExample) - - unmarshalReader(r, v.config) -} - -// make directories for testing -func initDirs(t *testing.T) (string, string, func()) { - +// initDirs makes directories for testing. +func initDirs(t *testing.T) (string, string) { var ( - testDirs = []string{`a a`, `b`, `c\c`, `D_`} + testDirs = []string{`a a`, `b`, `C_`} config = `improbable` ) - root, err := ioutil.TempDir("", "") - - cleanup := true - defer func() { - if cleanup { - os.Chdir("..") - os.RemoveAll(root) - } - }() - - assert.Nil(t, err) + if runtime.GOOS != "windows" { + testDirs = append(testDirs, `d\d`) + } - err = os.Chdir(root) - assert.Nil(t, err) + root := t.TempDir() for _, dir := range testDirs { - err = os.Mkdir(dir, 0750) - assert.Nil(t, err) - - err = ioutil.WriteFile( - path.Join(dir, config+".toml"), - []byte("key = \"value is "+dir+"\"\n"), - 0640) - assert.Nil(t, err) + innerDir := filepath.Join(root, dir) + err := os.Mkdir(innerDir, 0o750) + require.NoError(t, err) + + err = os.WriteFile( + filepath.Join(innerDir, config+".toml"), + []byte(`key = "value is `+dir+`"`+"\n"), + 0o640) + require.NoError(t, err) } - cleanup = false - return root, config, func() { - os.Chdir("..") - os.RemoveAll(root) - } + return root, config } -//stubs for PFlag Values +// stubs for PFlag Values. type stringValue string func newStringValue(val string, p *string) *stringValue { @@ -238,211 +168,657 @@ func (s *stringValue) Type() string { } func (s *stringValue) String() string { - return fmt.Sprintf("%s", *s) + return string(*s) } -func TestBasics(t *testing.T) { - SetConfigFile("/tmp/config.yaml") - filename, err := v.getConfigFile() - assert.Equal(t, "/tmp/config.yaml", filename) - assert.NoError(t, err) +func TestGetConfigFile(t *testing.T) { + t.Run("config file set", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + v.SetConfigFile(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename) + assert.NoError(t, err) + }) + + t.Run("find file", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename) + assert.NoError(t, err) + }) + + t.Run("find files only", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/config"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/config/config.yaml")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc") + v.AddConfigPath("/etc/config") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/config/config.yaml"), filename) + assert.NoError(t, err) + }) + + t.Run("precedence", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/home/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/home/viper/config.zml")) + require.NoError(t, err) + + err = fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.bml")) + require.NoError(t, err) + + err = fs.Mkdir(testutil.AbsFilePath(t, "/var/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/var/viper/config.yaml")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/home/viper") + v.AddConfigPath("/etc/viper") + v.AddConfigPath("/var/viper") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/var/viper/config.yaml"), filename) + assert.NoError(t, err) + }) + + t.Run("without extension", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + v.SetConfigName(".dotfilenoext") + v.SetConfigType("yaml") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext"), filename) + assert.NoError(t, err) + }) + + t.Run("without extension and config type", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext")) + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + v.SetConfigName(".dotfilenoext") + + _, err = v.getConfigFile() + // unless config type is set, files without extension + // are not considered + assert.Error(t, err) + }) + + t.Run("experimental finder", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + v := NewWithOptions(ExperimentalFinder()) + + v.SetFs(fs) + + v.AddConfigPath("/etc/viper") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), testutil.AbsFilePath(t, filename)) + assert.NoError(t, err) + }) + + t.Run("finder", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + _, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + finder := locafero.Finder{ + Paths: []string{testutil.AbsFilePath(t, "/etc/viper")}, + Names: locafero.NameWithExtensions("config", SupportedExts...), + Type: locafero.FileTypeFile, + } + + v := NewWithOptions(WithFinder(finder)) + + v.SetFs(fs) + + // These should be ineffective + v.AddConfigPath("/etc/something_else") + v.SetConfigName("not-config") + + filename, err := v.getConfigFile() + assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), testutil.AbsFilePath(t, filename)) + assert.NoError(t, err) + }) +} + +func TestReadInConfig(t *testing.T) { + t.Run("config file set", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir("/etc/viper", 0o777) + require.NoError(t, err) + + file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + _, err = file.WriteString(`key: value`) + require.NoError(t, err) + + file.Close() + + v := New() + + v.SetFs(fs) + v.SetConfigFile(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + + err = v.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, "value", v.Get("key")) + }) + + t.Run("find file", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + _, err = file.WriteString(`key: value`) + require.NoError(t, err) + + file.Close() + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + + err = v.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, "value", v.Get("key")) + }) + + t.Run("find file with experimental finder", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + _, err = file.WriteString(`key: value`) + require.NoError(t, err) + + file.Close() + + v := NewWithOptions(ExperimentalFinder()) + + v.SetFs(fs) + v.AddConfigPath("/etc/viper") + + err = v.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, "value", v.Get("key")) + }) + + t.Run("find file using a finder", func(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) + require.NoError(t, err) + + file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) + require.NoError(t, err) + + _, err = file.WriteString(`key: value`) + require.NoError(t, err) + + file.Close() + + finder := locafero.Finder{ + Paths: []string{testutil.AbsFilePath(t, "/etc/viper")}, + Names: locafero.NameWithExtensions("config", SupportedExts...), + Type: locafero.FileTypeFile, + } + + v := NewWithOptions(WithFinder(finder)) + + v.SetFs(fs) + + // These should be ineffective + v.AddConfigPath("/etc/something_else") + v.SetConfigName("not-config") + + err = v.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, "value", v.Get("key")) + }) } func TestDefault(t *testing.T) { - SetDefault("age", 45) - assert.Equal(t, 45, Get("age")) + v := New() + v.SetDefault("age", 45) + assert.Equal(t, 45, v.Get("age")) - SetDefault("clothing.jacket", "slacks") - assert.Equal(t, "slacks", Get("clothing.jacket")) + v.SetDefault("clothing.jacket", "slacks") + assert.Equal(t, "slacks", v.Get("clothing.jacket")) - SetConfigType("yaml") - err := ReadConfig(bytes.NewBuffer(yamlExample)) + v.SetConfigType("yaml") + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) assert.NoError(t, err) - assert.Equal(t, "leather", Get("clothing.jacket")) + assert.Equal(t, "leather", v.Get("clothing.jacket")) } -func TestUnmarshalling(t *testing.T) { - SetConfigType("yaml") +func TestUnmarshaling(t *testing.T) { + v := New() + v.SetConfigType("yaml") r := bytes.NewReader(yamlExample) - unmarshalReader(r, v.config) - assert.True(t, InConfig("name")) - assert.False(t, InConfig("state")) - assert.Equal(t, "steve", Get("name")) - assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies")) - assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, Get("clothing")) - assert.Equal(t, 35, Get("age")) + v.unmarshalReader(r, v.config) + assert.True(t, v.InConfig("name")) + assert.True(t, v.InConfig("clothing.jacket")) + assert.False(t, v.InConfig("state")) + assert.False(t, v.InConfig("clothing.hat")) + assert.Equal(t, "steve", v.Get("name")) + assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies")) + assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing")) + assert.Equal(t, 35, v.Get("age")) } func TestUnmarshalExact(t *testing.T) { - vip := New() + v := New() target := &testUnmarshalExtra{} - vip.SetConfigType("yaml") + v.SetConfigType("yaml") r := bytes.NewReader(yamlExampleWithExtras) - vip.ReadConfig(r) - err := vip.UnmarshalExact(target) - if err == nil { - t.Fatal("UnmarshalExact should error when populating a struct from a conf that contains unused fields") - } + v.ReadConfig(r) + err := v.UnmarshalExact(target) + assert.Error(t, err, "UnmarshalExact should error when populating a struct from a conf that contains unused fields") } func TestOverrides(t *testing.T) { - Set("age", 40) - assert.Equal(t, 40, Get("age")) + v := New() + v.Set("age", 40) + assert.Equal(t, 40, v.Get("age")) } func TestDefaultPost(t *testing.T) { - assert.NotEqual(t, "NYC", Get("state")) - SetDefault("state", "NYC") - assert.Equal(t, "NYC", Get("state")) + v := New() + assert.NotEqual(t, "NYC", v.Get("state")) + v.SetDefault("state", "NYC") + assert.Equal(t, "NYC", v.Get("state")) } func TestAliases(t *testing.T) { - RegisterAlias("years", "age") - assert.Equal(t, 40, Get("years")) - Set("years", 45) - assert.Equal(t, 45, Get("age")) + v := New() + v.Set("age", 40) + v.RegisterAlias("years", "age") + assert.Equal(t, 40, v.Get("years")) + v.Set("years", 45) + assert.Equal(t, 45, v.Get("age")) } func TestAliasInConfigFile(t *testing.T) { - // the config file specifies "beard". If we make this an alias for - // "hasbeard", we still want the old config file to work with beard. - RegisterAlias("beard", "hasbeard") - assert.Equal(t, true, Get("hasbeard")) - Set("hasbeard", false) - assert.Equal(t, false, Get("beard")) + v := New() + + v.SetConfigType("yaml") + + // Read the YAML data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading YAML data") + + v.RegisterAlias("beard", "hasbeard") + assert.Equal(t, true, v.Get("hasbeard")) + + v.Set("hasbeard", false) + assert.Equal(t, false, v.Get("beard")) } func TestYML(t *testing.T) { - initYAML() - assert.Equal(t, "steve", Get("name")) + v := New() + v.SetConfigType("yaml") + + // Read the YAML data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading YAML data") + + assert.Equal(t, "steve", v.Get("name")) } func TestJSON(t *testing.T) { - initJSON() - assert.Equal(t, "0001", Get("id")) -} + v := New() + + v.SetConfigType("json") + // Read the JSON data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading JSON data") -func TestProperties(t *testing.T) { - initProperties() - assert.Equal(t, "0001", Get("p_id")) + assert.Equal(t, "0001", v.Get("id")) } func TestTOML(t *testing.T) { - initTOML() - assert.Equal(t, "TOML Example", Get("title")) + v := New() + v.SetConfigType("toml") + + // Read the TOML data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(tomlExample)), "Error reading toml data") + + assert.Equal(t, "TOML Example", v.Get("title")) } -func TestHCL(t *testing.T) { - initHcl() - assert.Equal(t, "0001", Get("id")) - assert.Equal(t, 0.55, Get("ppu")) - assert.Equal(t, "donut", Get("type")) - assert.Equal(t, "Cake", Get("name")) - Set("id", "0002") - assert.Equal(t, "0002", Get("id")) - assert.NotEqual(t, "cronut", Get("type")) +func TestDotEnv(t *testing.T) { + v := New() + v.SetConfigType("env") + // Read the dotenv data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(dotenvExample)), "Error reading env data") + + assert.Equal(t, "DotEnv Example", v.Get("title_dotenv")) } func TestRemotePrecedence(t *testing.T) { - initJSON() + v := New() + v.SetConfigType("json") + // Read the remote data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") + assert.Equal(t, "0001", v.Get("id")) + + // update the kvstore with the remoteExample which should overite the key in v.config remote := bytes.NewReader(remoteExample) - assert.Equal(t, "0001", Get("id")) - unmarshalReader(remote, v.kvstore) - assert.Equal(t, "0001", Get("id")) - assert.NotEqual(t, "cronut", Get("type")) - assert.Equal(t, "remote", Get("newkey")) - Set("newkey", "newvalue") - assert.NotEqual(t, "remote", Get("newkey")) - assert.Equal(t, "newvalue", Get("newkey")) - Set("newkey", "remote") + require.NoError(t, v.unmarshalReader(remote, v.kvstore), "Error reading json data in to kvstore") + + assert.Equal(t, "0001", v.Get("id")) + assert.NotEqual(t, "cronut", v.Get("type")) + assert.Equal(t, "remote", v.Get("newkey")) + v.Set("newkey", "newvalue") + assert.NotEqual(t, "remote", v.Get("newkey")) + assert.Equal(t, "newvalue", v.Get("newkey")) } func TestEnv(t *testing.T) { - initJSON() + v := New() + v.SetConfigType("json") + // Read the JSON data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") - BindEnv("id") - BindEnv("f", "FOOD") + v.BindEnv("id") + v.BindEnv("f", "FOOD", "OLD_FOOD") - os.Setenv("ID", "13") - os.Setenv("FOOD", "apple") - os.Setenv("NAME", "crunk") + t.Setenv("ID", "13") + t.Setenv("FOOD", "apple") + t.Setenv("OLD_FOOD", "banana") + t.Setenv("NAME", "crunk") - assert.Equal(t, "13", Get("id")) - assert.Equal(t, "apple", Get("f")) - assert.Equal(t, "Cake", Get("name")) + assert.Equal(t, "13", v.Get("id")) + assert.Equal(t, "apple", v.Get("f")) + assert.Equal(t, "Cake", v.Get("name")) + + v.AutomaticEnv() + + assert.Equal(t, "crunk", v.Get("name")) +} + +func TestMultipleEnv(t *testing.T) { + v := New() + v.SetConfigType("json") + // Read the JSON data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") - AutomaticEnv() + v.BindEnv("f", "FOOD", "OLD_FOOD") - assert.Equal(t, "crunk", Get("name")) + t.Setenv("OLD_FOOD", "banana") + assert.Equal(t, "banana", v.Get("f")) +} + +func TestEmptyEnv(t *testing.T) { + v := New() + v.SetConfigType("json") + // Read the JSON data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") + + v.BindEnv("type") // Empty environment variable + v.BindEnv("name") // Bound, but not set environment variable + + t.Setenv("TYPE", "") + + assert.Equal(t, "donut", v.Get("type")) + assert.Equal(t, "Cake", v.Get("name")) +} + +func TestEmptyEnv_Allowed(t *testing.T) { + v := New() + v.SetConfigType("json") + // Read the JSON data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") + + v.AllowEmptyEnv(true) + + v.BindEnv("type") // Empty environment variable + v.BindEnv("name") // Bound, but not set environment variable + + t.Setenv("TYPE", "") + + assert.Equal(t, "", v.Get("type")) + assert.Equal(t, "Cake", v.Get("name")) } func TestEnvPrefix(t *testing.T) { - initJSON() + v := New() + v.SetConfigType("json") + // Read the JSON data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data") - SetEnvPrefix("foo") // will be uppercased automatically - BindEnv("id") - BindEnv("f", "FOOD") // not using prefix + v.SetEnvPrefix("foo") // will be uppercased automatically + v.BindEnv("id") + v.BindEnv("f", "FOOD") // not using prefix - os.Setenv("FOO_ID", "13") - os.Setenv("FOOD", "apple") - os.Setenv("FOO_NAME", "crunk") + t.Setenv("FOO_ID", "13") + t.Setenv("FOOD", "apple") + t.Setenv("FOO_NAME", "crunk") - assert.Equal(t, "13", Get("id")) - assert.Equal(t, "apple", Get("f")) - assert.Equal(t, "Cake", Get("name")) + assert.Equal(t, "13", v.Get("id")) + assert.Equal(t, "apple", v.Get("f")) + assert.Equal(t, "Cake", v.Get("name")) - AutomaticEnv() + v.AutomaticEnv() - assert.Equal(t, "crunk", Get("name")) + assert.Equal(t, "crunk", v.Get("name")) } func TestAutoEnv(t *testing.T) { - Reset() + v := New() - AutomaticEnv() - os.Setenv("FOO_BAR", "13") - assert.Equal(t, "13", Get("foo_bar")) + v.AutomaticEnv() + + t.Setenv("FOO_BAR", "13") + + assert.Equal(t, "13", v.Get("foo_bar")) } func TestAutoEnvWithPrefix(t *testing.T) { - Reset() - - AutomaticEnv() - SetEnvPrefix("Baz") - os.Setenv("BAZ_BAR", "13") - assert.Equal(t, "13", Get("bar")) + v := New() + v.AutomaticEnv() + v.SetEnvPrefix("Baz") + t.Setenv("BAZ_BAR", "13") + assert.Equal(t, "13", v.Get("bar")) } -func TestSetEnvReplacer(t *testing.T) { - Reset() +func TestSetEnvKeyReplacer(t *testing.T) { + v := New() + v.AutomaticEnv() - AutomaticEnv() - os.Setenv("REFRESH_INTERVAL", "30s") + t.Setenv("REFRESH_INTERVAL", "30s") replacer := strings.NewReplacer("-", "_") - SetEnvKeyReplacer(replacer) + v.SetEnvKeyReplacer(replacer) - assert.Equal(t, "30s", Get("refresh-interval")) + assert.Equal(t, "30s", v.Get("refresh-interval")) } -func TestAllKeys(t *testing.T) { - initConfigs() +func TestEnvKeyReplacer(t *testing.T) { + v := NewWithOptions(EnvKeyReplacer(strings.NewReplacer("-", "_"))) + v.AutomaticEnv() + t.Setenv("REFRESH_INTERVAL", "30s") + assert.Equal(t, "30s", v.Get("refresh-interval")) +} - ks := sort.StringSlice{"title", "newkey", "owner.organization", "owner.dob", "owner.bio", "name", "beard", "ppu", "batters.batter", "hobbies", "clothing.jacket", "clothing.trousers", "clothing.pants.size", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name", "foos"} - dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") - all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[string]interface{}{"trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{"batter": map[string]interface{}{"type": "Regular"}}, "p_type": "donut", "foos": []map[string]interface{}{map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"key": 1}, map[string]interface{}{"key": 2}, map[string]interface{}{"key": 3}, map[string]interface{}{"key": 4}}}}} +func TestEnvSubConfig(t *testing.T) { + v := New() + v.SetConfigType("yaml") + // Read the YAML data into Viper configuration v.config + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading json data") + v.AutomaticEnv() + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - var allkeys sort.StringSlice - allkeys = AllKeys() - allkeys.Sort() - ks.Sort() + t.Setenv("CLOTHING_PANTS_SIZE", "small") + subv := v.Sub("clothing").Sub("pants") + assert.Equal(t, "small", subv.Get("size")) + + // again with EnvPrefix + v.SetEnvPrefix("foo") // will be uppercased automatically + subWithPrefix := v.Sub("clothing").Sub("pants") + t.Setenv("FOO_CLOTHING_PANTS_SIZE", "large") + assert.Equal(t, "large", subWithPrefix.Get("size")) +} + +func TestAllKeys(t *testing.T) { + v := New() + initConfigs(v) + + ks := []string{ + "title", + "newkey", + "owner.organization", + "owner.dob", + "owner.bio", + "name", + "beard", + "ppu", + "batters.batter", + "hobbies", + "clothing.jacket", + "clothing.trousers", + "clothing.pants.size", + "age", + "hacker", + "id", + "type", + "eyes", + "title_dotenv", + "type_dotenv", + "name_dotenv", + } + dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") + all := map[string]any{ + "owner": map[string]any{ + "organization": "MongoDB", + "bio": "MongoDB Chief Developer Advocate & Hacker at Large", + "dob": dob, + }, + "title": "TOML Example", + "ppu": 0.55, + "eyes": "brown", + "clothing": map[string]any{ + "trousers": "denim", + "jacket": "leather", + "pants": map[string]any{"size": "large"}, + }, + "id": "0001", + "batters": map[string]any{ + "batter": []any{ + map[string]any{"type": "Regular"}, + map[string]any{"type": "Chocolate"}, + map[string]any{"type": "Blueberry"}, + map[string]any{"type": "Devil's Food"}, + }, + }, + "hacker": true, + "beard": true, + "hobbies": []any{ + "skateboarding", + "snowboarding", + "go", + }, + "age": 35, + "type": "donut", + "newkey": "remote", + "name": "Cake", + "title_dotenv": "DotEnv Example", + "type_dotenv": "donut", + "name_dotenv": "Cake", + } - assert.Equal(t, ks, allkeys) - assert.Equal(t, all, AllSettings()) + assert.ElementsMatch(t, ks, v.AllKeys()) + assert.Equal(t, all, v.AllSettings()) } func TestAllKeysWithEnv(t *testing.T) { @@ -452,67 +828,278 @@ func TestAllKeysWithEnv(t *testing.T) { v.BindEnv("id") v.BindEnv("foo.bar") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - os.Setenv("ID", "13") - os.Setenv("FOO_BAR", "baz") - expectedKeys := sort.StringSlice{"id", "foo.bar"} - expectedKeys.Sort() - keys := sort.StringSlice(v.AllKeys()) - keys.Sort() - assert.Equal(t, expectedKeys, keys) + t.Setenv("ID", "13") + t.Setenv("FOO_BAR", "baz") + + assert.ElementsMatch(t, []string{"id", "foo.bar"}, v.AllKeys()) } func TestAliasesOfAliases(t *testing.T) { - Set("Title", "Checking Case") - RegisterAlias("Foo", "Bar") - RegisterAlias("Bar", "Title") - assert.Equal(t, "Checking Case", Get("FOO")) + v := New() + v.Set("Title", "Checking Case") + v.RegisterAlias("Foo", "Bar") + v.RegisterAlias("Bar", "Title") + assert.Equal(t, "Checking Case", v.Get("FOO")) } func TestRecursiveAliases(t *testing.T) { - RegisterAlias("Baz", "Roo") - RegisterAlias("Roo", "baz") + v := New() + v.Set("baz", "bat") + v.RegisterAlias("Baz", "Roo") + v.RegisterAlias("Roo", "baz") + assert.Equal(t, "bat", v.Get("Baz")) } func TestUnmarshal(t *testing.T) { - SetDefault("port", 1313) - Set("name", "Steve") - Set("duration", "1s1ms") + v := New() + v.SetDefault("port", 1313) + v.Set("name", "Steve") + v.Set("duration", "1s1ms") + v.Set("modes", []int{1, 2, 3}) type config struct { Port int Name string Duration time.Duration + Modes []int } var C config + require.NoError(t, v.Unmarshal(&C), "unable to decode into struct") + + assert.Equal( + t, + &config{ + Name: "Steve", + Port: 1313, + Duration: time.Second + time.Millisecond, + Modes: []int{1, 2, 3}, + }, + &C, + ) + + v.Set("port", 1234) + require.NoError(t, v.Unmarshal(&C), "unable to decode into struct") - err := Unmarshal(&C) - if err != nil { - t.Fatalf("unable to decode into struct, %v", err) + assert.Equal( + t, + &config{ + Name: "Steve", + Port: 1234, + Duration: time.Second + time.Millisecond, + Modes: []int{1, 2, 3}, + }, + &C, + ) +} + +func TestUnmarshalWithDefaultDecodeHook(t *testing.T) { + opt := mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc(), + mapstructure.StringToSliceHookFunc(","), + // Custom Decode Hook Function + func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) { + if rf != reflect.String || rt != reflect.Map { + return data, nil + } + m := map[string]string{} + raw := data.(string) + if raw == "" { + return m, nil + } + err := json.Unmarshal([]byte(raw), &m) + return m, err + }, + ) + + v := NewWithOptions(WithDecodeHook(opt)) + v.Set("credentials", "{\"foo\":\"bar\"}") + + type config struct { + Credentials map[string]string } - assert.Equal(t, &config{Name: "Steve", Port: 1313, Duration: time.Second + time.Millisecond}, &C) + var C config - Set("port", 1234) - err = Unmarshal(&C) - if err != nil { - t.Fatalf("unable to decode into struct, %v", err) + require.NoError(t, v.Unmarshal(&C), "unable to decode into struct") + + assert.Equal(t, &config{ + Credentials: map[string]string{"foo": "bar"}, + }, &C) +} + +func TestUnmarshalWithDecoderOptions(t *testing.T) { + v := New() + v.Set("credentials", "{\"foo\":\"bar\"}") + + opt := DecodeHook(mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc(), + mapstructure.StringToSliceHookFunc(","), + // Custom Decode Hook Function + func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) { + if rf != reflect.String || rt != reflect.Map { + return data, nil + } + m := map[string]string{} + raw := data.(string) + if raw == "" { + return m, nil + } + err := json.Unmarshal([]byte(raw), &m) + return m, err + }, + )) + + type config struct { + Credentials map[string]string + } + + var C config + + require.NoError(t, v.Unmarshal(&C, opt), "unable to decode into struct") + + assert.Equal(t, &config{ + Credentials: map[string]string{"foo": "bar"}, + }, &C) +} + +func TestUnmarshalWithAutomaticEnv(t *testing.T) { + t.Setenv("PORT", "1313") + t.Setenv("NAME", "Steve") + t.Setenv("DURATION", "1s1ms") + t.Setenv("MODES", "1,2,3") + t.Setenv("SECRET", "42") + t.Setenv("FILESYSTEM_SIZE", "4096") + + type AuthConfig struct { + Secret string `mapstructure:"secret"` + } + + type StorageConfig struct { + Size int `mapstructure:"size"` + } + + type Configuration struct { + Port int `mapstructure:"port"` + Name string `mapstructure:"name"` + Duration time.Duration `mapstructure:"duration"` + + // Infer name from struct + Modes []int + + // Squash nested struct (omit prefix) + Authentication AuthConfig `mapstructure:",squash"` + + // Different key + Storage StorageConfig `mapstructure:"filesystem"` + + // Omitted field + Flag bool `mapstructure:"flag"` } - assert.Equal(t, &config{Name: "Steve", Port: 1234, Duration: time.Second + time.Millisecond}, &C) + + v := NewWithOptions(ExperimentalBindStruct()) + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.AutomaticEnv() + + t.Run("OK", func(t *testing.T) { + var config Configuration + if err := v.Unmarshal(&config); err != nil { + t.Fatalf("unable to decode into struct, %v", err) + } + + assert.Equal( + t, + Configuration{ + Name: "Steve", + Port: 1313, + Duration: time.Second + time.Millisecond, + Modes: []int{1, 2, 3}, + Authentication: AuthConfig{ + Secret: "42", + }, + Storage: StorageConfig{ + Size: 4096, + }, + }, + config, + ) + }) + + t.Run("Precedence", func(t *testing.T) { + var config Configuration + + v.Set("port", 1234) + if err := v.Unmarshal(&config); err != nil { + t.Fatalf("unable to decode into struct, %v", err) + } + + assert.Equal( + t, + Configuration{ + Name: "Steve", + Port: 1234, + Duration: time.Second + time.Millisecond, + Modes: []int{1, 2, 3}, + Authentication: AuthConfig{ + Secret: "42", + }, + Storage: StorageConfig{ + Size: 4096, + }, + }, + config, + ) + }) + + t.Run("Unset", func(t *testing.T) { + var config Configuration + + err := v.Unmarshal(&config, func(config *mapstructure.DecoderConfig) { + config.ErrorUnset = true + }) + + assert.Error(t, err, "expected viper.Unmarshal to return error due to unset field 'FLAG'") + }) + + t.Run("Exact", func(t *testing.T) { + var config Configuration + + v.Set("port", 1234) + if err := v.UnmarshalExact(&config); err != nil { + t.Fatalf("unable to decode into struct, %v", err) + } + + assert.Equal( + t, + Configuration{ + Name: "Steve", + Port: 1234, + Duration: time.Second + time.Millisecond, + Modes: []int{1, 2, 3}, + Authentication: AuthConfig{ + Secret: "42", + }, + Storage: StorageConfig{ + Size: 4096, + }, + }, + config, + ) + }) } func TestBindPFlags(t *testing.T) { v := New() // create independent Viper object flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) - var testValues = map[string]*string{ + testValues := map[string]*string{ "host": nil, "port": nil, "endpoint": nil, } - var mutatedTestValues = map[string]string{ + mutatedTestValues := map[string]string{ "host": "localhost", "port": "6060", "endpoint": "/public", @@ -523,9 +1110,7 @@ func TestBindPFlags(t *testing.T) { } err := v.BindPFlags(flagSet) - if err != nil { - t.Fatalf("error binding flag set, %v", err) - } + require.NoError(t, err, "error binding flag set") flagSet.VisitAll(func(flag *pflag.Flag) { flag.Value.Set(mutatedTestValues[flag.Name]) @@ -535,12 +1120,159 @@ func TestBindPFlags(t *testing.T) { for name, expected := range mutatedTestValues { assert.Equal(t, expected, v.Get(name)) } - +} + +func TestBindPFlagsStringSlice(t *testing.T) { + tests := []struct { + Expected []string + Value string + }{ + {[]string{}, ""}, + {[]string{"jeden"}, "jeden"}, + {[]string{"dwa", "trzy"}, "dwa,trzy"}, + {[]string{"cztery", "piec , szesc"}, "cztery,\"piec , szesc\""}, + } + + v := New() // create independent Viper object + defaultVal := []string{"default"} + v.SetDefault("stringslice", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.StringSlice("stringslice", testValue.Expected, "test") + + for _, changed := range []bool{true, false} { + flagSet.VisitAll(func(f *pflag.Flag) { + f.Value.Set(testValue.Value) + f.Changed = changed + }) + + err := v.BindPFlags(flagSet) + require.NoError(t, err, "error binding flag set") + + type TestStr struct { + StringSlice []string + } + val := &TestStr{} + err = v.Unmarshal(val) + require.NoError(t, err, "cannot unmarshal") + if changed { + assert.Equal(t, testValue.Expected, val.StringSlice) + assert.Equal(t, testValue.Expected, v.Get("stringslice")) + } else { + assert.Equal(t, defaultVal, val.StringSlice) + } + } + } +} + +func TestBindPFlagsStringArray(t *testing.T) { + tests := []struct { + Expected []string + Value string + }{ + {[]string{}, ""}, + {[]string{"jeden"}, "jeden"}, + {[]string{"dwa,trzy"}, "dwa,trzy"}, + {[]string{"cztery,\"piec , szesc\""}, "cztery,\"piec , szesc\""}, + } + + v := New() // create independent Viper object + defaultVal := []string{"default"} + v.SetDefault("stringarray", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.StringArray("stringarray", testValue.Expected, "test") + + for _, changed := range []bool{true, false} { + flagSet.VisitAll(func(f *pflag.Flag) { + f.Value.Set(testValue.Value) + f.Changed = changed + }) + + err := v.BindPFlags(flagSet) + require.NoError(t, err, "error binding flag set") + + type TestStr struct { + StringArray []string + } + val := &TestStr{} + err = v.Unmarshal(val) + require.NoError(t, err, "cannot unmarshal") + if changed { + assert.Equal(t, testValue.Expected, val.StringArray) + assert.Equal(t, testValue.Expected, v.Get("stringarray")) + } else { + assert.Equal(t, defaultVal, val.StringArray) + } + } + } +} + +func TestSliceFlagsReturnCorrectType(t *testing.T) { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.IntSlice("int", []int{1, 2}, "") + flagSet.StringSlice("str", []string{"3", "4"}, "") + flagSet.DurationSlice("duration", []time.Duration{5 * time.Second}, "") + + v := New() + v.BindPFlags(flagSet) + + all := v.AllSettings() + + assert.IsType(t, []int{}, all["int"]) + assert.IsType(t, []string{}, all["str"]) + assert.IsType(t, []time.Duration{}, all["duration"]) +} + +func TestBindPFlagsIntSlice(t *testing.T) { + tests := []struct { + Expected []int + Value string + }{ + {[]int{}, ""}, + {[]int{1}, "1"}, + {[]int{2, 3}, "2,3"}, + } + + v := New() // create independent Viper object + defaultVal := []int{0} + v.SetDefault("intslice", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.IntSlice("intslice", testValue.Expected, "test") + + for _, changed := range []bool{true, false} { + flagSet.VisitAll(func(f *pflag.Flag) { + f.Value.Set(testValue.Value) + f.Changed = changed + }) + + err := v.BindPFlags(flagSet) + require.NoError(t, err, "error binding flag set") + + type TestInt struct { + IntSlice []int + } + val := &TestInt{} + err = v.Unmarshal(val) + require.NoError(t, err, "cannot unmarshal") + if changed { + assert.Equal(t, testValue.Expected, val.IntSlice) + assert.Equal(t, testValue.Expected, v.Get("intslice")) + } else { + assert.Equal(t, defaultVal, val.IntSlice) + } + } + } } func TestBindPFlag(t *testing.T) { - var testString = "testing" - var testValue = newStringValue(testString, &testString) + v := New() + testString := "testing" + testValue := newStringValue(testString, &testString) flag := &pflag.Flag{ Name: "testflag", @@ -548,27 +1280,123 @@ func TestBindPFlag(t *testing.T) { Changed: false, } - BindPFlag("testvalue", flag) + v.BindPFlag("testvalue", flag) - assert.Equal(t, testString, Get("testvalue")) + assert.Equal(t, testString, v.Get("testvalue")) flag.Value.Set("testing_mutate") - flag.Changed = true //hack for pflag usage + flag.Changed = true // hack for pflag usage + + assert.Equal(t, "testing_mutate", v.Get("testvalue")) +} + +func TestBindPFlagDetectNilFlag(t *testing.T) { + v := New() + result := v.BindPFlag("testvalue", nil) + assert.Error(t, result) +} + +func TestBindPFlagStringToString(t *testing.T) { + tests := []struct { + Expected map[string]string + Value string + }{ + {map[string]string{}, ""}, + {map[string]string{"yo": "hi"}, "yo=hi"}, + {map[string]string{"yo": "hi", "oh": "hi=there"}, "yo=hi,oh=hi=there"}, + {map[string]string{"yo": ""}, "yo="}, + {map[string]string{"yo": "", "oh": "hi=there"}, "yo=,oh=hi=there"}, + } + + v := New() // create independent Viper object + defaultVal := map[string]string{} + v.SetDefault("stringtostring", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.StringToString("stringtostring", testValue.Expected, "test") + + for _, changed := range []bool{true, false} { + flagSet.VisitAll(func(f *pflag.Flag) { + f.Value.Set(testValue.Value) + f.Changed = changed + }) + + err := v.BindPFlags(flagSet) + require.NoError(t, err, "error binding flag set") + + type TestMap struct { + StringToString map[string]string + } + val := &TestMap{} + err = v.Unmarshal(val) + require.NoError(t, err, "cannot unmarshal") + if changed { + assert.Equal(t, testValue.Expected, val.StringToString) + } else { + assert.Equal(t, defaultVal, val.StringToString) + } + } + } +} - assert.Equal(t, "testing_mutate", Get("testvalue")) +func TestBindPFlagStringToInt(t *testing.T) { + tests := []struct { + Expected map[string]int + Value string + }{ + {map[string]int{"yo": 1, "oh": 21}, "yo=1,oh=21"}, + {map[string]int{"yo": 100000000, "oh": 0}, "yo=100000000,oh=0"}, + {map[string]int{}, "yo=2,oh=21.0"}, + {map[string]int{}, "yo=,oh=20.99"}, + {map[string]int{}, "yo=,oh="}, + } + v := New() // create independent Viper object + defaultVal := map[string]int{} + v.SetDefault("stringtoint", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.StringToInt("stringtoint", testValue.Expected, "test") + + for _, changed := range []bool{true, false} { + flagSet.VisitAll(func(f *pflag.Flag) { + f.Value.Set(testValue.Value) + f.Changed = changed + }) + + err := v.BindPFlags(flagSet) + require.NoError(t, err, "error binding flag set") + + type TestMap struct { + StringToInt map[string]int + } + val := &TestMap{} + err = v.Unmarshal(val) + require.NoError(t, err, "cannot unmarshal") + if changed { + assert.Equal(t, testValue.Expected, val.StringToInt) + } else { + assert.Equal(t, defaultVal, val.StringToInt) + } + } + } } func TestBoundCaseSensitivity(t *testing.T) { - assert.Equal(t, "brown", Get("eyes")) + v := New() + initConfigs(v) + assert.Equal(t, "brown", v.Get("eyes")) + + v.BindEnv("eYEs", "TURTLE_EYES") - BindEnv("eYEs", "TURTLE_EYES") - os.Setenv("TURTLE_EYES", "blue") + t.Setenv("TURTLE_EYES", "blue") - assert.Equal(t, "blue", Get("eyes")) + assert.Equal(t, "blue", v.Get("eyes")) - var testString = "green" - var testValue = newStringValue(testString, &testString) + testString := "green" + testValue := newStringValue(testString, &testString) flag := &pflag.Flag{ Name: "eyeballs", @@ -576,9 +1404,8 @@ func TestBoundCaseSensitivity(t *testing.T) { Changed: true, } - BindPFlag("eYEs", flag) - assert.Equal(t, "green", Get("eyes")) - + v.BindPFlag("eYEs", flag) + assert.Equal(t, "green", v.Get("eyes")) } func TestSizeInBytes(t *testing.T) { @@ -599,62 +1426,68 @@ func TestSizeInBytes(t *testing.T) { } func TestFindsNestedKeys(t *testing.T) { - initConfigs() + v := New() + initConfigs(v) dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") - Set("super", map[string]interface{}{ - "deep": map[string]interface{}{ + v.Set("super", map[string]any{ + "deep": map[string]any{ "nested": "value", }, }) - expected := map[string]interface{}{ - "super": map[string]interface{}{ - "deep": map[string]interface{}{ + expected := map[string]any{ + "super": map[string]any{ + "deep": map[string]any{ "nested": "value", }, }, - "super.deep": map[string]interface{}{ + "super.deep": map[string]any{ "nested": "value", }, "super.deep.nested": "value", "owner.organization": "MongoDB", - "batters.batter": []interface{}{ - map[string]interface{}{ + "batters.batter": []any{ + map[string]any{ "type": "Regular", }, - map[string]interface{}{ + map[string]any{ "type": "Chocolate", }, - map[string]interface{}{ + map[string]any{ "type": "Blueberry", }, - map[string]interface{}{ + map[string]any{ "type": "Devil's Food", }, }, - "hobbies": []interface{}{ + "hobbies": []any{ "skateboarding", "snowboarding", "go", }, - "title": "TOML Example", - "newkey": "remote", - "batters": map[string]interface{}{ - "batter": []interface{}{ - map[string]interface{}{ + "TITLE_DOTENV": "DotEnv Example", + "TYPE_DOTENV": "donut", + "NAME_DOTENV": "Cake", + "title": "TOML Example", + "newkey": "remote", + "batters": map[string]any{ + "batter": []any{ + map[string]any{ "type": "Regular", }, - map[string]interface{}{ + map[string]any{ "type": "Chocolate", - }, map[string]interface{}{ + }, + map[string]any{ "type": "Blueberry", - }, map[string]interface{}{ + }, + map[string]any{ "type": "Devil's Food", }, }, }, "eyes": "brown", "age": 35, - "owner": map[string]interface{}{ + "owner": map[string]any{ "organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob, @@ -665,10 +1498,10 @@ func TestFindsNestedKeys(t *testing.T) { "name": "Cake", "hacker": true, "ppu": 0.55, - "clothing": map[string]interface{}{ + "clothing": map[string]any{ "jacket": "leather", "trousers": "denim", - "pants": map[string]interface{}{ + "pants": map[string]any{ "size": "large", }, }, @@ -677,84 +1510,108 @@ func TestFindsNestedKeys(t *testing.T) { "clothing.trousers": "denim", "owner.dob": dob, "beard": true, - "foos": []map[string]interface{}{ - map[string]interface{}{ - "foo": []map[string]interface{}{ - map[string]interface{}{ - "key": 1, - }, - map[string]interface{}{ - "key": 2, - }, - map[string]interface{}{ - "key": 3, - }, - map[string]interface{}{ - "key": 4, - }, - }, - }, - }, } for key, expectedValue := range expected { - assert.Equal(t, expectedValue, v.Get(key)) } - } -func TestReadBufConfig(t *testing.T) { - v := New() - v.SetConfigType("yaml") - v.ReadConfig(bytes.NewBuffer(yamlExample)) - t.Log(v.AllKeys()) +func TestReadConfig(t *testing.T) { + t.Run("ok", func(t *testing.T) { + v := New() + v.SetConfigType("yaml") + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) + require.NoError(t, err) + t.Log(v.AllKeys()) + + assert.True(t, v.InConfig("name")) + assert.True(t, v.InConfig("clothing.jacket")) + assert.False(t, v.InConfig("state")) + assert.False(t, v.InConfig("clothing.hat")) + assert.Equal(t, "steve", v.Get("name")) + assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies")) + assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing")) + assert.Equal(t, 35, v.Get("age")) + }) - assert.True(t, v.InConfig("name")) - assert.False(t, v.InConfig("state")) - assert.Equal(t, "steve", v.Get("name")) - assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies")) - assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, v.Get("clothing")) - assert.Equal(t, 35, v.Get("age")) + t.Run("missing config type", func(t *testing.T) { + v := New() + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) + require.Error(t, err) + }) } func TestIsSet(t *testing.T) { v := New() v.SetConfigType("yaml") + + /* config and defaults */ v.ReadConfig(bytes.NewBuffer(yamlExample)) + v.SetDefault("clothing.shoes", "sneakers") + + assert.True(t, v.IsSet("clothing")) assert.True(t, v.IsSet("clothing.jacket")) assert.False(t, v.IsSet("clothing.jackets")) + assert.True(t, v.IsSet("clothing.shoes")) + + /* state change */ assert.False(t, v.IsSet("helloworld")) v.Set("helloworld", "fubar") assert.True(t, v.IsSet("helloworld")) + + /* env */ + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.BindEnv("eyes") + v.BindEnv("foo") + v.BindEnv("clothing.hat") + v.BindEnv("clothing.hats") + + t.Setenv("FOO", "bar") + t.Setenv("CLOTHING_HAT", "bowler") + + assert.True(t, v.IsSet("eyes")) // in the config file + assert.True(t, v.IsSet("foo")) // in the environment + assert.True(t, v.IsSet("clothing.hat")) // in the environment + assert.False(t, v.IsSet("clothing.hats")) // not defined + + /* flags */ + flagset := pflag.NewFlagSet("testisset", pflag.ContinueOnError) + flagset.Bool("foobaz", false, "foobaz") + flagset.Bool("barbaz", false, "barbaz") + foobaz, barbaz := flagset.Lookup("foobaz"), flagset.Lookup("barbaz") + v.BindPFlag("foobaz", foobaz) + v.BindPFlag("barbaz", barbaz) + barbaz.Value.Set("true") + barbaz.Changed = true // hack for pflag usage + + assert.False(t, v.IsSet("foobaz")) + assert.True(t, v.IsSet("barbaz")) } func TestDirsSearch(t *testing.T) { - - root, config, cleanup := initDirs(t) - defer cleanup() + root, config := initDirs(t) v := New() v.SetConfigName(config) v.SetDefault(`key`, `default`) - entries, err := ioutil.ReadDir(root) + entries, err := os.ReadDir(root) + require.NoError(t, err) for _, e := range entries { if e.IsDir() { - v.AddConfigPath(e.Name()) + v.AddConfigPath(filepath.Join(root, e.Name())) } } err = v.ReadInConfig() - assert.Nil(t, err) + require.NoError(t, err) - assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) + assert.Equal(t, `value is `+filepath.Base(v.configPaths[0]), v.GetString(`key`)) } func TestWrongDirsSearchNotFound(t *testing.T) { - - _, config, cleanup := initDirs(t) - defer cleanup() + _, config := initDirs(t) v := New() v.SetConfigName(config) @@ -764,7 +1621,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) { v.AddConfigPath(`thispathaintthere`) err := v.ReadInConfig() - assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err)) + assert.IsType(t, err, ConfigFileNotFoundError{"", ""}) // Even though config did not load and the error might have // been ignored by the client, the default still loads @@ -772,9 +1629,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) { } func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { - - _, config, cleanup := initDirs(t) - defer cleanup() + _, config := initDirs(t) v := New() v.SetConfigName(config) @@ -791,6 +1646,17 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { assert.Equal(t, `default`, v.GetString(`key`)) } +var yamlInvalid = []byte(`hash: map +- foo +- bar +`) + +func TestUnwrapParseErrors(t *testing.T) { + v := New() + v.SetConfigType("yaml") + assert.ErrorAs(t, v.ReadConfig(bytes.NewBuffer(yamlInvalid)), &ConfigParseError{}) +} + func TestSub(t *testing.T) { v := New() v.SetConfigType("yaml") @@ -807,12 +1673,325 @@ func TestSub(t *testing.T) { subv = v.Sub("missing.key") assert.Equal(t, (*Viper)(nil), subv) + + subv = v.Sub("clothing") + assert.Equal(t, []string{"clothing"}, subv.parents) + + subv = v.Sub("clothing").Sub("pants") + assert.Equal(t, []string{"clothing", "pants"}, subv.parents) +} + +func TestSubWithKeyDelimiter(t *testing.T) { + v := NewWithOptions(KeyDelimiter("::")) + v.SetConfigType("yaml") + r := strings.NewReader(string(yamlExampleWithDot)) + err := v.unmarshalReader(r, v.config) + require.NoError(t, err) + + subv := v.Sub("emails") + assert.Equal(t, "01/02/03", subv.Get("steve@hacker.com::created")) +} + +var jsonWriteExpected = []byte(`{ + "batters": { + "batter": [ + { + "type": "Regular" + }, + { + "type": "Chocolate" + }, + { + "type": "Blueberry" + }, + { + "type": "Devil's Food" + } + ] + }, + "id": "0001", + "name": "Cake", + "ppu": 0.55, + "type": "donut" +}`) + +// var yamlWriteExpected = []byte(`age: 35 +// beard: true +// clothing: +// jacket: leather +// pants: +// size: large +// trousers: denim +// eyes: brown +// hacker: true +// hobbies: +// - skateboarding +// - snowboarding +// - go +// name: steve +// `) + +func TestWriteConfig(t *testing.T) { + fs := afero.NewMemMapFs() + testCases := map[string]struct { + configName string + inConfigType string + outConfigType string + fileName string + input []byte + expectedContent []byte + }{ + "json with file extension": { + configName: "c", + inConfigType: "json", + outConfigType: "json", + fileName: "c.json", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "json without file extension": { + configName: "c", + inConfigType: "json", + outConfigType: "json", + fileName: "c", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "json with file extension and mismatch type": { + configName: "c", + inConfigType: "json", + outConfigType: "hcl", + fileName: "c.json", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "yaml with file extension": { + configName: "c", + inConfigType: "yaml", + outConfigType: "yaml", + fileName: "c.yaml", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, + "yaml without file extension": { + configName: "c", + inConfigType: "yaml", + outConfigType: "yaml", + fileName: "c", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, + "yaml with file extension and mismatch type": { + configName: "c", + inConfigType: "yaml", + outConfigType: "json", + fileName: "c.yaml", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.fileName) + v.SetConfigType(tc.inConfigType) + + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + require.NoError(t, err) + v.SetConfigType(tc.outConfigType) + err = v.WriteConfigAs(tc.fileName) + require.NoError(t, err) + read, err := afero.ReadFile(fs, tc.fileName) + require.NoError(t, err) + assert.Equal(t, tc.expectedContent, read) + }) + } +} + +func TestWriteConfigTOML(t *testing.T) { + fs := afero.NewMemMapFs() + + testCases := map[string]struct { + configName string + configType string + fileName string + input []byte + }{ + "with file extension": { + configName: "c", + configType: "toml", + fileName: "c.toml", + input: tomlExample, + }, + "without file extension": { + configName: "c", + configType: "toml", + fileName: "c", + input: tomlExample, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.configName) + v.SetConfigType(tc.configType) + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + require.NoError(t, err) + err = v.WriteConfigAs(tc.fileName) + require.NoError(t, err) + + // The TOML String method does not order the contents. + // Therefore, we must read the generated file and compare the data. + v2 := New() + v2.SetFs(fs) + v2.SetConfigName(tc.configName) + v2.SetConfigType(tc.configType) + v2.SetConfigFile(tc.fileName) + err = v2.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, v.GetString("title"), v2.GetString("title")) + assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio")) + assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob")) + assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization")) + }) + } +} + +func TestWriteConfigDotEnv(t *testing.T) { + fs := afero.NewMemMapFs() + testCases := map[string]struct { + configName string + configType string + fileName string + input []byte + }{ + "with file extension": { + configName: "c", + configType: "env", + fileName: "c.env", + input: dotenvExample, + }, + "without file extension": { + configName: "c", + configType: "env", + fileName: "c", + input: dotenvExample, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.configName) + v.SetConfigType(tc.configType) + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + require.NoError(t, err) + err = v.WriteConfigAs(tc.fileName) + require.NoError(t, err) + + // The TOML String method does not order the contents. + // Therefore, we must read the generated file and compare the data. + v2 := New() + v2.SetFs(fs) + v2.SetConfigName(tc.configName) + v2.SetConfigType(tc.configType) + v2.SetConfigFile(tc.fileName) + err = v2.ReadInConfig() + require.NoError(t, err) + + assert.Equal(t, v.GetString("title_dotenv"), v2.GetString("title_dotenv")) + assert.Equal(t, v.GetString("type_dotenv"), v2.GetString("type_dotenv")) + assert.Equal(t, v.GetString("kind_dotenv"), v2.GetString("kind_dotenv")) + }) + } +} + +func TestSafeWriteConfig(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.AddConfigPath("/test") + v.SetConfigName("c") + v.SetConfigType("yaml") + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample))) + require.NoError(t, v.SafeWriteConfig()) + read, err := afero.ReadFile(fs, testutil.AbsFilePath(t, "/test/c.yaml")) + require.NoError(t, err) + assert.Equal(t, yamlWriteExpected, read) +} + +func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigName("c") + v.SetConfigType("yaml") + require.EqualError(t, v.SafeWriteConfig(), "missing configuration for 'configPath'") +} + +func TestSafeWriteConfigWithExistingFile(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + fs.Create(testutil.AbsFilePath(t, "/test/c.yaml")) + v.SetFs(fs) + v.AddConfigPath("/test") + v.SetConfigName("c") + v.SetConfigType("yaml") + err := v.SafeWriteConfig() + require.Error(t, err) + _, ok := err.(ConfigFileAlreadyExistsError) + assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") +} + +func TestSafeWriteAsConfig(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigType("yaml") + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) + require.NoError(t, err) + require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml")) + _, err = afero.ReadFile(fs, "/test/c.yaml") + require.NoError(t, err) +} + +func TestSafeWriteConfigAsWithExistingFile(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + fs.Create("/test/c.yaml") + v.SetFs(fs) + err := v.SafeWriteConfigAs("/test/c.yaml") + require.Error(t, err) + _, ok := err.(ConfigFileAlreadyExistsError) + assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") +} + +func TestWriteHiddenFile(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + fs.Create(testutil.AbsFilePath(t, "/test/.config")) + v.SetFs(fs) + + v.SetConfigName(".config") + v.SetConfigType("yaml") + v.AddConfigPath("/test") + + err := v.ReadInConfig() + require.NoError(t, err) + + err = v.WriteConfig() + require.NoError(t, err) } var yamlMergeExampleTgt = []byte(` hello: pop: 37890 - lagrenum: 765432101234567 + largenum: 765432101234567 + num2pow63: 9223372036854775808 + universe: null world: - us - uk @@ -823,107 +2002,125 @@ hello: var yamlMergeExampleSrc = []byte(` hello: pop: 45000 - lagrenum: 7654321001234567 + largenum: 7654321001234567 universe: - mw - ad + ints: + - 1 + - 2 fu: bar `) -func TestMergeConfig(t *testing.T) { - v := New() - v.SetConfigType("yml") - if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil { - t.Fatal(err) - } - - if pop := v.GetInt("hello.pop"); pop != 37890 { - t.Fatalf("pop != 37890, = %d", pop) - } - - if pop := v.GetInt("hello.lagrenum"); pop != 765432101234567 { - t.Fatalf("lagrenum != 765432101234567, = %d", pop) - } - - if pop := v.GetInt64("hello.lagrenum"); pop != int64(765432101234567) { - t.Fatalf("int64 lagrenum != 765432101234567, = %d", pop) - } - - if world := v.GetStringSlice("hello.world"); len(world) != 4 { - t.Fatalf("len(world) != 4, = %d", len(world)) - } - - if fu := v.GetString("fu"); fu != "" { - t.Fatalf("fu != \"\", = %s", fu) - } - - if err := v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc)); err != nil { - t.Fatal(err) - } - - if pop := v.GetInt("hello.pop"); pop != 45000 { - t.Fatalf("pop != 45000, = %d", pop) +var jsonMergeExampleTgt = []byte(` +{ + "hello": { + "foo": null, + "pop": 123456 } +} +`) - if pop := v.GetInt("hello.lagrenum"); pop != 7654321001234567 { - t.Fatalf("lagrenum != 7654321001234567, = %d", pop) +var jsonMergeExampleSrc = []byte(` +{ + "hello": { + "foo": "foo str", + "pop": "pop str" } +} +`) - if pop := v.GetInt64("hello.lagrenum"); pop != int64(7654321001234567) { - t.Fatalf("int64 lagrenum != 7654321001234567, = %d", pop) - } +func TestMergeConfig(t *testing.T) { + v := New() + v.SetConfigType("yml") + err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)) + require.NoError(t, err) + + assert.Equal(t, 37890, v.GetInt("hello.pop")) + assert.Equal(t, int32(37890), v.GetInt32("hello.pop")) + assert.Equal(t, int64(765432101234567), v.GetInt64("hello.largenum")) + assert.Equal(t, uint(37890), v.GetUint("hello.pop")) + assert.Equal(t, uint16(37890), v.GetUint16("hello.pop")) + assert.Equal(t, uint32(37890), v.GetUint32("hello.pop")) + assert.Equal(t, uint64(9223372036854775808), v.GetUint64("hello.num2pow63")) + assert.Len(t, v.GetStringSlice("hello.world"), 4) + assert.Empty(t, v.GetString("fu")) + + err = v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc)) + require.NoError(t, err) + + assert.Equal(t, 45000, v.GetInt("hello.pop")) + assert.Equal(t, int32(45000), v.GetInt32("hello.pop")) + assert.Equal(t, int64(7654321001234567), v.GetInt64("hello.largenum")) + assert.Len(t, v.GetStringSlice("hello.world"), 4) + assert.Len(t, v.GetStringSlice("hello.universe"), 2) + assert.Len(t, v.GetIntSlice("hello.ints"), 2) + assert.Equal(t, "bar", v.GetString("fu")) +} - if world := v.GetStringSlice("hello.world"); len(world) != 4 { - t.Fatalf("len(world) != 4, = %d", len(world)) - } +func TestMergeConfigOverrideType(t *testing.T) { + v := New() + v.SetConfigType("json") + err := v.ReadConfig(bytes.NewBuffer(jsonMergeExampleTgt)) + require.NoError(t, err) - if universe := v.GetStringSlice("hello.universe"); len(universe) != 2 { - t.Fatalf("len(universe) != 2, = %d", len(universe)) - } + err = v.MergeConfig(bytes.NewBuffer(jsonMergeExampleSrc)) + require.NoError(t, err) - if fu := v.GetString("fu"); fu != "bar" { - t.Fatalf("fu != \"bar\", = %s", fu) - } + assert.Equal(t, "pop str", v.GetString("hello.pop")) + assert.Equal(t, "foo str", v.GetString("hello.foo")) } func TestMergeConfigNoMerge(t *testing.T) { v := New() v.SetConfigType("yml") - if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil { - t.Fatal(err) - } + err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)) + require.NoError(t, err) - if pop := v.GetInt("hello.pop"); pop != 37890 { - t.Fatalf("pop != 37890, = %d", pop) - } + assert.Equal(t, 37890, v.GetInt("hello.pop")) + assert.Len(t, v.GetStringSlice("hello.world"), 4) + assert.Empty(t, v.GetString("fu")) - if world := v.GetStringSlice("hello.world"); len(world) != 4 { - t.Fatalf("len(world) != 4, = %d", len(world)) - } + err = v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc)) + require.NoError(t, err) - if fu := v.GetString("fu"); fu != "" { - t.Fatalf("fu != \"\", = %s", fu) - } + assert.Equal(t, 45000, v.GetInt("hello.pop")) + assert.Empty(t, v.GetStringSlice("hello.world")) + assert.Len(t, v.GetStringSlice("hello.universe"), 2) + assert.Len(t, v.GetIntSlice("hello.ints"), 2) + assert.Equal(t, "bar", v.GetString("fu")) +} - if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc)); err != nil { - t.Fatal(err) +func TestMergeConfigMap(t *testing.T) { + v := New() + v.SetConfigType("yml") + err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)) + require.NoError(t, err) + + assertFn := func(i int) { + large := v.GetInt64("hello.largenum") + pop := v.GetInt("hello.pop") + assert.Equal(t, int64(765432101234567), large) + assert.Equal(t, i, pop) } - if pop := v.GetInt("hello.pop"); pop != 45000 { - t.Fatalf("pop != 45000, = %d", pop) - } + assertFn(37890) - if world := v.GetStringSlice("hello.world"); len(world) != 0 { - t.Fatalf("len(world) != 0, = %d", len(world)) + update := map[string]any{ + "Hello": map[string]any{ + "Pop": 1234, + }, + "World": map[any]any{ + "Rock": 345, + }, } - if universe := v.GetStringSlice("hello.universe"); len(universe) != 2 { - t.Fatalf("len(universe) != 2, = %d", len(universe)) - } + err = v.MergeConfigMap(update) + require.NoError(t, err) - if fu := v.GetString("fu"); fu != "bar" { - t.Fatalf("fu != \"bar\", = %s", fu) - } + assert.Equal(t, 345, v.GetInt("world.rock")) + + assertFn(1234) } func TestUnmarshalingWithAliases(t *testing.T) { @@ -944,25 +2141,22 @@ func TestUnmarshalingWithAliases(t *testing.T) { var C config err := v.Unmarshal(&C) - if err != nil { - t.Fatalf("unable to decode into struct, %v", err) - } + require.NoError(t, err, "unable to decode into struct") assert.Equal(t, &config{ID: 1, FirstName: "Steve", Surname: "Owen"}, &C) } func TestSetConfigNameClearsFileCache(t *testing.T) { - SetConfigFile("/tmp/config.yaml") - SetConfigName("default") + v := New() + v.SetConfigFile("/tmp/config.yaml") + v.SetConfigName("default") f, err := v.getConfigFile() - if err == nil { - t.Fatalf("config file cache should have been cleared") - } + require.Error(t, err, "config file cache should have been cleared") assert.Empty(t, f) } func TestShadowedNestedValue(t *testing.T) { - + v := New() config := `name: steve clothing: jacket: leather @@ -970,31 +2164,37 @@ clothing: pants: size: large ` - initConfig("yaml", config) + initConfig("yaml", config, v) - assert.Equal(t, "steve", GetString("name")) + assert.Equal(t, "steve", v.GetString("name")) polyester := "polyester" - SetDefault("clothing.shirt", polyester) - SetDefault("clothing.jacket.price", 100) + v.SetDefault("clothing.shirt", polyester) + v.SetDefault("clothing.jacket.price", 100) - assert.Equal(t, "leather", GetString("clothing.jacket")) - assert.Nil(t, Get("clothing.jacket.price")) - assert.Equal(t, polyester, GetString("clothing.shirt")) + assert.Equal(t, "leather", v.GetString("clothing.jacket")) + assert.Nil(t, v.Get("clothing.jacket.price")) + assert.Equal(t, polyester, v.GetString("clothing.shirt")) - clothingSettings := AllSettings()["clothing"].(map[string]interface{}) + clothingSettings := v.AllSettings()["clothing"].(map[string]any) assert.Equal(t, "leather", clothingSettings["jacket"]) assert.Equal(t, polyester, clothingSettings["shirt"]) } func TestDotParameter(t *testing.T) { - initJSON() - // shoud take precedence over batters defined in jsonExample + v := New() + + v.SetConfigType("json") + + // Read the YAML data into Viper configuration + require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading YAML data") + + // should take precedence over batters defined in jsonExample r := bytes.NewReader([]byte(`{ "batters.batter": [ { "type": "Small" } ] }`)) - unmarshalReader(r, v.config) + v.unmarshalReader(r, v.config) - actual := Get("batters.batter") - expected := []interface{}{map[string]interface{}{"type": "Small"}} + actual := v.Get("batters.batter") + expected := []any{map[string]any{"type": "Small"}} assert.Equal(t, expected, actual) } @@ -1044,76 +2244,361 @@ R = 6 } func TestCaseInsensitiveSet(t *testing.T) { - Reset() - m1 := map[string]interface{}{ + v := New() + m1 := map[string]any{ "Foo": 32, - "Bar": map[interface{}]interface { - }{ + "Bar": map[any]any{ "ABc": "A", - "cDE": "B"}, + "cDE": "B", + }, } - m2 := map[string]interface{}{ + m2 := map[string]any{ "Foo": 52, - "Bar": map[interface{}]interface { - }{ + "Bar": map[any]any{ "bCd": "A", - "eFG": "B"}, + "eFG": "B", + }, } - Set("Given1", m1) - Set("Number1", 42) + v.Set("Given1", m1) + v.Set("Number1", 42) - SetDefault("Given2", m2) - SetDefault("Number2", 52) + v.SetDefault("Given2", m2) + v.SetDefault("Number2", 52) // Verify SetDefault - if v := Get("number2"); v != 52 { - t.Fatalf("Expected 52 got %q", v) + assert.Equal(t, 52, v.Get("number2")) + assert.Equal(t, 52, v.Get("given2.foo")) + assert.Equal(t, "A", v.Get("given2.bar.bcd")) + _, ok := m2["Foo"] + assert.True(t, ok) + + // Verify Set + assert.Equal(t, 42, v.Get("number1")) + assert.Equal(t, 32, v.Get("given1.foo")) + assert.Equal(t, "A", v.Get("given1.bar.abc")) + _, ok = m1["Foo"] + assert.True(t, ok) +} + +func TestParseNested(t *testing.T) { + v := New() + type duration struct { + Delay time.Duration } - if v := Get("given2.foo"); v != 52 { - t.Fatalf("Expected 52 got %q", v) + type item struct { + Name string + Delay time.Duration + Nested duration } - if v := Get("given2.bar.bcd"); v != "A" { - t.Fatalf("Expected A got %q", v) + config := `[[parent]] + delay="100ms" + [parent.nested] + delay="200ms" +` + initConfig("toml", config, v) + + var items []item + err := v.UnmarshalKey("parent", &items) + require.NoError(t, err, "unable to decode into struct") + + assert.Len(t, items, 1) + assert.Equal(t, 100*time.Millisecond, items[0].Delay) + assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay) +} + +func doTestCaseInsensitive(t *testing.T, typ, config string) { + v := New() + initConfig(typ, config, v) + v.Set("RfD", true) + assert.Equal(t, true, v.Get("rfd")) + assert.Equal(t, true, v.Get("rFD")) + assert.Equal(t, 1, cast.ToInt(v.Get("abcd"))) + assert.Equal(t, 1, cast.ToInt(v.Get("Abcd"))) + assert.Equal(t, 2, cast.ToInt(v.Get("ef.gh"))) + assert.Equal(t, 3, cast.ToInt(v.Get("ef.ijk"))) + assert.Equal(t, 4, cast.ToInt(v.Get("ef.lm.no"))) + assert.Equal(t, 5, cast.ToInt(v.Get("ef.lm.p.q"))) +} + +func newViperWithConfigFile(t *testing.T) (*Viper, string) { + watchDir := t.TempDir() + configFile := path.Join(watchDir, "config.yaml") + err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640) + require.NoError(t, err) + v := New() + v.SetConfigFile(configFile) + err = v.ReadInConfig() + require.NoError(t, err) + require.Equal(t, "bar", v.Get("foo")) + return v, configFile +} + +func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) { + watchDir := t.TempDir() + dataDir1 := path.Join(watchDir, "data1") + err := os.Mkdir(dataDir1, 0o777) + require.NoError(t, err) + realConfigFile := path.Join(dataDir1, "config.yaml") + t.Logf("Real config file location: %s\n", realConfigFile) + err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640) + require.NoError(t, err) + // now, symlink the tm `data1` dir to `data` in the baseDir + os.Symlink(dataDir1, path.Join(watchDir, "data")) + // and link the `/datadir1/config.yaml` to `/config.yaml` + configFile := path.Join(watchDir, "config.yaml") + os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile) + t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml")) + // init Viper + v := New() + v.SetConfigFile(configFile) + err = v.ReadInConfig() + require.NoError(t, err) + require.Equal(t, "bar", v.Get("foo")) + return v, watchDir, configFile +} + +func TestWatchFile(t *testing.T) { + if runtime.GOOS == "linux" { + // TODO(bep) FIX ME + t.Skip("Skip test on Linux ...") } - if _, ok := m2["Foo"]; !ok { - t.Fatal("Input map changed") + t.Run("file content changed", func(t *testing.T) { + // given a `config.yaml` file being watched + v, configFile := newViperWithConfigFile(t) + _, err := os.Stat(configFile) + require.NoError(t, err) + t.Logf("test config file: %s\n", configFile) + wg := sync.WaitGroup{} + wg.Add(1) + var wgDoneOnce sync.Once // OnConfigChange is called twice on Windows + v.OnConfigChange(func(_ fsnotify.Event) { + t.Logf("config file changed") + wgDoneOnce.Do(func() { + wg.Done() + }) + }) + v.WatchConfig() + // when overwriting the file and waiting for the custom change notification handler to be triggered + err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640) + wg.Wait() + // then the config value should have changed + require.NoError(t, err) + assert.Equal(t, "baz", v.Get("foo")) + }) + + t.Run("link to real file changed (à la Kubernetes)", func(t *testing.T) { + // skip if not executed on Linux + if runtime.GOOS != "linux" { + t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...") + } + v, watchDir, _ := newViperWithSymlinkedConfigFile(t) + wg := sync.WaitGroup{} + v.WatchConfig() + v.OnConfigChange(func(_ fsnotify.Event) { + t.Logf("config file changed") + wg.Done() + }) + wg.Add(1) + // when link to another `config.yaml` file + dataDir2 := path.Join(watchDir, "data2") + err := os.Mkdir(dataDir2, 0o777) + require.NoError(t, err) + configFile2 := path.Join(dataDir2, "config.yaml") + err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640) + require.NoError(t, err) + // change the symlink using the `ln -sfn` command + err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run() + require.NoError(t, err) + wg.Wait() + // then + require.NoError(t, err) + assert.Equal(t, "baz", v.Get("foo")) + }) +} + +func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { + flags := pflag.NewFlagSet("test", pflag.ContinueOnError) + flags.String("foo.bar", "cobra_flag", "") + + v := New() + assert.NoError(t, v.BindPFlags(flags)) + + config := &struct { + Foo struct { + Bar string + } + }{} + + assert.NoError(t, v.Unmarshal(config)) + assert.Equal(t, "cobra_flag", config.Foo.Bar) +} + +// var yamlExampleWithDot = []byte(`Hacker: true +// name: steve +// hobbies: +// - skateboarding +// - snowboarding +// - go +// clothing: +// jacket: leather +// trousers: denim +// pants: +// size: large +// age: 35 +// eyes : brown +// beard: true +// emails: +// steve@hacker.com: +// created: 01/02/03 +// active: true +// `) + +func TestKeyDelimiter(t *testing.T) { + v := NewWithOptions(KeyDelimiter("::")) + v.SetConfigType("yaml") + r := strings.NewReader(string(yamlExampleWithDot)) + + err := v.unmarshalReader(r, v.config) + require.NoError(t, err) + + values := map[string]any{ + "image": map[string]any{ + "repository": "someImage", + "tag": "1.0.0", + }, + "ingress": map[string]any{ + "annotations": map[string]any{ + "traefik.frontend.rule.type": "PathPrefix", + "traefik.ingress.kubernetes.io/ssl-redirect": "true", + }, + }, } - // Verify Set - if v := Get("number1"); v != 42 { - t.Fatalf("Expected 42 got %q", v) + v.SetDefault("charts::values", values) + + assert.Equal(t, "leather", v.GetString("clothing::jacket")) + assert.Equal(t, "01/02/03", v.GetString("emails::steve@hacker.com::created")) + + type config struct { + Charts struct { + Values map[string]any + } } - if v := Get("given1.foo"); v != 32 { - t.Fatalf("Expected 32 got %q", v) + expected := config{ + Charts: struct { + Values map[string]any + }{ + Values: values, + }, } - if v := Get("given1.bar.abc"); v != "A" { - t.Fatalf("Expected A got %q", v) + var actual config + + assert.NoError(t, v.Unmarshal(&actual)) + + assert.Equal(t, expected, actual) +} + +var yamlDeepNestedSlices = []byte(`TV: +- title: "The Expanse" + title_i18n: + USA: "The Expanse" + Japan: "エクスパンス -巨獣めざめる-" + seasons: + - first_released: "December 14, 2015" + episodes: + - title: "Dulcinea" + air_date: "December 14, 2015" + - title: "The Big Empty" + air_date: "December 15, 2015" + - title: "Remember the Cant" + air_date: "December 22, 2015" + - first_released: "February 1, 2017" + episodes: + - title: "Safe" + air_date: "February 1, 2017" + - title: "Doors & Corners" + air_date: "February 1, 2017" + - title: "Static" + air_date: "February 8, 2017" + episodes: + - ["Dulcinea", "The Big Empty", "Remember the Cant"] + - ["Safe", "Doors & Corners", "Static"] +`) + +func TestSliceIndexAccess(t *testing.T) { + v := New() + v.SetConfigType("yaml") + r := strings.NewReader(string(yamlDeepNestedSlices)) + + err := v.unmarshalReader(r, v.config) + require.NoError(t, err) + + assert.Equal(t, "The Expanse", v.GetString("tv.0.title")) + assert.Equal(t, "February 1, 2017", v.GetString("tv.0.seasons.1.first_released")) + assert.Equal(t, "Static", v.GetString("tv.0.seasons.1.episodes.2.title")) + assert.Equal(t, "December 15, 2015", v.GetString("tv.0.seasons.0.episodes.1.air_date")) + + // Test nested keys with capital letters + assert.Equal(t, "The Expanse", v.GetString("tv.0.title_i18n.USA")) + assert.Equal(t, "エクスパンス -巨獣めざめる-", v.GetString("tv.0.title_i18n.Japan")) + + // Test for index out of bounds + assert.Equal(t, "", v.GetString("tv.0.seasons.2.first_released")) + + // Accessing multidimensional arrays + assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2")) +} + +func TestIsPathShadowedInFlatMap(t *testing.T) { + v := New() + + stringMap := map[string]string{ + "foo": "value", } - if _, ok := m1["Foo"]; !ok { - t.Fatal("Input map changed") + flagMap := map[string]FlagValue{ + "foo": pflagValue{}, } + + path1 := []string{"foo", "bar"} + expected1 := "foo" + + // "foo.bar" should shadowed by "foo" + assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, stringMap)) + assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, flagMap)) + + path2 := []string{"bar", "foo"} + expected2 := "" + + // "bar.foo" should not shadowed by "foo" + assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, stringMap)) + assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, flagMap)) } -func doTestCaseInsensitive(t *testing.T, typ, config string) { - initConfig(typ, config) - Set("RfD", true) - assert.Equal(t, true, Get("rfd")) - assert.Equal(t, true, Get("rFD")) - assert.Equal(t, 1, cast.ToInt(Get("abcd"))) - assert.Equal(t, 1, cast.ToInt(Get("Abcd"))) - assert.Equal(t, 2, cast.ToInt(Get("ef.gh"))) - assert.Equal(t, 3, cast.ToInt(Get("ef.ijk"))) - assert.Equal(t, 4, cast.ToInt(Get("ef.lm.no"))) - assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q"))) +func TestFlagShadow(t *testing.T) { + v := New() + v.SetDefault("foo.bar1.bar2", "default") + + flags := pflag.NewFlagSet("test", pflag.ContinueOnError) + flags.String("foo.bar1", "shadowed", "") + flags.VisitAll(func(flag *pflag.Flag) { + flag.Changed = true + }) + + v.BindPFlags(flags) + + assert.Equal(t, "shadowed", v.GetString("foo.bar1")) + // the default "foo.bar1.bar2" value should shadowed by flag "foo.bar1" value + // and should return an empty string + assert.Equal(t, "", v.GetString("foo.bar1.bar2")) } func BenchmarkGetBool(b *testing.B) { @@ -1140,7 +2625,7 @@ func BenchmarkGet(b *testing.B) { } } -// This is the "perfect result" for the above. +// BenchmarkGetBoolFromMap is the "perfect result" for the above. func BenchmarkGetBoolFromMap(b *testing.B) { m := make(map[string]bool) key := "BenchmarkGetBool" @@ -1152,3 +2637,10 @@ func BenchmarkGetBoolFromMap(b *testing.B) { } } } + +// Skip some tests on Windows that kept failing when Windows was added to the CI as a target. +func skipWindows(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Skip test on Windows") + } +} diff --git a/viper_yaml_test.go b/viper_yaml_test.go new file mode 100644 index 000000000..264446b9d --- /dev/null +++ b/viper_yaml_test.go @@ -0,0 +1,53 @@ +package viper + +var yamlExample = []byte(`Hacker: true +name: steve +hobbies: + - skateboarding + - snowboarding + - go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +`) + +var yamlWriteExpected = []byte(`age: 35 +beard: true +clothing: + jacket: leather + pants: + size: large + trousers: denim +eyes: brown +hacker: true +hobbies: + - skateboarding + - snowboarding + - go +name: steve +`) + +var yamlExampleWithDot = []byte(`Hacker: true +name: steve +hobbies: + - skateboarding + - snowboarding + - go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +emails: + steve@hacker.com: + created: 01/02/03 + active: true +`)