add example
This commit is contained in:
32
example/definition/defenition.go
Normal file
32
example/definition/defenition.go
Normal 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
|
||||
}
|
||||
61
example/definition/defenition_config_bootstrap.go
Normal file
61
example/definition/defenition_config_bootstrap.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user