Skip to content

Commit

Permalink
add envconfig package and add values_by tag for support yaml list syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Feb 2, 2024
1 parent a6bf1b9 commit 0d430da
Show file tree
Hide file tree
Showing 22 changed files with 2,503 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/ddl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/unionj-cloud/go-doudou/v2/cmd/internal/ddl"
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/ddl/table/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
mapset "github.com/deckarep/golang-set"
"github.com/iancoleman/strcase"
"github.com/jmoiron/sqlx"
"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/testcontainers/testcontainers-go"
Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/svc/codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
var configTmpl = templates.EditableHeaderTmpl + `package config
import (
"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/unionj-cloud/go-doudou/v2/framework/config"
"github.com/unionj-cloud/go-doudou/v2/toolkit/errorx"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
)
var G_Config *Config
Expand All @@ -32,7 +32,7 @@ func init() {
var conf Config
err := envconfig.Process("{{.ServiceName}}", &conf)
if err != nil {
errorx.Panic("Error processing environment variables")
zlogger.Panic().Msgf("Error processing environment variables: %v", err)
}
G_Config = &conf
}
Expand Down
1 change: 0 additions & 1 deletion cmd/internal/svc/codegen/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/iancoleman/strcase v0.1.3
github.com/jmoiron/sqlx v1.3.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/opentracing-contrib/go-stdlib v1.0.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/svc/codegen/testdata/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/sirupsen/logrus"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/testsvc/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/sirupsen/logrus"
)

Expand Down
30 changes: 15 additions & 15 deletions framework/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"github.com/wubin1989/nacos-sdk-go/v2/vo"
_ "go.uber.org/automaxprocs"

"github.com/kelseyhightower/envconfig"
"github.com/unionj-cloud/go-doudou/v2/framework/configmgr"
"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
"github.com/unionj-cloud/go-doudou/v2/toolkit/dotenv"
"github.com/unionj-cloud/go-doudou/v2/toolkit/envconfig"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
"github.com/unionj-cloud/go-doudou/v2/toolkit/yaml"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
Expand Down Expand Up @@ -97,7 +97,7 @@ func init() {
LoadConfigFromRemote()
err := envconfig.Process("gdd", GddConfig)
if err != nil {
zlogger.Panic().Msgf("Error processing environment variables: %s", err.Error())
zlogger.Panic().Msgf("Error processing environment variables: %v", err)
}
zl, _ := zerolog.ParseLevel(GddLogLevel.LoadOrDefault(DefaultGddLogLevel))
opts := []zlogger.LoggerConfigOption{
Expand Down Expand Up @@ -534,19 +534,19 @@ type Config struct {
}

type gddConfig struct {
Banner bool
BannerText string `default:"go-doudou" split_words:"true"`
RouteRootPath string `default:"/" split_words:"true"`
WriteTimeout time.Duration `default:"15s" split_words:"true"`
ReadTimeout time.Duration `default:"15s" split_words:"true"`
IdleTimeout time.Duration `default:"60s" split_words:"true"`
GraceTimeout time.Duration `default:"15s" split_words:"true"`
Port string `default:"6060"`
Host string
EnableResponseGzip bool `default:"true" split_words:"true"`
LogReqEnable bool `default:"false" split_words:"true"`
ManageEnable bool `default:"true" split_words:"true"`
Grpc struct {
Banner bool
BannerText string `default:"go-doudou" split_words:"true"`
RouteRootPath string `default:"/" split_words:"true"`
WriteTimeout time.Duration `default:"15s" split_words:"true"`
ReadTimeout time.Duration `default:"15s" split_words:"true"`
IdleTimeout time.Duration `default:"60s" split_words:"true"`
GraceTimeout time.Duration `default:"15s" split_words:"true"`
Port string `default:"6060"`
Host string
EnableResponseGzip bool `default:"true" split_words:"true"`
LogReqEnable bool `default:"false" split_words:"true"`
ManageEnable bool `default:"true" split_words:"true"`
Grpc struct {
Port string `default:"50051"`
}
Config
Expand Down
19 changes: 19 additions & 0 deletions toolkit/envconfig/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 Kelsey Hightower

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
231 changes: 231 additions & 0 deletions toolkit/envconfig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# envconfig

[![Build Status](https://travis-ci.org/kelseyhightower/envconfig.svg)](https://travis-ci.org/kelseyhightower/envconfig)

```Go
import "github.com/kelseyhightower/envconfig"
```

## Documentation

See [godoc](http://godoc.org/github.com/kelseyhightower/envconfig)

## Usage

Set some environment variables:

```Bash
export MYAPP_DEBUG=false
export MYAPP_PORT=8080
export MYAPP_USER=Kelsey
export MYAPP_RATE="0.5"
export MYAPP_TIMEOUT="3m"
export MYAPP_USERS="rob,ken,robert"
export MYAPP_COLORCODES="red:1,green:2,blue:3"
```

Write some code:

```Go
package main

import (
"fmt"
"log"
"time"

"github.com/kelseyhightower/envconfig"
)

type Specification struct {
Debug bool
Port int
User string
Users []string
Rate float32
Timeout time.Duration
ColorCodes map[string]int
}

func main() {
var s Specification
err := envconfig.Process("myapp", &s)
if err != nil {
log.Fatal(err.Error())
}
format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nTimeout: %s\n"
_, err = fmt.Printf(format, s.Debug, s.Port, s.User, s.Rate, s.Timeout)
if err != nil {
log.Fatal(err.Error())
}

fmt.Println("Users:")
for _, u := range s.Users {
fmt.Printf(" %s\n", u)
}

fmt.Println("Color codes:")
for k, v := range s.ColorCodes {
fmt.Printf(" %s: %d\n", k, v)
}
}
```

Results:

```Bash
Debug: false
Port: 8080
User: Kelsey
Rate: 0.500000
Timeout: 3m0s
Users:
rob
ken
robert
Color codes:
red: 1
green: 2
blue: 3
```

## Struct Tag Support

Envconfig supports the use of struct tags to specify alternate, default, and required
environment variables.

For example, consider the following struct:

```Go
type Specification struct {
ManualOverride1 string `envconfig:"manual_override_1"`
DefaultVar string `default:"foobar"`
RequiredVar string `required:"true"`
IgnoredVar string `ignored:"true"`
AutoSplitVar string `split_words:"true"`
RequiredAndAutoSplitVar string `required:"true" split_words:"true"`
}
```

Envconfig has automatic support for CamelCased struct elements when the
`split_words:"true"` tag is supplied. Without this tag, `AutoSplitVar` above
would look for an environment variable called `MYAPP_AUTOSPLITVAR`. With the
setting applied it will look for `MYAPP_AUTO_SPLIT_VAR`. Note that numbers
will get globbed into the previous word. If the setting does not do the
right thing, you may use a manual override.

Envconfig will process value for `ManualOverride1` by populating it with the
value for `MYAPP_MANUAL_OVERRIDE_1`. Without this struct tag, it would have
instead looked up `MYAPP_MANUALOVERRIDE1`. With the `split_words:"true"` tag
it would have looked up `MYAPP_MANUAL_OVERRIDE1`.

```Bash
export MYAPP_MANUAL_OVERRIDE_1="this will be the value"

# export MYAPP_MANUALOVERRIDE1="and this will not"
```

If envconfig can't find an environment variable value for `MYAPP_DEFAULTVAR`,
it will populate it with "foobar" as a default value.

If envconfig can't find an environment variable value for `MYAPP_REQUIREDVAR`,
it will return an error when asked to process the struct. If
`MYAPP_REQUIREDVAR` is present but empty, envconfig will not return an error.

If envconfig can't find an environment variable in the form `PREFIX_MYVAR`, and there
is a struct tag defined, it will try to populate your variable with an environment
variable that directly matches the envconfig tag in your struct definition:

```shell
export SERVICE_HOST=127.0.0.1
export MYAPP_DEBUG=true
```
```Go
type Specification struct {
ServiceHost string `envconfig:"SERVICE_HOST"`
Debug bool
}
```

Envconfig won't process a field with the "ignored" tag set to "true", even if a corresponding
environment variable is set.

## Supported Struct Field Types

envconfig supports these struct field types:

* string
* int8, int16, int32, int64
* bool
* float32, float64
* slices of any supported type
* maps (keys and values of any supported type)
* [encoding.TextUnmarshaler](https://golang.org/pkg/encoding/#TextUnmarshaler)
* [encoding.BinaryUnmarshaler](https://golang.org/pkg/encoding/#BinaryUnmarshaler)
* [time.Duration](https://golang.org/pkg/time/#Duration)

Embedded structs using these fields are also supported.

## Custom Decoders

Any field whose type (or pointer-to-type) implements `envconfig.Decoder` can
control its own deserialization:

```Bash
export DNS_SERVER=8.8.8.8
```

```Go
type IPDecoder net.IP

func (ipd *IPDecoder) Decode(value string) error {
*ipd = IPDecoder(net.ParseIP(value))
return nil
}

type DNSConfig struct {
Address IPDecoder `envconfig:"DNS_SERVER"`
}
```

Example for decoding the environment variables into map[string][]structName type

```Bash
export SMS_PROVIDER_WITH_WEIGHT= `IND=[{"name":"SMSProvider1","weight":70},{"name":"SMSProvider2","weight":30}];US=[{"name":"SMSProvider1","weight":100}]`
```

```GO
type providerDetails struct {
Name string
Weight int
}

type SMSProviderDecoder map[string][]providerDetails

func (sd *SMSProviderDecoder) Decode(value string) error {
smsProvider := map[string][]providerDetails{}
pairs := strings.Split(value, ";")
for _, pair := range pairs {
providerdata := []providerDetails{}
kvpair := strings.Split(pair, "=")
if len(kvpair) != 2 {
return fmt.Errorf("invalid map item: %q", pair)
}
err := json.Unmarshal([]byte(kvpair[1]), &providerdata)
if err != nil {
return fmt.Errorf("invalid map json: %w", err)
}
smsProvider[kvpair[0]] = providerdata

}
*sd = SMSProviderDecoder(smsProvider)
return nil
}

type SMSProviderConfig struct {
ProviderWithWeight SMSProviderDecoder `envconfig:"SMS_PROVIDER_WITH_WEIGHT"`
}
```

Also, envconfig will use a `Set(string) error` method like from the
[flag.Value](https://godoc.org/flag#Value) interface if implemented.
8 changes: 8 additions & 0 deletions toolkit/envconfig/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2013 Kelsey Hightower. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in
// the LICENSE file.

// Package envconfig implements decoding of environment variables based on a user
// defined specification. A typical use is using environment variables for
// configuration settings.
package envconfig
7 changes: 7 additions & 0 deletions toolkit/envconfig/env_os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build appengine go1.5

package envconfig

import "os"

var lookupEnv = os.LookupEnv
7 changes: 7 additions & 0 deletions toolkit/envconfig/env_syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !appengine,!go1.5

package envconfig

import "syscall"

var lookupEnv = syscall.Getenv
Loading

0 comments on commit 0d430da

Please sign in to comment.