Merge pull request 'add example' (#11) from example into master
Some checks failed
continuous-integration/drone/push Build was killed

Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
2024-01-25 23:58:31 +03:00
5 changed files with 300 additions and 0 deletions

172
client_example_test.go Normal file
View File

@@ -0,0 +1,172 @@
package config_test
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/arg"
"gitoa.ru/go-4devs/config/provider/env"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/test"
)
func ExampleClient_Value() {
ctx := context.Background()
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{"main.go", "--host=gitoa.ru"}
// read json config
config, err := config.New(
arg.New(),
env.New(test.Namespace, test.AppName),
)
if err != nil {
log.Print(err)
return
}
port, err := config.Value(ctx, "listen")
if err != nil {
log.Print("listen", err)
return
}
hostValue, err := config.Value(ctx, "host")
if err != nil {
log.Print("host ", err)
return
}
fmt.Printf("listen from env: %d\n", port.Int())
fmt.Printf("replace env host by args: %v\n", hostValue.String())
// Output:
// listen from env: 8080
// replace env host by args: gitoa.ru
}
func ExampleClient_Watch() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")
watcher, err := config.New(
watcher.New(time.Microsecond, env.New(test.Namespace, test.AppName)),
)
if err != nil {
log.Print(err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) error {
fmt.Println("update example_enable old: ", oldVar.Bool(), " new:", newVar.Bool())
wg.Done()
return nil
}, "example_enable")
if err != nil {
log.Print(err)
return
}
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) error {
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
wg.Done()
return nil
}, "example_db_dsn")
if err != nil {
log.Print(err)
return
}
wg.Wait()
// Output:
// update example_enable old: true new: false
}
func ExampleClient_Value_factory() {
ctx := context.Background()
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
_ = os.Setenv("FDEVS_GOLANG_HOST", "go.dev")
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{"main.go", "--env=golang"}
config, err := config.New(
arg.New(),
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
val, err := cfg.Value(ctx, "env")
if err != nil {
return nil, fmt.Errorf("failed read config file:%w", err)
}
return env.New(test.Namespace, val.String()), nil
}),
env.New(test.Namespace, test.AppName),
)
if err != nil {
log.Print(err)
return
}
envName, err := config.Value(ctx, "env")
if err != nil {
log.Print("env ", err)
return
}
host, err := config.Value(ctx, "host")
if err != nil {
log.Print("host ", err)
return
}
listen, err := config.Value(ctx, "listen")
if err != nil {
log.Print("listen", err)
return
}
fmt.Printf("envName from env: %s\n", envName.String())
fmt.Printf("host from env with app name golang: %s\n", host.String())
fmt.Printf("listen from env with default app name: %s\n", listen.String())
// Output:
// envName from env: golang
// host from env with app name golang: go.dev
// listen from env with default app name: 8080
}

View File

@@ -0,0 +1,32 @@
package definition
import (
"context"
"time"
"github.com/sirupsen/logrus"
"gitoa.ru/go-4devs/config/definition"
"gitoa.ru/go-4devs/config/definition/group"
"gitoa.ru/go-4devs/config/definition/option"
"gitoa.ru/go-4devs/config/definition/proto"
"gitoa.ru/go-4devs/log/level"
)
func Configure(ctx context.Context, def *definition.Definition) error {
def.Add(
option.String("test", "test description", option.Default("defult")),
option.Int("int", "test int description", option.Default(2)),
group.New("group", "group description", option.String("test", "test description")),
option.Time("start", "start at", option.Default(time.Now())),
option.Duration("timer", "timer", option.Default(time.Hour)),
group.New("log", "logger",
option.New("level", "log level", level.Level(0), option.Default(level.Debug)),
option.New("logrus", "logrus level", logrus.Level(0), option.Default(logrus.DebugLevel)),
proto.New("sevice", "cutom service log", option.New("level", "log level", level.Level(0), option.Default(level.Debug))),
),
option.New("erors", "skiped errors", []string{}),
proto.New("proto_errors", "proto errors", option.New("erors", "skiped errors", []string{})),
)
return nil
}

View File

@@ -0,0 +1,61 @@
//go:build ignore
// +build ignore
package main
import (
"context"
"fmt"
"go/format"
"os"
"gitoa.ru/go-4devs/config/definition"
"gitoa.ru/go-4devs/config/definition/generate"
"gitoa.ru/go-4devs/config/eample"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stdout, err)
os.Exit(1)
}
}
func run() error {
ctx := context.Background()
def := definition.New()
if err := eample.Configure(ctx, &def); err != nil {
return err
}
f, err := os.Create("eample/defenition_input.go")
if err != nil {
return err
}
gerr := generate.Run(f, "eample", def, generate.ViewOption{
Struct: "Configure",
Suffix: "Input",
Errors: generate.ViewErrors{
Default: []string{
"gitoa.ru/go-4devs/config.ErrVariableNotFound",
},
},
})
if gerr != nil {
return gerr
}
in, err := os.ReadFile(f.Name())
if err != nil {
return err
}
out, err := format.Source(in)
if err != nil {
return err
}
return os.WriteFile(f.Name(), out, 0644)
}

14
example/go.mod Normal file
View File

@@ -0,0 +1,14 @@
module gitoa.ru/go-4devs/config/example
go 1.21.5
require (
github.com/sirupsen/logrus v1.9.3
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8
gitoa.ru/go-4devs/log v0.5.3
)
require (
github.com/iancoleman/strcase v0.3.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

21
example/go.sum Normal file
View File

@@ -0,0 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8 h1:PZ4SE0gq719+lXorGaRPfGSSO2JpfxTWCWOSsna+7Yw=
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8/go.mod h1:jV6jF0PsK4ffNC8hrBxMx33PVJtnW5O6S/sGNJDqrd4=
gitoa.ru/go-4devs/log v0.5.3 h1:o/4DcypxbgQ9GEfUWrZ3FVrVfttuJgLs2ptMVPj47sE=
gitoa.ru/go-4devs/log v0.5.3/go.mod h1:tREtjEH2cTHl0p3uCVcH9g5tlqtsVNI/tDQVfq53Ty4=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=