Skip to content

Commit

Permalink
up: update readme and examples, default recommend use yaml.v3
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2022
1 parent 46cb2f1 commit 808bb97
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ package main

import (
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/config/v2/yamlv3"
)

// go run ./examples/yaml.go
func main() {
config.WithOptions(config.ParseEnv)

// add driver for support yaml content
config.AddDriver(yaml.Driver)
config.AddDriver(yamlv3.Driver)

err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ package main

import (
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/config/v2/yamlv3"
)

// go run ./examples/yaml.go
Expand All @@ -84,7 +84,7 @@ func main() {
config.WithOptions(config.ParseEnv)

// 添加驱动程序以支持yaml内容解析(除了JSON是默认支持,其他的则是按需使用)
config.AddDriver(yaml.Driver)
config.AddDriver(yamlv3.Driver)

// 加载配置,可以同时传入多个文件
err := config.LoadFiles("testdata/yml_base.yml")
Expand Down
6 changes: 3 additions & 3 deletions _examples/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"fmt"

"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/config/v2/yamlv3"
)

// go run ./examples/yaml.go
func main() {
config.WithOptions(config.ParseEnv)

// only add decoder
// config.SetDecoder(config.Yaml, yaml.Decoder)
// config.SetDecoder(config.Yaml, yamlv3.Decoder)
// Or
config.AddDriver(yaml.Driver)
config.AddDriver(yamlv3.Driver)

err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ var jsonStr = `{
func Example() {
// WithOptions(ParseEnv)

// add Decoder and Encoder
// use yaml github.com/gookit/config/yaml
// AddDriver(Yaml, yaml.Driver)
// use yaml github.com/gookit/config/yamlv3
// AddDriver(Yaml, yamlv3.Driver)
// use toml github.com/gookit/config/toml
// AddDriver(Toml, toml.Driver)
// use toml github.com/gookit/config/hcl
// AddDriver(Hcl, hcl.Driver)
// Or
// config.DecoderEncoder(config.JSON, yaml.Decoder, yaml.Encoder)

err := LoadFiles("testdata/json_base.json")
if err != nil {
Expand Down
21 changes: 3 additions & 18 deletions dotnev/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import (
"github.com/gookit/ini/v2/dotenv"
)

var (
// UpperEnvKey change key to upper on set ENV
UpperEnvKey = true

// DefaultName default file name
DefaultName = ".env"

// OnlyLoadExists load on file exists
OnlyLoadExists bool

// save original Env data
// originalEnv []string

// cache all loaded ENV data
// loadedData = map[string]string{}
)

// LoadedData get all loaded data by dontenv
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func LoadedData() map[string]string {
Expand All @@ -45,7 +28,9 @@ func DontUpperEnvKey() {
// Load parse .env file data to os ENV.
//
// Usage:
// dotenv.Load("./", ".env")
//
// dotenv.Load("./", ".env")
//
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func Load(dir string, filenames ...string) (err error) {
return dotenv.Load(dir, filenames...)
Expand Down
24 changes: 0 additions & 24 deletions dotnev/dotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,3 @@ func TestLoadFromMap(t *testing.T) {
})
assert.Error(t, err)
}

func TestDontUpperEnvKey(t *testing.T) {
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))

DontUpperEnvKey()

err := LoadFromMap(map[string]string{
"dont_env_test": "val",
})

assert.Contains(t, fmt.Sprint(os.Environ()), "dont_env_test=val")
assert.NoError(t, err)
assert.Equal(t, "val", Get("dont_env_test"))

// on windows, os.Getenv() not case sensitive
if runtime.GOOS == "windows" {
assert.Equal(t, "val", Get("DONT_ENV_TEST"))
} else {
assert.Equal(t, "", Get("DONT_ENV_TEST"))
}

UpperEnvKey = true // revert
ClearLoaded()
}
2 changes: 1 addition & 1 deletion yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Package yaml is a driver use YAML format content as config source
Usage please see example:
*/
package yaml

Expand All @@ -19,4 +18,5 @@ var Decoder config.Decoder = yaml.Unmarshal
var Encoder config.Encoder = yaml.Marshal

// Driver for yaml
// TIP: recommended use the yamlv3.Driver
var Driver = config.NewDriver(config.Yaml, Decoder, Encoder)

0 comments on commit 808bb97

Please sign in to comment.