diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md index d294fc32..c711a594 100644 --- a/.github/CODE_OF_CONDUCT.md +++ b/.github/CODE_OF_CONDUCT.md @@ -1,4 +1,3 @@ - ## Our Pledge We as members, contributors, and leaders pledge to make participation in our @@ -105,7 +104,7 @@ Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an +standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 98288c87..7ac4512d 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,6 +1,7 @@ # Contributing -By participating to this project, you agree to abide [code of conduct](https://github.com/obalunenko/advent-of-code/blob/master/.github/CODE_OF_CONDUCT.md). +By participating to this project, you agree to +abide [code of conduct](https://github.com/obalunenko/advent-of-code/blob/master/.github/CODE_OF_CONDUCT.md). Please note that I'll not accept any PR with new puzzle solutions - I don't like spoilers. diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 082f371d..7b07f63e 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,7 +13,7 @@ updates: commit-message: prefix: "chore" include: "scope" - + - package-ecosystem: "github-actions" directory: "/" schedule: diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7831dc06..cf5ada87 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -41,7 +41,7 @@ jobs: max-parallel: 3 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Build steps: @@ -93,7 +93,7 @@ jobs: max-parallel: 2 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Run Tests steps: @@ -140,7 +140,7 @@ jobs: max-parallel: 2 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Run linters steps: @@ -186,13 +186,13 @@ jobs: shell: bash reports: - needs: [testing, linting, build] + needs: [ testing, linting, build ] strategy: fail-fast: true max-parallel: 1 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Quality reports steps: @@ -227,14 +227,9 @@ jobs: make install-tools shell: bash - - name: Prepare test coverage + - name: Prepare test coverage and reports run: | - make test-cover - shell: bash - - - name: Tests report - run: | - make test-sonar-report + make prepare-cover-report shell: bash - name: Prepare lint report diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0c6a973f..297d7a31 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: max-parallel: 2 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Build steps: @@ -60,7 +60,7 @@ jobs: max-parallel: 2 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Regression tests steps: @@ -109,7 +109,7 @@ jobs: max-parallel: 1 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Lint steps: @@ -156,13 +156,13 @@ jobs: shell: bash release: - needs: [build, regression_test, linting] + needs: [ build, regression_test, linting ] strategy: fail-fast: false max-parallel: 1 matrix: os: [ 'ubuntu-22.04' ] - go: [ '1.19' ] + go: [ '1.20' ] runs-on: ${{ matrix.os }} name: Release steps: diff --git a/.golangci.pipe.yml b/.golangci.pipe.yml index cfa17a1e..b368f910 100644 --- a/.golangci.pipe.yml +++ b/.golangci.pipe.yml @@ -70,29 +70,29 @@ issues: fix: false severity: - # Default value is empty string. - # Set the default severity for issues. If severity rules are defined and the issues - # do not match or no severity is provided to the rule this will be the default - # severity applied. Severities should match the supported severity names of the - # selected out format. - # - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity - # - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity - # - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message - default-severity: error - - # The default value is false. - # If set to true severity-rules regular expressions become case sensitive. - case-sensitive: false - - # Default value is empty list. - # When a list of severity rules are provided, severity information will be added to lint - # issues. Severity rules have the same filtering capability as exclude rules except you - # are allowed to specify one matcher per severity rule. - # Only affects out formats that support setting severity information. - rules: - - linters: - - dupl - severity: warning + # Default value is empty string. + # Set the default severity for issues. If severity rules are defined and the issues + # do not match or no severity is provided to the rule this will be the default + # severity applied. Severities should match the supported severity names of the + # selected out format. + # - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity + # - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity + # - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message + default-severity: error + + # The default value is false. + # If set to true severity-rules regular expressions become case sensitive. + case-sensitive: false + + # Default value is empty list. + # When a list of severity rules are provided, severity information will be added to lint + # issues. Severity rules have the same filtering capability as exclude rules except you + # are allowed to specify one matcher per severity rule. + # Only affects out formats that support setting severity information. + rules: + - linters: + - dupl + severity: warning run: issues-exit-code: 1 diff --git a/.golangci.yml b/.golangci.yml index 7f76f2fb..3cbd333d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -153,29 +153,29 @@ issues: fix: false severity: - # Default value is empty string. - # Set the default severity for issues. If severity rules are defined and the issues - # do not match or no severity is provided to the rule this will be the default - # severity applied. Severities should match the supported severity names of the - # selected out format. - # - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity - # - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity - # - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message - default-severity: error + # Default value is empty string. + # Set the default severity for issues. If severity rules are defined and the issues + # do not match or no severity is provided to the rule this will be the default + # severity applied. Severities should match the supported severity names of the + # selected out format. + # - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity + # - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity + # - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message + default-severity: error - # The default value is false. - # If set to true severity-rules regular expressions become case sensitive. - case-sensitive: false + # The default value is false. + # If set to true severity-rules regular expressions become case sensitive. + case-sensitive: false - # Default value is empty list. - # When a list of severity rules are provided, severity information will be added to lint - # issues. Severity rules have the same filtering capability as exclude rules except you - # are allowed to specify one matcher per severity rule. - # Only affects out formats that support setting severity information. - rules: - - linters: - - dupl - severity: warning + # Default value is empty list. + # When a list of severity rules are provided, severity information will be added to lint + # issues. Severity rules have the same filtering capability as exclude rules except you + # are allowed to specify one matcher per severity rule. + # Only affects out formats that support setting severity information. + rules: + - linters: + - dupl + severity: warning run: issues-exit-code: 0 diff --git a/.goreleaser.yml b/.goreleaser.yml index d0c2a7ad..83fa44c5 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -7,7 +7,7 @@ release: gomod: proxy: true - # If proxy is true, use these environment variables when running `go mod` commands (namely, `go mod tidy`). + # If proxy is true, use these environment variables when running `go mod` commands (namely, `go mod tidy`). # Defaults to `os.Environ()`. env: - GOPROXY=https://proxy.golang.org,direct @@ -44,8 +44,7 @@ builds: - "{{ .Env.GO_BUILD_LDFLAGS }}" - universal_binaries: - - - # ID of the source build + - # ID of the source build # # Defaults to the project name. id: cli diff --git a/Makefile b/Makefile index 5c26b02a..e44a0f3e 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VERSION ?= $(shell git describe --tags $(git rev-list --tags --max-count=1)) APP_NAME?=aoc-cli SHELL := env APP_NAME=$(APP_NAME) $(SHELL) -GOTOOLS_IMAGE_TAG?=v0.0.2 +GOTOOLS_IMAGE_TAG?=v0.2.1 SHELL := env GOTOOLS_IMAGE_TAG=$(GOTOOLS_IMAGE_TAG) $(SHELL) COMPOSE_TOOLS_FILE=deployments/docker-compose/go-tools-docker-compose.yml @@ -46,18 +46,17 @@ test-cover: $(COMPOSE_TOOLS_CMD_UP) run-tests-coverage run-tests-coverage .PHONY: test-cover -## Tests sonar report generate. -test-sonar-report: - ./scripts/tests/sonar-report.sh -.PHONY: test-sonar-report +prepare-cover-report: test-cover + $(COMPOSE_TOOLS_CMD_UP) prepare-cover-report prepare-cover-report +.PHONY: prepare-cover-report ## Open coverage report. -open-cover-report: test-cover +open-cover-report: prepare-cover-report ./scripts/open-coverage-report.sh .PHONY: open-cover-report ## Update readme coverage. -update-readme-cover: build test-cover +update-readme-cover: build prepare-cover-report $(COMPOSE_TOOLS_CMD_UP) update-readme-coverage update-readme-coverage .PHONY: update-readme-cover diff --git a/deployments/docker-compose/go-tools-docker-compose.yml b/deployments/docker-compose/go-tools-docker-compose.yml index f51ae97a..fb8f8348 100755 --- a/deployments/docker-compose/go-tools-docker-compose.yml +++ b/deployments/docker-compose/go-tools-docker-compose.yml @@ -6,6 +6,7 @@ services: working_dir: /app/ volumes: - ../../:/app/ + command: /bin/sh -c '' fix-imports: extends: @@ -34,6 +35,11 @@ services: service: tools entrypoint: /bin/sh -c './scripts/tests/coverage.sh' + prepare-cover-report: + extends: + service: tools + entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/tests/prepare-cover-report.sh' + update-readme-coverage: extends: service: tools @@ -47,12 +53,12 @@ services: lint-pipeline: extends: service: tools - entrypoint: /bin/sh -c './scripts/linting/golangci-pipeline.sh' + entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-pipeline.sh' lint-sonar: extends: service: tools - entrypoint: /bin/sh -c './scripts/linting/golangci-sonar.sh' + entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-sonar.sh' go-generate: extends: diff --git a/go.mod b/go.mod index c4ce51df..2730a786 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module github.com/obalunenko/advent-of-code -go 1.19 +go 1.20 require ( github.com/briandowns/spinner v1.20.0 github.com/manifoldco/promptui v0.9.0 - github.com/obalunenko/getenv v1.2.1 + github.com/obalunenko/getenv v1.3.0 github.com/obalunenko/logger v0.5.1 github.com/obalunenko/version v1.1.0 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index 5a473e1b..7e48b6a1 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/obalunenko/getenv v1.2.1 h1:K7kcPZ+E4CxfJDI4JyiQyIzxEFPqYwmfxAZqmzPOIUc= -github.com/obalunenko/getenv v1.2.1/go.mod h1:rQSU30CHN52CamPdfTjUF4dk0oaoTo03kjxw2Yx3s2Y= +github.com/obalunenko/getenv v1.3.0 h1:gBH8OYI2fw+Rfyd8aywMnqRLRNxhI4bLztAZLuOjMVU= +github.com/obalunenko/getenv v1.3.0/go.mod h1:trfsd8x+Ulqtw8DHKTmL1DdM9wgUd8IvOUgo22GLn3U= github.com/obalunenko/logger v0.5.1 h1:49DIEwy4sBvRzM/OtD/m7zITi5L3S5DtSYZ/WoUrnno= github.com/obalunenko/logger v0.5.1/go.mod h1:/GnmNcmEV7tAL9StHrs3nDkXAK+TEvChoNY+YYRZww4= github.com/obalunenko/version v1.1.0 h1:yVua7OHnK3+MJpendeMmAlfzVmq7R1h8MO3Ufz7HEec= diff --git a/scripts/build/app.sh b/scripts/build/app.sh index 5e7fdafa..8f91f87d 100755 --- a/scripts/build/app.sh +++ b/scripts/build/app.sh @@ -39,6 +39,6 @@ GO_BUILD_PACKAGE="${REPO_ROOT}/cmd/${APP}" rm -rf "${BIN_OUT}" -go build -o "${BIN_OUT}" -a -ldflags "${GO_BUILD_LDFLAGS}" "${GO_BUILD_PACKAGE}" +go build -trimpath -o "${BIN_OUT}" -a -ldflags "${GO_BUILD_LDFLAGS}" "${GO_BUILD_PACKAGE}" echo "Binary compiled at ${BIN_OUT}" diff --git a/scripts/linting/golangci-sonar.sh b/scripts/linting/golangci-sonar.sh index 23e9d395..e756a3ac 100755 --- a/scripts/linting/golangci-sonar.sh +++ b/scripts/linting/golangci-sonar.sh @@ -15,6 +15,6 @@ checkInstalled golangci-lint echo "Linting..." -golangci-lint run --config .golangci.yml > linters.out +golangci-lint run --config .golangci.yml >linters.out echo "${SCRIPT_NAME} done." diff --git a/scripts/tests/coverage.sh b/scripts/tests/coverage.sh index 08be37d4..2d4bfefb 100755 --- a/scripts/tests/coverage.sh +++ b/scripts/tests/coverage.sh @@ -12,22 +12,11 @@ source "${SCRIPTS_DIR}/helpers-source.sh" echo "${SCRIPT_NAME} is running... " -checkInstalled gocov - export GO111MODULE=on rm -rf "${COVER_DIR}" mkdir -p "${COVER_DIR}" -go test --count=1 -coverprofile "${COVER_DIR}/unit.cov" -covermode=atomic ./... - -{ - echo "mode: atomic" - tail -q -n +2 "${COVER_DIR}"/*.cov -} >>"${COVER_DIR}/full.cov" - -gocov convert "${COVER_DIR}/full.cov" >"${COVER_DIR}/full.json" - -checkInstalled 'gocov-html' +go test --count=1 -coverprofile "${COVER_DIR}/unit.cov" -covermode=atomic -json ./... >tests-report.json -gocov-html "${COVER_DIR}/full.json" >"${COVER_DIR}/full.html" +echo "${SCRIPT_NAME} is done... " diff --git a/scripts/tests/prepare-cover-report.sh b/scripts/tests/prepare-cover-report.sh new file mode 100755 index 00000000..64a9b930 --- /dev/null +++ b/scripts/tests/prepare-cover-report.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -Eeuo pipefail + +SCRIPT_NAME="$(basename "$0")" +SCRIPT_DIR="$(dirname "$0")" +REPO_ROOT="$(cd "${SCRIPT_DIR}" && git rev-parse --show-toplevel)" +SCRIPTS_DIR="${REPO_ROOT}/scripts" +COVER_DIR=${REPO_ROOT}/coverage + +source "${SCRIPTS_DIR}/helpers-source.sh" + +echo "${SCRIPT_NAME} is running... " + +checkInstalled gocov + +{ + echo "mode: atomic" + tail -q -n +2 "${COVER_DIR}"/*.cov +} >>"${COVER_DIR}/full.cov" + +gocov convert "${COVER_DIR}/full.cov" >"${COVER_DIR}/full.json" + +checkInstalled 'gocov-html' + +gocov-html "${COVER_DIR}/full.json" >"${COVER_DIR}/full.html" + +echo "${SCRIPT_NAME} is done... " diff --git a/scripts/tests/sonar-report.sh b/scripts/tests/sonar-report.sh deleted file mode 100755 index c8408106..00000000 --- a/scripts/tests/sonar-report.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -set -eu pipefail - -SCRIPT_NAME="$(basename "$0")" - -echo "${SCRIPT_NAME} is running... " - -go test -json ./... > tests-report.json - -echo "${SCRIPT_NAME} done." diff --git a/tests/regression_test.go b/tests/regression_test.go index 8070b985..db3068e5 100644 --- a/tests/regression_test.go +++ b/tests/regression_test.go @@ -32,11 +32,11 @@ const ( // Regression tests for all puzzles. Check that answers still correct. func TestRun(t *testing.T) { - if !getenv.BoolOrDefault(regressionEnabled, false) { + if !getenv.EnvOrDefault(regressionEnabled, false) { t.Skipf("%s disabled", regressionEnabled) } - session := getenv.StringOrDefault(puzzles.AOCSession, "") + session := getenv.EnvOrDefault(puzzles.AOCSession, "") if session == "" { t.Fatalf("%s not set", puzzles.AOCSession) } diff --git a/vendor/github.com/obalunenko/getenv/Makefile b/vendor/github.com/obalunenko/getenv/Makefile index 93274f99..87edff89 100644 --- a/vendor/github.com/obalunenko/getenv/Makefile +++ b/vendor/github.com/obalunenko/getenv/Makefile @@ -6,7 +6,7 @@ VERSION ?= $(shell git describe --tags $(git rev-list --tags --max-count=1)) APP_NAME?=getenv SHELL := env APP_NAME=$(APP_NAME) $(SHELL) -GOTOOLS_IMAGE_TAG?=v0.1.4 +GOTOOLS_IMAGE_TAG?=v0.2.1 SHELL := env GOTOOLS_IMAGE_TAG=$(GOTOOLS_IMAGE_TAG) $(SHELL) COMPOSE_TOOLS_FILE=deployments/docker-compose/go-tools-docker-compose.yml diff --git a/vendor/github.com/obalunenko/getenv/README.md b/vendor/github.com/obalunenko/getenv/README.md index b626742e..708db0c9 100644 --- a/vendor/github.com/obalunenko/getenv/README.md +++ b/vendor/github.com/obalunenko/getenv/README.md @@ -18,6 +18,8 @@ Package getenv provides functionality for loading environment variables and pars - []int - int64 - []int64 +- uint64 +- []uint64 - float64 - []float64 - time.Time diff --git a/vendor/github.com/obalunenko/getenv/getenv.go b/vendor/github.com/obalunenko/getenv/getenv.go index 0aeeed68..bff872aa 100644 --- a/vendor/github.com/obalunenko/getenv/getenv.go +++ b/vendor/github.com/obalunenko/getenv/getenv.go @@ -2,8 +2,6 @@ package getenv import ( - "time" - "github.com/obalunenko/getenv/internal" "github.com/obalunenko/getenv/option" ) @@ -30,102 +28,3 @@ func newParseParams(opts []option.Option) internal.Parameters { return p } - -// IntOrDefault retrieves the int value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func IntOrDefault(key string, defaultVal int) int { - return EnvOrDefault(key, defaultVal) -} - -// StringOrDefault retrieves the string value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func StringOrDefault(key, defaultVal string) string { - return EnvOrDefault(key, defaultVal) -} - -// BoolOrDefault retrieves the bool value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func BoolOrDefault(key string, defaultVal bool) bool { - return EnvOrDefault(key, defaultVal) -} - -// StringSliceOrDefault retrieves the string slice value of the environment variable named -// by the key and separated by sep. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func StringSliceOrDefault(key string, defaultVal []string, sep string) []string { - return EnvOrDefault(key, defaultVal, option.WithSeparator(sep)) -} - -// IntSliceOrDefault retrieves the int slice value of the environment variable named -// by the key and separated by sep. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func IntSliceOrDefault(key string, defaultVal []int, sep string) []int { - return EnvOrDefault(key, defaultVal, option.WithSeparator(sep)) -} - -// Float64SliceOrDefault retrieves the float64 slice value of the environment variable named -// by the key and separated by sep. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func Float64SliceOrDefault(key string, defaultVal []float64, sep string) []float64 { - return EnvOrDefault(key, defaultVal, option.WithSeparator(sep)) -} - -// DurationOrDefault retrieves the time.Duration value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func DurationOrDefault(key string, defaultVal time.Duration) time.Duration { - return EnvOrDefault(key, defaultVal) -} - -// TimeOrDefault retrieves the time.Time value of the environment variable named -// by the key represented by layout. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func TimeOrDefault(key string, defaultVal time.Time, layout string) time.Time { - return EnvOrDefault(key, defaultVal, option.WithTimeLayout(layout)) -} - -// Int64OrDefault retrieves the int64 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func Int64OrDefault(key string, defaultVal int64) int64 { - return EnvOrDefault(key, defaultVal) -} - -// Float64OrDefault retrieves the float64 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func Float64OrDefault(key string, defaultVal float64) float64 { - return EnvOrDefault(key, defaultVal) -} - -// Int64SliceOrDefault retrieves the int6464 slice value of the environment variable named -// by the key and separated by sep. -// If variable not set or value is empty - defaultVal will be returned. -// -// Deprecated: use EnvOrDefault. -func Int64SliceOrDefault(key string, defaultVal []int64, sep string) []int64 { - return EnvOrDefault(key, defaultVal, option.WithSeparator(sep)) -} diff --git a/vendor/github.com/obalunenko/getenv/internal/constraint.go b/vendor/github.com/obalunenko/getenv/internal/constraint.go index f82ab686..e777f369 100644 --- a/vendor/github.com/obalunenko/getenv/internal/constraint.go +++ b/vendor/github.com/obalunenko/getenv/internal/constraint.go @@ -16,7 +16,7 @@ type String interface { // Int is a constraint for integer and slice of integers. type Int interface { - int | []int | int64 | []int64 + int | []int | int64 | []int64 | uint64 | []uint64 } // Float is a constraint for floats and slice of floats. diff --git a/vendor/github.com/obalunenko/getenv/internal/iface.go b/vendor/github.com/obalunenko/getenv/internal/iface.go index b80b8e7f..34737451 100644 --- a/vendor/github.com/obalunenko/getenv/internal/iface.go +++ b/vendor/github.com/obalunenko/getenv/internal/iface.go @@ -24,11 +24,15 @@ func NewEnvParser(v any) EnvParser { case int64: p = int64Parser(v.(int64)) case []int64: - p = in64SliceParser(v.([]int64)) + p = int64SliceParser(v.([]int64)) case float64: p = float64Parser(v.(float64)) case []float64: p = float64SliceParser(v.([]float64)) + case uint64: + p = uint64Parser(v.(uint64)) + case []uint64: + p = uint64SliceParser(v.([]uint64)) case time.Time: p = timeParser(v.(time.Time)) case time.Duration: @@ -37,9 +41,7 @@ func NewEnvParser(v any) EnvParser { panic(fmt.Sprintf("unsupported type :%T", i)) } - return envParserWrap{ - EnvParser: p, - } + return p } // EnvParser interface for parsing environment variables. @@ -47,10 +49,6 @@ type EnvParser interface { ParseEnv(key string, defaltVal any, options Parameters) any } -type envParserWrap struct { - EnvParser -} - type stringParser string func (s stringParser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -105,9 +103,9 @@ func (i int64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } -type in64SliceParser []int64 +type int64SliceParser []int64 -func (i in64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { +func (i int64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { sep := options.Separator val := int64SliceOrDefault(key, defaltVal.([]int64), sep) @@ -148,3 +146,21 @@ func (d durationParser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } + +type uint64Parser uint64 + +func (d uint64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { + val := uint64OrDefault(key, defaltVal.(uint64)) + + return val +} + +type uint64SliceParser []uint64 + +func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { + sep := options.Separator + + val := uint64SliceOrDefault(key, defaltVal.([]uint64), sep) + + return val +} diff --git a/vendor/github.com/obalunenko/getenv/internal/parsers.go b/vendor/github.com/obalunenko/getenv/internal/parsers.go index 75f46519..ff165f9c 100644 --- a/vendor/github.com/obalunenko/getenv/internal/parsers.go +++ b/vendor/github.com/obalunenko/getenv/internal/parsers.go @@ -225,3 +225,53 @@ func float64OrDefault(key string, defaultVal float64) float64 { return val } + +// uint64OrDefault retrieves the unt64 value of the environment variable named +// by the key. +// If variable not set or value is empty - defaultVal will be returned. +func uint64OrDefault(key string, defaultVal uint64) uint64 { + env := stringOrDefault(key, "") + if env == "" { + return defaultVal + } + + const ( + base = 10 + bitsize = 64 + ) + + val, err := strconv.ParseUint(env, base, bitsize) + if err != nil { + return defaultVal + } + + return val +} + +// uint64SliceOrDefault retrieves the uint64 slice value of the environment variable named +// by the key and separated by sep. +// If variable not set or value is empty - defaultVal will be returned. +func uint64SliceOrDefault(key string, defaultVal []uint64, sep string) []uint64 { + valraw := stringSliceOrDefault(key, nil, sep) + if valraw == nil { + return defaultVal + } + + val := make([]uint64, 0, len(valraw)) + + const ( + base = 10 + bitsize = 64 + ) + + for _, s := range valraw { + v, err := strconv.ParseUint(s, base, bitsize) + if err != nil { + return defaultVal + } + + val = append(val, v) + } + + return val +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 75cb4eeb..0c3bb87b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -33,8 +33,8 @@ github.com/mattn/go-colorable # github.com/mattn/go-isatty v0.0.8 ## explicit github.com/mattn/go-isatty -# github.com/obalunenko/getenv v1.2.1 -## explicit; go 1.19 +# github.com/obalunenko/getenv v1.3.0 +## explicit; go 1.20 github.com/obalunenko/getenv github.com/obalunenko/getenv/internal github.com/obalunenko/getenv/option