From 65d7c0e26ce95eeffb0a38f5aa5c9106aeb9cf52 Mon Sep 17 00:00:00 2001 From: Zero Date: Sun, 21 Apr 2024 20:07:05 +0800 Subject: [PATCH 1/2] feat: support option allow env to load config from env --- config.go | 20 ++++++++++++++++++-- config_test.go | 19 +++++++++++++++++++ env.go | 25 +++++++++++++++++++++++++ go.mod | 8 ++++++-- go.sum | 14 +++++++++----- 5 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 env.go diff --git a/config.go b/config.go index 59ff54a..3ead6f3 100644 --- a/config.go +++ b/config.go @@ -28,6 +28,9 @@ type LoadOptions struct { // Config name, default: config.yml, and type is YAML Name string + + // AllowEnv is whether to allow environment variables in the config file, default is false. + AllowEnv bool } // Load loads the config from the given file path. @@ -44,8 +47,9 @@ func Load(config any, options ...*LoadOptions) error { fileType := DefaultFileType filepathX := fs.JoinCurrentDir(".config.yml") + optionsX := &LoadOptions{} if len(options) > 0 && options[0] != nil { - optionsX := options[0] + optionsX = options[0] if optionsX.FilePath != "" { filepathX = optionsX.FilePath @@ -84,6 +88,10 @@ func Load(config any, options ...*LoadOptions) error { } if !fs.IsExist(filepathX) { + if optionsX.AllowEnv { + return Env(config) + } + return fmt.Errorf("config path (%s) not found", filepathX) } @@ -115,5 +123,13 @@ func Load(config any, options ...*LoadOptions) error { tg := tag.New("config", datasource.NewMapDataSource(configDataSource)) - return tg.Decode(config) + if err := tg.Decode(config); err != nil { + return err + } + + if optionsX.AllowEnv { + return Env(config) + } + + return nil } diff --git a/config_test.go b/config_test.go index 03ca35a..f161303 100644 --- a/config_test.go +++ b/config_test.go @@ -1,6 +1,7 @@ package config import ( + "os" "testing" ) @@ -33,6 +34,9 @@ type Config struct { Crypto string `config:"crypto"` // Actions map[string]Action `config:"actions"` + + // + AllowEnvValue string `config:"allow_env_value" env:"ALLOW_ENV_VALUE"` } type Action struct { @@ -118,3 +122,18 @@ func TestConfig(t *testing.T) { t.Fatal("actions.action1.socks5 is not empty string") } } + +func TestConfigAllowEnv(t *testing.T) { + var cfg Config + os.Setenv("ALLOW_ENV_VALUE", "value_from_env_with_allow_env") + if err := Load(&cfg, &LoadOptions{ + FilePath: "./testdata/config.yml", + AllowEnv: true, + }); err != nil { + t.Fatal(err) + } + + if cfg.AllowEnvValue != "value_from_env_with_allow_env" { + t.Fatalf("allow_env_value is not value_from_env_with_allow_env, but got %s", cfg.AllowEnvValue) + } +} diff --git a/env.go b/env.go new file mode 100644 index 0000000..b1dc63e --- /dev/null +++ b/env.go @@ -0,0 +1,25 @@ +package config + +import ( + "github.com/go-zoox/tag" + "github.com/go-zoox/tag/datasource" +) + +// Env loads the config from the environment variables. +// The config must be a pointer to a struct. +// Example: +// +// type Config struct { +// Host string `env:"HOST"` +// Port int `env:"PORT"` +// } +// +// var config Config +// if err := config.Env(&config); err != nil { +// log.Fatal(err) +// } +func Env(config interface{}) error { + tg := tag.New("env", datasource.NewEnvSource()) + + return tg.Decode(config) +} diff --git a/go.mod b/go.mod index aaa31f2..826f16d 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,11 @@ module github.com/go-zoox/config go 1.18 require ( - github.com/go-zoox/core-utils v1.2.9 + github.com/go-zoox/core-utils v1.3.5 + github.com/go-zoox/dotenv v1.2.5 github.com/go-zoox/encoding v1.2.1 github.com/go-zoox/fs v1.3.13 - github.com/go-zoox/tag v1.2.2 + github.com/go-zoox/tag v1.2.6 ) require ( @@ -16,6 +17,7 @@ require ( github.com/go-zoox/uuid v0.0.1 // indirect github.com/goccy/go-yaml v1.9.8 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/joho/godotenv v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/pelletier/go-toml v1.9.5 // indirect @@ -23,3 +25,5 @@ require ( golang.org/x/sys v0.7.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) + +replace github.com/go-zoox/tag => ../tag diff --git a/go.sum b/go.sum index 76a4a2d..23559b9 100644 --- a/go.sum +++ b/go.sum @@ -12,17 +12,19 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/go-zoox/core-utils v1.2.9 h1:OEp4BEk/s/DAIqNz2hJC8fQJPlK9UyKz+BjcIwfkATk= -github.com/go-zoox/core-utils v1.2.9/go.mod h1:Y6izFcxuELrkOen5mTQccCJxJqqPJaZV5dQtUMBdkBM= +github.com/go-zoox/core-utils v1.3.5 h1:uxnPjUdo20ZCYCJp+1deuoqdMByTTJxQFrMp8e5ty+E= +github.com/go-zoox/core-utils v1.3.5/go.mod h1:raOOwr2l2sJQyjR0Dg33sg0ry4U1/L2eNTuLFRpUXWs= +github.com/go-zoox/dotenv v1.2.5 h1:zCWYvy7zARJDR6kA9CcaYCfCx8zb3Cb5wem2MikZ8lY= +github.com/go-zoox/dotenv v1.2.5/go.mod h1:dWAYv/KrzLZDyo2ESShVHVysqcr3cjAR9vSMsL9U8Nk= github.com/go-zoox/encoding v1.2.1 h1:38rQRsfL1f1YHZaqsPaGcNMkPnzatnPlYiHioUh9F4A= github.com/go-zoox/encoding v1.2.1/go.mod h1:NdcM7Ln73oVW3vJgx3MH4fJknCcdQfq+NgJ0tuCo7tU= github.com/go-zoox/fs v1.3.13 h1:fe0uvtXCM+9s51z/CnQ5kxB4hBYaK55tkrE9gq0385U= github.com/go-zoox/fs v1.3.13/go.mod h1:wCM+UQkjFTxNjOOCNlGcN3k9FeXXUwn9bFnpyjOn55c= github.com/go-zoox/ini v1.0.4 h1:N4mUbAO0juYIRrv3ysjKtpEn/+yQv57eQietsgpkAYQ= github.com/go-zoox/ini v1.0.4/go.mod h1:SisQneNLb1EBeZ5bA5GnrJd8FNg372hQrPh+gb3IzV4= -github.com/go-zoox/tag v1.2.2 h1:MLtfFEEBVwV3HVhgNHXGDl0Xv4h9hj1+6Hx9GaRHvFc= -github.com/go-zoox/tag v1.2.2/go.mod h1:z9z4iZb/XPE4HwTXJgPIdwgH90c2NysGxIMq9tW+GuU= -github.com/go-zoox/testify v1.0.0 h1:zXuj+JMcudM/dWk8HgMfCKpGYDcyHbTUBGxH35SGubU= +github.com/go-zoox/tag v1.2.6 h1:TtqqB45XWupwHasEA+O+lhMsFbKOpXscebdd/CtPsPY= +github.com/go-zoox/tag v1.2.6/go.mod h1:r8rpj8j4cFe2d79oXjbEVgy9bVAWUvvgRblYo8tyhig= +github.com/go-zoox/testify v1.0.2 h1:G5sQ3xm0uwCuytnMhgnqZ5BItCt2DN3n2wLBqlIJEWA= github.com/go-zoox/uuid v0.0.1 h1:txqmDavRTq68gzzqWfJQLorFyUp9a7M2lmq2KcwPGPA= github.com/go-zoox/uuid v0.0.1/go.mod h1:0/F4LdfLqFdyqOf7aXoiYXRkXHU324JQ5DZEytXYBPM= github.com/goccy/go-yaml v1.9.8 h1:5gMyLUeU1/6zl+WFfR1hN7D2kf+1/eRGa7DFtToiBvQ= @@ -30,6 +32,8 @@ github.com/goccy/go-yaml v1.9.8/go.mod h1:JubOolP3gh0HpiBc4BLRD4YmjEjHAmIIB2aaXK github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= From 174884ad074a88f9a92104a68771872587ec7842 Mon Sep 17 00:00:00 2001 From: Zero Date: Sun, 21 Apr 2024 20:12:16 +0800 Subject: [PATCH 2/2] chore: upgrade dependencies --- go.mod | 6 +----- go.sum | 8 ++------ 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 826f16d..3724d7a 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,9 @@ go 1.18 require ( github.com/go-zoox/core-utils v1.3.5 - github.com/go-zoox/dotenv v1.2.5 github.com/go-zoox/encoding v1.2.1 github.com/go-zoox/fs v1.3.13 - github.com/go-zoox/tag v1.2.6 + github.com/go-zoox/tag v1.2.7 ) require ( @@ -17,7 +16,6 @@ require ( github.com/go-zoox/uuid v0.0.1 // indirect github.com/goccy/go-yaml v1.9.8 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/joho/godotenv v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/pelletier/go-toml v1.9.5 // indirect @@ -25,5 +23,3 @@ require ( golang.org/x/sys v0.7.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) - -replace github.com/go-zoox/tag => ../tag diff --git a/go.sum b/go.sum index 23559b9..e19dae8 100644 --- a/go.sum +++ b/go.sum @@ -14,16 +14,14 @@ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7a github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-zoox/core-utils v1.3.5 h1:uxnPjUdo20ZCYCJp+1deuoqdMByTTJxQFrMp8e5ty+E= github.com/go-zoox/core-utils v1.3.5/go.mod h1:raOOwr2l2sJQyjR0Dg33sg0ry4U1/L2eNTuLFRpUXWs= -github.com/go-zoox/dotenv v1.2.5 h1:zCWYvy7zARJDR6kA9CcaYCfCx8zb3Cb5wem2MikZ8lY= -github.com/go-zoox/dotenv v1.2.5/go.mod h1:dWAYv/KrzLZDyo2ESShVHVysqcr3cjAR9vSMsL9U8Nk= github.com/go-zoox/encoding v1.2.1 h1:38rQRsfL1f1YHZaqsPaGcNMkPnzatnPlYiHioUh9F4A= github.com/go-zoox/encoding v1.2.1/go.mod h1:NdcM7Ln73oVW3vJgx3MH4fJknCcdQfq+NgJ0tuCo7tU= github.com/go-zoox/fs v1.3.13 h1:fe0uvtXCM+9s51z/CnQ5kxB4hBYaK55tkrE9gq0385U= github.com/go-zoox/fs v1.3.13/go.mod h1:wCM+UQkjFTxNjOOCNlGcN3k9FeXXUwn9bFnpyjOn55c= github.com/go-zoox/ini v1.0.4 h1:N4mUbAO0juYIRrv3ysjKtpEn/+yQv57eQietsgpkAYQ= github.com/go-zoox/ini v1.0.4/go.mod h1:SisQneNLb1EBeZ5bA5GnrJd8FNg372hQrPh+gb3IzV4= -github.com/go-zoox/tag v1.2.6 h1:TtqqB45XWupwHasEA+O+lhMsFbKOpXscebdd/CtPsPY= -github.com/go-zoox/tag v1.2.6/go.mod h1:r8rpj8j4cFe2d79oXjbEVgy9bVAWUvvgRblYo8tyhig= +github.com/go-zoox/tag v1.2.7 h1:B+TLmlnm21YKZz+Ytmo4aiS1WhOc0pv+9IF2HodrGnM= +github.com/go-zoox/tag v1.2.7/go.mod h1:r8rpj8j4cFe2d79oXjbEVgy9bVAWUvvgRblYo8tyhig= github.com/go-zoox/testify v1.0.2 h1:G5sQ3xm0uwCuytnMhgnqZ5BItCt2DN3n2wLBqlIJEWA= github.com/go-zoox/uuid v0.0.1 h1:txqmDavRTq68gzzqWfJQLorFyUp9a7M2lmq2KcwPGPA= github.com/go-zoox/uuid v0.0.1/go.mod h1:0/F4LdfLqFdyqOf7aXoiYXRkXHU324JQ5DZEytXYBPM= @@ -32,8 +30,6 @@ github.com/goccy/go-yaml v1.9.8/go.mod h1:JubOolP3gh0HpiBc4BLRD4YmjEjHAmIIB2aaXK github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= -github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=