diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..0d3bd399 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,28 @@ +[*] +charset = utf-8 +end_of_line = lf +trim_trailing_whitespace = true +max_line_length = 120 + +[{go.mod,go.sum,*.go}] +insert_final_newline = true +indent_size = tab +indent_style = tab +tab_width = 4 + +[Makefile] +max_line_length = off +insert_final_newline = true +indent_size = tab +indent_style = tab +tab_width = 4 + +[*.md] +max_line_length = off +trim_trailing_whitespace = false +indent_size = tab +indent_style = space +tab_width = 2 + +[.mailmap] +max_line_length = off diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 49d3ea8c..571a1620 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -35,7 +35,7 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.txt - uses: goreleaser/goreleaser-action@v6 - if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable' + if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable' # editorconfig-checker-disable-line with: version: latest distribution: goreleaser-pro diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 90b0fe45..a26c4d1a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,8 +1,15 @@ -name: golangci-lint +name: linters on: push: pull_request: jobs: + editorconfig: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: editorconfig-checker/action-editorconfig-checker@main + - run: editorconfig-checker + golangci: name: lint runs-on: ubuntu-latest diff --git a/.golangci.yml b/.golangci.yml index ff791f86..73db6131 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,3 +1,16 @@ +linters-settings: + revive: + rules: + - name: line-length-limit + arguments: [120] + +issues: + exclude-rules: + - path: _test\.go + linters: + - revive + text: "line-length-limit:" + linters: enable: - thelper @@ -6,3 +19,4 @@ linters: - unconvert - unparam - wastedassign + - revive diff --git a/README.md b/README.md index 3625727a..9c8f738a 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ err := env.Parse(&cfg) cfg, err := env.ParseAs[config]() ``` -You can see the full documentation and list of examples at -[pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11). +You can see the full documentation and list of examples at [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11). --- @@ -27,7 +26,7 @@ You can see the full documentation and list of examples at

- encore icon + encore icon

@@ -48,19 +47,15 @@ You can see the full documentation and list of examples at - `Parse`: parse the current environment into a type - `ParseAs`: parse the current environment into a type using generics -- `ParseWithOptions`: parse the current environment into a type with custom - options -- `ParseAsithOptions`: parse the current environment into a type with custom - options and using generics +- `ParseWithOptions`: parse the current environment into a type with custom options +- `ParseAsithOptions`: parse the current environment into a type with custom options and using generics - `Must`: can be used to wrap `Parse.*` calls to panic on error - `GetFieldParams`: get the `env` parsed options for a type -- `GetFieldParamsWithOptions`: get the `env` parsed options for a type with - custom options +- `GetFieldParamsWithOptions`: get the `env` parsed options for a type with custom options ### Supported types -Out of the box all built-in types are supported, plus a few others that -are commonly used. +Out of the box all built-in types are supported, plus a few others that are commonly used. Complete list: @@ -83,8 +78,7 @@ Complete list: - `encoding.TextUnmarshaler` - `url.URL` -Pointers, slices and slices of pointers, and maps of those types are also -supported. +Pointers, slices and slices of pointers, and maps of those types are also supported. You may also add custom parsers for your types. @@ -92,15 +86,11 @@ You may also add custom parsers for your types. The following tags are provided: -- `env`: sets the environment variable name and optionally takes the tag options - described below +- `env`: sets the environment variable name and optionally takes the tag options described below - `envDefault`: sets the default value for the field -- `envPrefix`: can be used in a field that is a complex type to set a prefix to - all environment variables used in it -- `envSeparator`: sets the character to be used to separate items in slices and - maps (default: `,`) -- `envKeyValSeparator`: sets the character to be used to separate keys and their - values in maps (default: `:`) +- `envPrefix`: can be used in a field that is a complex type to set a prefix to all environment variables used in it +- `envSeparator`: sets the character to be used to separate items in slices and maps (default: `,`) +- `envKeyValSeparator`: sets the character to be used to separate keys and their values in maps (default: `:`) ### `env` tag options @@ -119,20 +109,16 @@ There are a few options available in the functions that end with `WithOptions`: - `Environment`: keys and values to be used instead of `os.Environ()` - `TagName`: specifies another tag name to use rather than the default `env` -- `RequiredIfNoDef`: set all `env` fields as required if they do not declare - `envDefault` -- `OnSet`: allows to hook into the `env` parsing and do something when a value - is set +- `RequiredIfNoDef`: set all `env` fields as required if they do not declare `envDefault` +- `OnSet`: allows to hook into the `env` parsing and do something when a value is set - `Prefix`: prefix to be used in all environment variables - `UseFieldNameByDefault`: defines whether or not `env` should use the field name by default if the `env` key is missing - `FuncMap`: custom parse functions for custom types ### Documentation and examples -Examples are live in -[pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11), -and also in the -[example test file](./example_test.go). +Examples are live in [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11), +and also in the [example test file](./example_test.go). ## Badges diff --git a/env.go b/env.go index fd2f1692..e62b2224 100644 --- a/env.go +++ b/env.go @@ -125,7 +125,12 @@ type ParserFunc func(v string) (interface{}, error) type OnSetFn func(tag string, value interface{}, isDefault bool) // processFieldFn is a function which takes all information about a field and processes it. -type processFieldFn func(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error +type processFieldFn func( + refField reflect.Value, + refTypeField reflect.StructField, + opts Options, + fieldParams FieldParams, +) error // Options for the parser. type Options struct { @@ -332,7 +337,12 @@ func doParse(ref reflect.Value, processField processFieldFn, opts Options) error return agrErr } -func doParseField(refField reflect.Value, refTypeField reflect.StructField, processField processFieldFn, opts Options) error { +func doParseField( + refField reflect.Value, + refTypeField reflect.StructField, + processField processFieldFn, + opts Options, +) error { if !refField.CanSet() { return nil } @@ -559,7 +569,12 @@ func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, err func get(fieldParams FieldParams, opts Options) (val string, err error) { var exists, isDefault bool - val, exists, isDefault = getOr(fieldParams.Key, fieldParams.DefaultValue, fieldParams.HasDefaultValue, opts.Environment) + val, exists, isDefault = getOr( + fieldParams.Key, + fieldParams.DefaultValue, + fieldParams.HasDefaultValue, + opts.Environment, + ) if fieldParams.Expand { val = os.Expand(val, opts.getRawEnv) diff --git a/env_test.go b/env_test.go index 121edc54..69b17134 100644 --- a/env_test.go +++ b/env_test.go @@ -1,3 +1,4 @@ +// editorconfig-checker-disable-file package env import ( @@ -485,7 +486,7 @@ func TestParseCustomMapType(t *testing.T) { var cfg config isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ - reflect.TypeOf(custommap{}): func(value string) (interface{}, error) { + reflect.TypeOf(custommap{}): func(_ string) (interface{}, error) { return custommap(map[string]bool{}), nil }, }})) @@ -547,7 +548,7 @@ func TestParseMapCustomKeyTypeError(t *testing.T) { var cfg config err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ - reflect.TypeOf(CustomKey("")): func(value string) (interface{}, error) { + reflect.TypeOf(CustomKey("")): func(_ string) (interface{}, error) { return nil, fmt.Errorf("custom error") }, }}) @@ -565,7 +566,7 @@ func TestParseMapCustomValueTypeError(t *testing.T) { var cfg config err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ - reflect.TypeOf(Customval("")): func(value string) (interface{}, error) { + reflect.TypeOf(Customval("")): func(_ string) (interface{}, error) { return nil, fmt.Errorf("custom error") }, }}) @@ -1148,7 +1149,7 @@ func TestCustomParserError(t *testing.T) { name string } - customParserFunc := func(v string) (interface{}, error) { + customParserFunc := func(_ string) (interface{}, error) { return nil, errors.New("something broke") } diff --git a/env_tomap_windows_test.go b/env_tomap_windows_test.go index dc1458b3..d8efdc64 100644 --- a/env_tomap_windows_test.go +++ b/env_tomap_windows_test.go @@ -4,7 +4,8 @@ package env import "testing" -// On Windows, environment variables can start with '='. This test verifies this behavior without relying on a Windows environment. +// On Windows, environment variables can start with '='. +// This test verifies this behavior without relying on a Windows environment. // See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58 func TestToMapWindows(t *testing.T) { envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR", "FOO=", "BAR"} diff --git a/error.go b/error.go index a5c6ae43..a08c2b4d 100644 --- a/error.go +++ b/error.go @@ -6,7 +6,8 @@ import ( "strings" ) -// An aggregated error wrapper to combine gathered errors. This allows either to display all errors or convert them individually +// An aggregated error wrapper to combine gathered errors. +// This allows either to display all errors or convert them individually // List of the available errors // ParseError // NotStructPtrError diff --git a/go.mod b/go.mod index affcbd49..92cabd79 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,10 @@ module github.com/caarlos0/env/v11 -retract v11.0.1 // v11.0.1 accidentally introduced a breaking change regarding the behavior of nil pointers. You can now chose to auto-initialize them by setting the `init` tag option. +// v11.0.1 accidentally introduced a breaking change regarding the behavior of nil pointers. +// You can now chose to auto-initialize them by setting the `init` tag option. +retract v11.0.1 -retract v11.2.0 // v11.2.0 accidentally introduced a breaking change regarding the behavior of nil slices of complex types. +// v11.2.0 accidentally introduced a breaking change regarding the behavior of nil slices of complex types. +retract v11.2.0 go 1.18