32 Commits

Author SHA1 Message Date
6cd208b6e8 add config definition
Some checks failed
continuous-integration/drone/push Build was killed
2024-01-25 23:51:15 +03:00
a02b701a11 add config definition 2024-01-25 23:32:49 +03:00
aeb90ceaa6 Merge pull request 'add provider vault' (#9) from vault into master
Some checks failed
continuous-integration/drone/push Build was killed
Reviewed-on: #9
2024-01-25 23:08:20 +03:00
80b0244b52 add provider vault
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-01-25 23:07:50 +03:00
947c40d918 Merge pull request 'add etcd provider' (#8) from etcd into master
Some checks failed
continuous-integration/drone/push Build was killed
Reviewed-on: #8
2024-01-25 22:49:02 +03:00
788e2928fe add etcd provider
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build was killed
2024-01-25 22:48:33 +03:00
66c1eef44a add assert tests
Some checks failed
continuous-integration/drone/push Build was killed
continuous-integration/drone/tag Build was killed
2024-01-25 22:30:15 +03:00
b7736b7d4c Merge pull request 'add toml provider' (#7) from toml into master
Some checks failed
continuous-integration/drone/push Build was killed
Reviewed-on: #7
2024-01-25 22:29:06 +03:00
fdab23b756 add toml provider
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build is passing
2024-01-25 22:04:58 +03:00
18ce790363 Merge pull request 'add provider ini' (#6) from ini into master
Some checks failed
continuous-integration/drone/push Build was killed
Reviewed-on: #6
2024-01-25 22:02:59 +03:00
5cb46f5030 add provider ini
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build is failing
2024-01-25 22:02:02 +03:00
dd37b51974 Merge pull request 'add yaml provider' (#5) from yaml into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #5
2024-01-25 21:39:05 +03:00
1088e26bcf add yaml provider
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build is passing
2024-01-25 21:35:01 +03:00
089f43dcb9 Merge pull request 'add json provider' (#4) from json into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #4
2024-01-25 21:30:11 +03:00
59f97e1681 add json provider
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build is passing
2024-01-25 21:23:39 +03:00
b9af510e71 update version golangci-lint
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2024-01-25 21:06:29 +03:00
4ef55a0d93 update version go for slog
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 21:01:28 +03:00
f094e4b48c remove race from test
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:57:43 +03:00
126ec4cb88 fix lint
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:56:21 +03:00
085589a938 add stop watch
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:49:37 +03:00
f4446378d4 update drone
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:36:51 +03:00
b866158d9b udate watcher
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:33:10 +03:00
d864996848 update go mod
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:08:57 +03:00
e2a2db01bb remove definition
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:44:58 +03:00
d5b5103f40 remove yaml provider
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:43:56 +03:00
11c3c3e5f8 remove json provider 2024-01-25 19:13:01 +03:00
792b48306a remove key
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:04:29 +03:00
4faa38fc84 remove toml provider
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:03:01 +03:00
3d2934239a remove toml provider 2024-01-25 18:56:20 +03:00
andrey
14ab46a1eb remove etcd provider
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 18:48:49 +03:00
andrey
996a948b7f update go.mod
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 18:31:58 +03:00
andrey
b3928d4dd1 remove vault provider
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 18:31:14 +03:00
75 changed files with 1097 additions and 1059 deletions

View File

@@ -1,29 +1,165 @@
---
kind: pipeline kind: pipeline
name: default name: default
steps:
- name: test
image: golang
commands:
# - go test -parallel 10 -race ./...
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- golangci-lint run
---
kind: pipeline
name: json
steps:
- name: test
image: golang
commands:
- cd provider/json
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/json
- golangci-lint run
---
kind: pipeline
name: yaml
steps:
- name: test
image: golang
commands:
- cd provider/yaml
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/yaml
- golangci-lint run
---
kind: pipeline
type: docker
name: ini
steps:
- name: test
image: golang
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
commands:
- cd provider/ini
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/ini
- golangci-lint run
---
kind: pipeline
type: docker
name: toml
steps:
- name: test
image: golang
commands:
- cd provider/toml
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/toml
- golangci-lint run
---
kind: pipeline
type: docker
name: etcd
environment:
FDEVS_CONFIG_ETCD_HOST: etcd:2379
services:
- name: etcd
image: bitnami/etcd:3.5.11
environment:
ALLOW_NONE_AUTHENTICATION: yes
steps:
- name: test
image: golang
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
commands:
- cd provider/etcd
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/etcd
- golangci-lint run
---
kind: pipeline
type: docker
name: vault
environment:
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200
VAULT_DEV_ROOT_TOKEN_ID: dev
services: services:
- name: vault - name: vault
image: vault:1.13.3 image: vault:1.13.3
environment: environment:
VAULT_DEV_ROOT_TOKEN_ID: dev VAULT_DEV_ROOT_TOKEN_ID: dev
VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200 VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200
- name: etcd
image: bitnami/etcd:3.5.11
environment:
ALLOW_NONE_AUTHENTICATION: yes
environment: steps:
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200 - name: test
VAULT_DEV_ROOT_TOKEN_ID: dev image: golang
FDEVS_CONFIG_ETCD_HOST: etcd:2379 failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
commands:
- cd provider/vault
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/vault
- golangci-lint run
---
kind: pipeline
type: docker
name: definition
steps: steps:
- name: test - name: test
image: golang image: golang
commands: commands:
- go test -parallel 10 -race ./... - cd definition
- go test ./...
- name: golangci-lint - name: golangci-lint
image: golangci/golangci-lint:v1.53 image: golangci/golangci-lint:v1.55
commands: commands:
- cd definition
- golangci-lint run - golangci-lint run

View File

@@ -24,6 +24,13 @@ linters-settings:
locale: US locale: US
varnamelen: varnamelen:
min-name-length: 2 min-name-length: 2
ignore-decls:
- w io.Writer
- t testing.T
- e error
- i int
- b bytes.Buffer
- h Handle
linters: linters:
enable-all: true enable-all: true
@@ -43,6 +50,8 @@ linters:
- ifshort - ifshort
- nosnakecase - nosnakecase
- ireturn # implement provider interface
issues: issues:
# Excluding configuration per-path, per-linter, per-text and per-source # Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules: exclude-rules:

View File

@@ -124,7 +124,6 @@ func (c *Client) Value(ctx context.Context, path ...string) (Value, error) {
} }
func (c *Client) Watch(ctx context.Context, callback WatchCallback, path ...string) error { func (c *Client) Watch(ctx context.Context, callback WatchCallback, path ...string) error {
for idx, prov := range c.providers { for idx, prov := range c.providers {
provider, ok := prov.(WatchProvider) provider, ok := prov.(WatchProvider)
if !ok { if !ok {

View File

@@ -1,304 +0,0 @@
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/etcd"
"gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/provider/vault"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/provider/yaml"
"gitoa.ru/go-4devs/config/test"
)
func ExampleClient_Value() {
const (
namespace = "fdevs"
appName = "config"
)
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"}
// configure etcd client
etcdClient, err := test.NewEtcd(ctx)
if err != nil {
log.Print(err)
return
}
// configure vault client
vaultClient, err := test.NewVault()
if err != nil {
log.Print(err)
return
}
// read json config
jsonConfig := test.ReadFile("config.json")
config, err := config.New(
arg.New(),
env.New(test.Namespace, test.AppName),
etcd.NewProvider(namespace, appName, etcdClient),
vault.NewSecretKV2(namespace, appName, vaultClient),
json.New(jsonConfig),
)
if err != nil {
log.Print(err)
return
}
dsn, err := config.Value(ctx, "example", "dsn")
if err != nil {
log.Print("example:dsn", err)
return
}
port, err := config.Value(ctx, "listen")
if err != nil {
log.Print("listen", err)
return
}
enabled, err := config.Value(ctx, "maintain")
if err != nil {
log.Print("maintain", err)
return
}
title, err := config.Value(ctx, "app.name.title")
if err != nil {
log.Print("app.name.title", err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print(err)
return
}
hostValue, err := config.Value(ctx, "host")
if err != nil {
log.Print(err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("dsn from vault: %s\n", dsn.String())
fmt.Printf("listen from env: %d\n", port.Int())
fmt.Printf("maintain from etcd: %v\n", enabled.Bool())
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("struct from json: %+v\n", cfg)
fmt.Printf("replace env host by args: %v\n", hostValue.String())
// Output:
// dsn from vault: pgsql://user@pass:127.0.0.1:5432
// listen from env: 8080
// maintain from etcd: true
// title from json: config title
// struct from json: {Duration:21m0s Enabled:true}
// replace env host by args: gitoa.ru
}
func ExampleClient_Watch() {
const (
namespace = "fdevs"
appName = "config"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// configure etcd client
etcdClient, err := test.NewEtcd(ctx)
if err != nil {
log.Print(err)
return
}
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")
_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "pgsql://user@pass:127.0.0.1:5432")
if err != nil {
log.Print(err)
return
}
defer func() {
cancel()
if _, err = etcdClient.KV.Delete(context.Background(), "fdevs/config/example_db_dsn"); err != nil {
log.Print(err)
return
}
}()
watcher, err := config.New(
watcher.New(time.Microsecond, env.New(test.Namespace, test.AppName)),
watcher.New(time.Microsecond, yaml.NewWatch("test/fixture/config.yaml")),
etcd.NewProvider(namespace, appName, etcdClient),
)
if err != nil {
log.Print(err)
return
}
wg := sync.WaitGroup{}
wg.Add(2)
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) {
fmt.Println("update example_enable old: ", oldVar.Bool(), " new:", newVar.Bool())
wg.Done()
}, "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) {
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
wg.Done()
}, "example_db_dsn")
if err != nil {
log.Print(err)
return
}
time.AfterFunc(time.Second, func() {
if _, err := etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "mysql://localhost:5432"); err != nil {
log.Print(err)
return
}
})
wg.Wait()
// Output:
// update example_enable old: true new: false
// update example_db_dsn old: pgsql://user@pass:127.0.0.1:5432 new: mysql://localhost:5432
}
func ExampleClient_Value_factory() {
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", "--config-json=config.json", "--config-yaml=test/fixture/config.yaml"}
config, err := config.New(
arg.New(),
env.New(test.Namespace, test.AppName),
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
val, err := cfg.Value(ctx, "config-json")
if err != nil {
return nil, fmt.Errorf("failed read config file:%w", err)
}
jsonConfig := test.ReadFile(val.String())
return json.New(jsonConfig), nil
}),
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
val, err := cfg.Value(ctx, "config-yaml")
if err != nil {
return nil, fmt.Errorf("failed read config file:%w", err)
}
provader, err := yaml.NewFile(val.String())
if err != nil {
return nil, fmt.Errorf("failed init by file %v:%w", val.String(), err)
}
return provader, nil
}),
)
if err != nil {
log.Print(err)
return
}
port, err := config.Value(ctx, "listen")
if err != nil {
log.Print(err)
return
}
title, err := config.Value(ctx, "app", "name", "title")
if err != nil {
log.Print(err)
return
}
yamlTitle, err := config.Value(ctx, "app", "title")
if err != nil {
log.Print(err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print(err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("listen from env: %d\n", port.Int())
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("yaml title: %v\n", yamlTitle.String())
fmt.Printf("struct from json: %+v\n", cfg)
// Output:
// listen from env: 8080
// title from json: config title
// yaml title: yaml title
// struct from json: {Duration:21m0s Enabled:true}
}

View File

@@ -9,6 +9,8 @@ import (
var ( var (
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
ErrAlreadyExist = errors.New("already exist") ErrAlreadyExist = errors.New("already exist")
ErrWrongType = errors.New("wrong type")
ErrWrongFormat = errors.New("wrong format")
) )
func FuncName(in string) string { func FuncName(in string) string {

View File

@@ -31,8 +31,9 @@ func (i Imports) Imports() []Import {
func (i *Imports) Short(fullType string) (string, error) { func (i *Imports) Short(fullType string) (string, error) {
idx := strings.LastIndexByte(fullType, '.') idx := strings.LastIndexByte(fullType, '.')
if idx == -1 { if idx == -1 {
return "", fmt.Errorf("unexpected") return "", fmt.Errorf("%w: expect package.Type", ErrWrongFormat)
} }
if alias, ok := i.data[fullType[:idx]]; ok { if alias, ok := i.data[fullType[:idx]]; ok {
return alias + fullType[idx:], nil return alias + fullType[idx:], nil
} }
@@ -43,13 +44,12 @@ func (i *Imports) Short(fullType string) (string, error) {
func (i *Imports) AddType(fullType string) (string, error) { func (i *Imports) AddType(fullType string) (string, error) {
idx := strings.LastIndexByte(fullType, '.') idx := strings.LastIndexByte(fullType, '.')
if idx == -1 { if idx == -1 {
return "", fmt.Errorf("unexpected") return "", fmt.Errorf("%w: expect pckage.Type", ErrWrongFormat)
} }
imp := i.Add(fullType[:idx]) imp := i.Add(fullType[:idx])
return imp.Alias + fullType[idx:], nil return imp.Alias + fullType[idx:], nil
} }
func (i *Imports) Adds(pkgs ...string) { func (i *Imports) Adds(pkgs ...string) {
@@ -60,10 +60,11 @@ func (i *Imports) Adds(pkgs ...string) {
func (i *Imports) Add(pkg string) Import { func (i *Imports) Add(pkg string) Import {
alias := pkg alias := pkg
idx := strings.LastIndexByte(pkg, '/')
if idx != -1 { if idx := strings.LastIndexByte(pkg, '/'); idx != -1 {
alias = pkg[idx+1:] alias = pkg[idx+1:]
} }
if al, ok := i.data[pkg]; ok { if al, ok := i.data[pkg]; ok {
return Import{Package: pkg, Alias: al} return Import{Package: pkg, Alias: al}
} }
@@ -73,6 +74,7 @@ func (i *Imports) Add(pkg string) Import {
alias += strconv.Itoa(len(i.data)) alias += strconv.Itoa(len(i.data))
} }
} }
i.data[pkg] = alias i.data[pkg] = alias
return Import{ return Import{

View File

@@ -18,6 +18,7 @@ func Run(w io.Writer, pkgName string, defs definition.Definition, viewOpt ViewOp
gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context") gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context")
var view bytes.Buffer var view bytes.Buffer
err := defs.View(func(o definition.Option) error { err := defs.View(func(o definition.Option) error {
return gen.Handle(&view, &gen, o) return gen.Handle(&view, &gen, o)
}) })

View File

@@ -2,9 +2,10 @@ package generate
import "text/template" import "text/template"
var tpl = template.Must(template.New("tpls").Parse(baseTemplate)) //nolint:gochecknoglobals
var (
var baseTemplate = `// Code generated gitoa.ru/go-4devs/config DO NOT EDIT. tpl = template.Must(template.New("tpls").Parse(baseTemplate))
baseTemplate = `// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
package {{.Pkg}} package {{.Pkg}}
import ( import (
@@ -39,3 +40,4 @@ type {{.StructName}} struct {
log func(context.Context, string, ...any) log func(context.Context, string, ...any)
} }
` `
)

View File

@@ -8,6 +8,7 @@ import (
"gitoa.ru/go-4devs/config/definition" "gitoa.ru/go-4devs/config/definition"
) )
//nolint:gochecknoglobals
var handlers = sync.Map{} var handlers = sync.Map{}
func Add(kind string, h Handle) error { func Add(kind string, h Handle) error {
@@ -17,18 +18,20 @@ func Add(kind string, h Handle) error {
} }
handlers.Store(kind, h) handlers.Store(kind, h)
return nil return nil
} }
//nolint:forcetypeassert
func get(kind string) Handle { func get(kind string) Handle {
h, ok := handlers.Load(kind) handler, ok := handlers.Load(kind)
if !ok { if !ok {
return func(w io.Writer, h Handler, o definition.Option) error { return func(w io.Writer, h Handler, o definition.Option) error {
return fmt.Errorf("handler by %v:%w", kind, ErrNotFound) return fmt.Errorf("handler by %v:%w", kind, ErrNotFound)
} }
} }
return h.(Handle) return handler.(Handle)
} }
func MustAdd(kind string, h Handle) { func MustAdd(kind string, h Handle) {
@@ -41,7 +44,7 @@ type Handle func(io.Writer, Handler, definition.Option) error
type Handler interface { type Handler interface {
StructName() string StructName() string
Handle(io.Writer, Handler, definition.Option) error Handle(w io.Writer, handler Handler, opt definition.Option) error
Options() ViewOption Options() ViewOption
Keys() []string Keys() []string
AddType(fullName string) (string, error) AddType(fullName string) (string, error)

5
definition/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module gitoa.ru/go-4devs/config/definition
go 1.21.5
require github.com/iancoleman/strcase v0.3.0

2
definition/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=

View File

@@ -9,6 +9,7 @@ import (
"gitoa.ru/go-4devs/config/definition/generate" "gitoa.ru/go-4devs/config/definition/generate"
) )
//nolint:gochecknoinits
func init() { func init() {
generate.MustAdd(Kind, handle) generate.MustAdd(Kind, handle)
} }
@@ -16,8 +17,9 @@ func init() {
func handle(w io.Writer, data generate.Handler, option definition.Option) error { func handle(w io.Writer, data generate.Handler, option definition.Option) error {
group, ok := option.(Group) group, ok := option.(Group)
if !ok { if !ok {
return fmt.Errorf("uexepected type:%T", option) return fmt.Errorf("%w:%T", generate.ErrWrongType, option)
} }
viewData := View{ viewData := View{
Group: group, Group: group,
ParentName: data.StructName(), ParentName: data.StructName(),
@@ -25,7 +27,6 @@ func handle(w io.Writer, data generate.Handler, option definition.Option) error
} }
err := tpl.Execute(w, viewData) err := tpl.Execute(w, viewData)
if err != nil { if err != nil {
return fmt.Errorf("render group:%w", err) return fmt.Errorf("render group:%w", err)
} }
@@ -54,8 +55,8 @@ func (c ChildData) StructName() string {
return c.structName return c.structName
} }
func (v ChildData) Keys() []string { func (c ChildData) Keys() []string {
return v.keys return c.keys
} }
type View struct { type View struct {
@@ -72,9 +73,10 @@ func (v View) StructName() string {
return generate.FuncName(v.Prefix + v.Name + v.Suffix) return generate.FuncName(v.Prefix + v.Name + v.Suffix)
} }
var tpl = template.Must(template.New("tpls").Parse(tplw)) //nolint:gochecknoglobals
var (
var tplw = `type {{.StructName}} struct { tpl = template.Must(template.New("tpls").Parse(gpoupTemplate))
gpoupTemplate = `type {{.StructName}} struct {
{{.ParentName}} {{.ParentName}}
} }
@@ -83,3 +85,4 @@ func (i {{.ParentName}}) {{.FuncName}}() {{.StructName}} {
return {{.StructName}}{i} return {{.StructName}}{i}
} }
` `
)

View File

@@ -19,8 +19,10 @@ import (
//go:embed tpl/* //go:embed tpl/*
var tpls embed.FS var tpls embed.FS
//nolint:gochecknoglobals
var tpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tmpl")) var tpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tmpl"))
//nolint:gochecknoinits
func init() { func init() {
generate.MustAdd(Kind, Handle(tpl.Lookup("option.tmpl"))) generate.MustAdd(Kind, Handle(tpl.Lookup("option.tmpl")))
} }
@@ -47,7 +49,9 @@ func (v View) Context() bool {
func (v View) FuncName() string { func (v View) FuncName() string {
if funcName, ok := v.Option.Params.Get(ViewParamFunctName); ok { if funcName, ok := v.Option.Params.Get(ViewParamFunctName); ok {
return funcName.(string) name, _ := funcName.(string)
return name
} }
return generate.FuncName(v.Name) return generate.FuncName(v.Name)
@@ -55,7 +59,9 @@ func (v View) FuncName() string {
func (v View) Description() string { func (v View) Description() string {
if desc, ok := v.Option.Params.Get(ViewParamDescription); ok { if desc, ok := v.Option.Params.Get(ViewParamDescription); ok {
return desc.(string) description, _ := desc.(string)
return description
} }
return v.Option.Description return v.Option.Description
@@ -86,12 +92,14 @@ func (v View) ParentKeys() string {
func (v View) Type() string { func (v View) Type() string {
slice := "" slice := ""
if vtype, ok := v.Option.Type.(string); ok { if vtype, ok := v.Option.Type.(string); ok {
if strings.Contains(vtype, ".") { if strings.Contains(vtype, ".") {
if name, err := v.AddType(vtype); err == nil { if name, err := v.AddType(vtype); err == nil {
return slice + name return slice + name
} }
} }
return vtype return vtype
} }
@@ -118,7 +126,6 @@ func (v View) FuncType() string {
} }
func (v View) Parse(valName string, value string, keys []string) string { func (v View) Parse(valName string, value string, keys []string) string {
h := parser(v.Option.Type) h := parser(v.Option.Type)
data, err := h(ParseData{ data, err := h(ParseData{
@@ -134,13 +141,14 @@ func (v View) Parse(valName string, value string, keys []string) string {
return data return data
} }
//nolint:gochecknoglobals,unparam
var parses = map[string]func(data ParseData) (string, error){ var parses = map[string]func(data ParseData) (string, error){
typesIntreface[0].Name(): func(data ParseData) (string, error) { typesIntreface[0].Name(): func(data ParseData) (string, error) {
var b bytes.Buffer var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "unmarshal_text.tmpl", data) err := tpl.ExecuteTemplate(&b, "unmarshal_text.tmpl", data)
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("execute unmarshal text:%w", err)
} }
return b.String(), nil return b.String(), nil
@@ -150,7 +158,7 @@ var parses = map[string]func(data ParseData) (string, error){
err := tpl.ExecuteTemplate(&b, "unmarshal_json.tmpl", data) err := tpl.ExecuteTemplate(&b, "unmarshal_json.tmpl", data)
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("execute unmarshal json:%w", err)
} }
return b.String(), nil return b.String(), nil
@@ -175,24 +183,25 @@ var parses = map[string]func(data ParseData) (string, error){
func internal(data ParseData) (string, error) { func internal(data ParseData) (string, error) {
var b bytes.Buffer var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "parse.tmpl", data) err := tpl.ExecuteTemplate(&b, "parse.tmpl", data)
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("execute parse.tmpl:%w", err)
} }
return b.String(), nil return b.String(), nil
} }
var ( //nolint:gochecknoglobals
typesIntreface = [...]reflect.Type{ var typesIntreface = [...]reflect.Type{
reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(), reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(),
reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(), reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(),
} }
)
func parser(data any) func(ParseData) (string, error) { func parser(data any) func(ParseData) (string, error) {
vtype := reflect.TypeOf(data) vtype := reflect.TypeOf(data)
name := vtype.Name() name := vtype.Name()
if v, ok := data.(string); ok { if v, ok := data.(string); ok {
name = v name = v
} }

View File

@@ -6,7 +6,7 @@ import (
const Kind = "proto" const Kind = "proto"
func New(name, desc string, opt definition.Option, opts ...func(*Proto)) Proto { func New(name, desc string, opt definition.Option) Proto {
pr := Proto{ pr := Proto{
Name: name, Name: name,
Description: desc, Description: desc,

View File

@@ -11,6 +11,7 @@ import (
"gitoa.ru/go-4devs/config/definition/option" "gitoa.ru/go-4devs/config/definition/option"
) )
//nolint:gochecknoinits
func init() { func init() {
generate.MustAdd(Kind, handle) generate.MustAdd(Kind, handle)
} }
@@ -18,8 +19,9 @@ func init() {
func handle(w io.Writer, data generate.Handler, opt definition.Option) error { func handle(w io.Writer, data generate.Handler, opt definition.Option) error {
proto, ok := opt.(Proto) proto, ok := opt.(Proto)
if !ok { if !ok {
return fmt.Errorf("uexepected type:%T", opt) return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
} }
if viewOpt, ok := proto.Option.(option.Option); ok { if viewOpt, ok := proto.Option.(option.Option); ok {
viewOpt = viewOpt.WithParams( viewOpt = viewOpt.WithParams(
definition.Param{ definition.Param{
@@ -35,12 +37,13 @@ func handle(w io.Writer, data generate.Handler, opt definition.Option) error {
return option.Handle(tpl)(w, data, viewOpt) return option.Handle(tpl)(w, data, viewOpt)
} }
return fmt.Errorf("not support option type") return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
} }
var tpl = template.Must(template.New("tpls").Funcs(template.FuncMap{"join": strings.Join}).Parse(templateOption)) //nolint:gochecknoglobals
var (
var templateOption = `// read{{.FuncName}} {{.Description}}. tpl = template.Must(template.New("tpls").Funcs(template.FuncMap{"join": strings.Join}).Parse(templateOption))
templateOption = `// read{{.FuncName}} {{.Description}}.
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context, key string) (v {{.Type}},e error) { func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context, key string) (v {{.Type}},e error) {
val, err := i.Value(ctx, {{ .ParentKeys }} key, "{{.Name}}") val, err := i.Value(ctx, {{ .ParentKeys }} key, "{{.Name}}")
if err != nil { if err != nil {
@@ -74,3 +77,4 @@ func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context, {{en
return val return val
} }
` `
)

View File

@@ -7,4 +7,5 @@ var (
ErrInvalidValue = errors.New("invalid value") ErrInvalidValue = errors.New("invalid value")
ErrUnknowType = errors.New("unknow type") ErrUnknowType = errors.New("unknow type")
ErrInitFactory = errors.New("init factory") ErrInitFactory = errors.New("init factory")
ErrStopWatch = errors.New("stop watch")
) )

51
go.mod
View File

@@ -1,52 +1,3 @@
module gitoa.ru/go-4devs/config module gitoa.ru/go-4devs/config
go 1.18 go 1.21
require (
github.com/hashicorp/vault/api v1.1.0
github.com/iancoleman/strcase v0.3.0
github.com/pelletier/go-toml v1.9.0
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.5
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0
gopkg.in/ini.v1 v1.62.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.3.5 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 // indirect
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.0 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 // indirect
google.golang.org/grpc v1.32.0 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
)

320
go.sum
View File

@@ -1,320 +0,0 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/go-metrics v0.3.0/go.mod h1:zXjbSimjXTd7vOpY8B0/2LpvNvDoXBuplAD+gJD3GYs=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.25.37/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.1.0 h1:kq/SbG2BCKLkDKkjQf5OWwKWUKj1lgs3lFI4PxnR5lg=
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-ldap/ldap/v3 v3.1.3/go.mod h1:3rbOH3jRS2u6jg2rJnKAMLE/xQyCKIveG2Sa/Cohzb8=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.12.0 h1:d4QkX8FRTYaKaCZBoXYY8zJX2BXjWxurN/GA2tkrmZM=
github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-kms-wrapping/entropy v0.1.0/go.mod h1:d1g9WGtAunDNpek8jUIEJnBlbgKS1N2Q61QkHiZyR1g=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/vault/api v1.1.0 h1:QcxC7FuqEl0sZaIjcXB/kNEeBa0DH5z57qbWBvZwLC4=
github.com/hashicorp/vault/api v1.1.0/go.mod h1:R3Umvhlxi2TN7Ex2hzOowyeNb+SfbVWI973N+ctaFMk=
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267 h1:e1ok06zGrWJW91rzRroyl5nRNqraaBe4d5hiKcVZuHM=
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.0 h1:NOd0BRdOKpPf0SxkL3HxSQOG7rNh+4kl6PHcBPFs7Q0=
github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc=
github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0 h1:+e5nrluATIy3GP53znpkHMFzPTHGYyzvJGFCbuI6ZLc=
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0/go.mod h1:mPcW6aZJukV6Aa81LSKpBjQXTWlXB5r74ymPoSWa3Sw=
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0 h1:dr1EOILak2pu4Nf5XbRIOCNIBjcz6UmkQd7hHRXwxaM=
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0/go.mod h1:wKt7jgDgf/OfKiYmCq5WFGxOFAkVMLxiiXgLDFhECr8=
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0 h1:3yLUEC0nFCxw/RArImOyRUI4OAFbg4PFpBbAhSNzKNY=
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0/go.mod h1:tV31atvwzcybuqejDoY3oaNRTtlD2l/Ot78Pc9w7DMY=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.32.0 h1:zWTV+LMdc3kaiJMSTOFz2UgSBgx8RNQoTGiZu3fR9S0=
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/square/go-jose.v2 v2.5.1 h1:7odma5RETjNHWJnR32wx8t+Io4djHE1PqxCFx3iiZ2w=
gopkg.in/square/go-jose.v2 v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

15
key.go
View File

@@ -1,15 +0,0 @@
package config
import "context"
type Key struct {
Name string
AppName string
Namespace string
}
type KeyFactory func(ctx context.Context, key Key) string
func (k Key) String() string {
return k.Namespace + "_" + k.AppName + "_" + k.Name
}

View File

@@ -1,32 +0,0 @@
package key
import (
"context"
"strings"
"gitoa.ru/go-4devs/config"
)
func LastIndex(sep string, factory config.KeyFactory) func(ctx context.Context, key config.Key) (string, string) {
return func(ctx context.Context, key config.Key) (string, string) {
name := factory(ctx, key)
idx := strings.LastIndex(name, sep)
if idx == -1 {
return name, ""
}
return name[0:idx], name[idx+len(sep):]
}
}
func LastIndexField(sep, def string, factory config.KeyFactory) func(ctx context.Context, key config.Key) (string, string) {
return func(ctx context.Context, key config.Key) (string, string) {
p, k := LastIndex(sep, factory)(ctx, key)
if k == "" {
return p, def
}
return p, k
}
}

View File

@@ -1,64 +0,0 @@
package key_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
)
func TestLastIndex(t *testing.T) {
t.Parallel()
ctx := context.Background()
cases := map[string]struct {
sep string
path string
field string
}{
"/secret/with/field/name": {
sep: "/",
path: "/secret/with/field",
field: "name",
},
"/secret/database:username": {
sep: ":",
path: "/secret/database",
field: "username",
},
"database:username": {
sep: ":",
path: "database",
field: "username",
},
"/secret/database-dsn": {
sep: ":",
path: "/secret/database-dsn",
field: "",
},
"/secret/database--dsn": {
sep: "--",
path: "/secret/database",
field: "dsn",
},
"/secret/database:dsn": {
sep: "--",
path: "/secret/database:dsn",
field: "",
},
}
for path, data := range cases {
k := config.Key{
Name: path,
}
fn := key.LastIndex(data.sep, key.Name)
ns, field := fn(ctx, k)
assert.Equal(t, data.field, field, k)
assert.Equal(t, data.path, ns, k)
}
}

View File

@@ -1,46 +0,0 @@
package key
import (
"context"
"strings"
"gitoa.ru/go-4devs/config"
)
func NsAppName(sep string) config.KeyFactory {
return func(_ context.Context, key config.Key) string {
return strings.Join([]string{key.Namespace, key.AppName, key.Name}, sep)
}
}
func AppName(sep string) config.KeyFactory {
return func(_ context.Context, key config.Key) string {
return strings.Join([]string{key.AppName, key.Name}, sep)
}
}
func PrefixName(prefix string, factory config.KeyFactory) config.KeyFactory {
return func(ctx context.Context, key config.Key) string {
return prefix + factory(ctx, key)
}
}
func Name(_ context.Context, key config.Key) string {
return key.Name
}
func AliasName(name string, alias string, def config.KeyFactory) config.KeyFactory {
return func(ctx context.Context, key config.Key) string {
if name == key.Name {
return alias
}
return def(ctx, key)
}
}
func ReplaceAll(oldVal, newVal string, parent config.KeyFactory) config.KeyFactory {
return func(ctx context.Context, key config.Key) string {
return strings.ReplaceAll(parent(ctx, key), oldVal, newVal)
}
}

View File

@@ -11,7 +11,7 @@ type NamedProvider interface {
Provider Provider
} }
type WatchCallback func(ctx context.Context, oldVar, newVar Value) type WatchCallback func(ctx context.Context, oldVar, newVar Value) error
type WatchProvider interface { type WatchProvider interface {
Watch(ctx context.Context, callback WatchCallback, path ...string) error Watch(ctx context.Context, callback WatchCallback, path ...string) error

View File

@@ -2,13 +2,13 @@ package arg
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value" "gitoa.ru/go-4devs/config/value"
"gopkg.in/yaml.v3"
) )
const Name = "arg" const Name = "arg"
@@ -106,7 +106,7 @@ func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
if err := p.parse(); err != nil { if err := p.parse(); err != nil {
return nil, err return nil, err
} }
@@ -117,13 +117,18 @@ func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, err
case len(val) == 1: case len(val) == 1:
return value.JString(val[0]), nil return value.JString(val[0]), nil
default: default:
var yNode yaml.Node data, jerr := json.Marshal(val)
if jerr != nil {
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &yNode); err != nil { return nil, fmt.Errorf("failed load data:%w", jerr)
return nil, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
} }
return value.Decode(yNode.Decode), nil return value.Decode(func(v interface{}) error {
if err := json.Unmarshal(data, v); err != nil {
return fmt.Errorf("unmarshal:%w", err)
}
return nil
}), nil
} }
} }

View File

@@ -1,7 +1,9 @@
package arg_test package arg_test
import ( import (
"fmt"
"os" "os"
"strings"
"testing" "testing"
"time" "time"
@@ -35,7 +37,7 @@ func TestProvider(t *testing.T) {
test.NewRead("config.hcl", "config"), test.NewRead("config.hcl", "config"),
test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"), test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"),
test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"), test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"),
test.NewReadUnmarshal(&[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}, "timeout"), test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"),
test.NewReadUnmarshal(&[]time.Time{ test.NewReadUnmarshal(&[]time.Time{
test.Time("2009-01-02T15:04:05Z"), test.Time("2009-01-02T15:04:05Z"),
test.Time("2008-01-02T15:04:05+03:00"), test.Time("2008-01-02T15:04:05+03:00"),
@@ -46,3 +48,22 @@ func TestProvider(t *testing.T) {
test.Run(t, prov, read) test.Run(t, prov, read)
} }
type Duration struct {
time.Duration
}
func (d *Duration) UnmarshalJSON(in []byte) error {
o, err := time.ParseDuration(strings.Trim(string(in), `"`))
if err != nil {
return fmt.Errorf("parse:%w", err)
}
d.Duration = o
return nil
}
func (d *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", d)), nil
}

View File

@@ -45,7 +45,7 @@ func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
name := p.prefix + p.key(path...) name := p.prefix + p.key(path...)
if val, ok := os.LookupEnv(name); ok { if val, ok := os.LookupEnv(name); ok {
return value.JString(val), nil return value.JString(val), nil

View File

@@ -1,4 +1,4 @@
package test package etcd_test
import ( import (
"context" "context"
@@ -8,6 +8,8 @@ import (
client "go.etcd.io/etcd/client/v3" client "go.etcd.io/etcd/client/v3"
) )
const ConfigJSON = `{"duration":1260000000000,"enabled":true}`
func NewEtcd(ctx context.Context) (*client.Client, error) { func NewEtcd(ctx context.Context) (*client.Client, error) {
dsn, ok := os.LookupEnv("FDEVS_CONFIG_ETCD_HOST") dsn, ok := os.LookupEnv("FDEVS_CONFIG_ETCD_HOST")
if !ok { if !ok {

32
provider/etcd/go.mod Normal file
View File

@@ -0,0 +1,32 @@
module gitoa.ru/go-4devs/config/provider/etcd
go 1.21
require (
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.2
go.etcd.io/etcd/api/v3 v3.5.11
go.etcd.io/etcd/client/v3 v3.5.11
)
require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

97
provider/etcd/go.sum Normal file
View File

@@ -0,0 +1,97 @@
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
go.etcd.io/etcd/api/v3 v3.5.11 h1:B54KwXbWDHyD3XYAwprxNzTe7vlhR69LuBgZnMVvS7E=
go.etcd.io/etcd/api/v3 v3.5.11/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4=
go.etcd.io/etcd/client/pkg/v3 v3.5.11 h1:bT2xVspdiCj2910T0V+/KHcVKjkUrCZVtk8J2JF2z1A=
go.etcd.io/etcd/client/pkg/v3 v3.5.11/go.mod h1:seTzl2d9APP8R5Y2hFL3NVlD6qC/dOT+3kvrqPyTas4=
go.etcd.io/etcd/client/v3 v3.5.11 h1:ajWtgoNSZJ1gmS8k+icvPtqsqEav+iUorF7b0qozgUU=
go.etcd.io/etcd/client/v3 v3.5.11/go.mod h1:a6xQUEqFJ8vztO1agJh/KQKOMfFI8og52ZconzcDJwE=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -26,8 +26,8 @@ type Client interface {
client.Watcher client.Watcher
} }
func NewProvider(namespace, appName string, client Client) *Provider { func New(namespace, appName string, client Client) *Provider {
p := Provider{ prov := Provider{
client: client, client: client,
key: func(s ...string) string { key: func(s ...string) string {
return strings.Join(s, Separator) return strings.Join(s, Separator)
@@ -36,7 +36,7 @@ func NewProvider(namespace, appName string, client Client) *Provider {
prefix: namespace + Separator + appName, prefix: namespace + Separator + appName,
} }
return &p return &prov
} }
type Provider struct { type Provider struct {
@@ -44,11 +44,13 @@ type Provider struct {
key func(...string) string key func(...string) string
name string name string
prefix string prefix string
log func(context.Context, string, ...any)
} }
func (p *Provider) Name() string { func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Key(s []string) string { func (p *Provider) Key(s []string) string {
return p.prefix + Separator + p.key(s...) return p.prefix + Separator + p.key(s...)
} }
@@ -77,7 +79,10 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, pat
if len(kvs) > 0 { if len(kvs) > 0 {
newVar, _ := p.resolve(key, kvs) newVar, _ := p.resolve(key, kvs)
oldVar, _ := p.resolve(key, olds) oldVar, _ := p.resolve(key, olds)
callback(ctx, oldVar, newVar)
if err := callback(ctx, oldVar, newVar); err != nil {
p.log(ctx, "watch callback[%v] %v:%v", p.Name(), path, err)
}
} }
} }
}(ctx, p.Key(path), callback) }(ctx, p.Key(path), callback)

View File

@@ -0,0 +1,121 @@
package etcd_test
import (
"context"
"fmt"
"log"
"sync"
"time"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/etcd"
)
func ExampleClient_Value() {
const (
namespace = "fdevs"
appName = "config"
)
ctx := context.Background()
// configure etcd client
etcdClient, err := NewEtcd(ctx)
if err != nil {
log.Print(err)
return
}
config, err := config.New(
etcd.New(namespace, appName, etcdClient),
)
if err != nil {
log.Print(err)
return
}
enabled, err := config.Value(ctx, "maintain")
if err != nil {
log.Print("maintain ", err)
return
}
fmt.Printf("maintain from etcd: %v\n", enabled.Bool())
// Output:
// maintain from etcd: true
}
func ExampleClient_Watch() {
const (
namespace = "fdevs"
appName = "config"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// configure etcd client
etcdClient, err := NewEtcd(ctx)
if err != nil {
log.Print(err)
return
}
_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "pgsql://user@pass:127.0.0.1:5432")
if err != nil {
log.Print(err)
return
}
defer func() {
cancel()
if _, err = etcdClient.KV.Delete(context.Background(), "fdevs/config/example_db_dsn"); err != nil {
log.Print(err)
return
}
}()
watcher, err := config.New(
etcd.New(namespace, appName, etcdClient),
)
if err != nil {
log.Print(err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
err = watcher.Watch(ctx, func(_ 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
}
time.AfterFunc(time.Second, func() {
if _, err := etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "mysql://localhost:5432"); err != nil {
log.Print(err)
return
}
})
wg.Wait()
// Output:
// update example_db_dsn old: pgsql://user@pass:127.0.0.1:5432 new: mysql://localhost:5432
}

View File

@@ -9,10 +9,10 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/etcd" "gitoa.ru/go-4devs/config/provider/etcd"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
) )
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
@@ -20,10 +20,10 @@ func TestProvider(t *testing.T) {
ctx := context.Background() ctx := context.Background()
et, err := test.NewEtcd(ctx) et, err := NewEtcd(ctx)
require.NoError(t, err) require.NoError(t, err)
provider := etcd.NewProvider("fdevs", "config", et) provider := etcd.New("fdevs", "config", et)
read := []test.Read{ read := []test.Read{
test.NewRead(test.DSN, "db_dsn"), test.NewRead(test.DSN, "db_dsn"),
test.NewRead(12*time.Minute, "duration"), test.NewRead(12*time.Minute, "duration"),
@@ -51,7 +51,7 @@ func TestWatcher(t *testing.T) {
key := "test_watch" key := "test_watch"
et, err := test.NewEtcd(ctx) et, err := NewEtcd(ctx)
require.NoError(t, err) require.NoError(t, err)
defer func() { defer func() {
@@ -61,12 +61,12 @@ func TestWatcher(t *testing.T) {
var cnt, cnt2 int32 var cnt, cnt2 int32
prov := etcd.NewProvider("fdevs", "config", et) prov := etcd.New("fdevs", "config", et)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(6) wg.Add(6)
watch := func(cnt *int32) func(ctx context.Context, oldVar, newVar config.Value) { watch := func(cnt *int32) config.WatchCallback {
return func(ctx context.Context, oldVar, newVar config.Value) { return func(ctx context.Context, oldVar, newVar config.Value) error {
switch *cnt { switch *cnt {
case 0: case 0:
assert.Equal(t, value(*cnt), newVar.String()) assert.Equal(t, value(*cnt), newVar.String())
@@ -76,15 +76,18 @@ func TestWatcher(t *testing.T) {
assert.Equal(t, value(*cnt-1), oldVar.String()) assert.Equal(t, value(*cnt-1), oldVar.String())
case 2: case 2:
_, perr := newVar.ParseString() _, perr := newVar.ParseString()
assert.NoError(t, perr) require.NoError(t, perr)
assert.Equal(t, "", newVar.String()) assert.Equal(t, "", newVar.String())
assert.Equal(t, value(*cnt-1), oldVar.String()) assert.Equal(t, value(*cnt-1), oldVar.String())
default: default:
assert.Fail(t, "unexpected watch") t.Error("unexpected watch")
t.Fail()
} }
wg.Done() wg.Done()
atomic.AddInt32(cnt, 1) atomic.AddInt32(cnt, 1)
return nil
} }
} }

15
provider/ini/go.mod Normal file
View File

@@ -0,0 +1,15 @@
module gitoa.ru/go-4devs/config/provider/ini
go 1.21
require (
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.1
gopkg.in/ini.v1 v1.67.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

14
provider/ini/go.sum Normal file
View File

@@ -0,0 +1,14 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -41,7 +41,7 @@ func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
section, name := p.resolve(path) section, name := p.resolve(path)
iniSection, err := p.data.GetSection(section) iniSection, err := p.data.GetSection(section)

View File

@@ -1,17 +1,27 @@
package ini_test package ini_test
import ( import (
"embed"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/ini" "gitoa.ru/go-4devs/config/provider/ini"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
lib "gopkg.in/ini.v1"
) )
//go:embed fixture/*
var fixtures embed.FS
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
t.Parallel() t.Parallel()
file := test.NewINI() data, derr := fixtures.ReadFile("fixture/config.ini")
require.NoError(t, derr)
file, err := lib.Load(data)
require.NoError(t, err)
read := []test.Read{ read := []test.Read{
test.NewRead("To Do, In Progress, Done", "project", "PROJECT_BOARD_BASIC_KANBAN_TYPE"), test.NewRead("To Do, In Progress, Done", "project", "PROJECT_BOARD_BASIC_KANBAN_TYPE"),

13
provider/json/go.mod Normal file
View File

@@ -0,0 +1,13 @@
module gitoa.ru/go-4devs/config/provider/json
go 1.21
require (
github.com/tidwall/gjson v1.17.0
gitoa.ru/go-4devs/config v0.0.1
)
require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

10
provider/json/go.sum Normal file
View File

@@ -0,0 +1,10 @@
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a h1:61iwpn1Ec4yIkBSvSz8knNENJuhj6v2rp6bfw1wkG0E=
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a/go.mod h1:3g2bwE2OTDyYwm33KN/Cqc8pEdGlWXnB8Ju3PsYNQr0=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=

View File

@@ -55,7 +55,7 @@ func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
key := p.key(path...) key := p.key(path...)
if val := gjson.GetBytes(p.data, key); val.Exists() { if val := gjson.GetBytes(p.data, key); val.Exists() {
return value.JString(val.String()), nil return value.JString(val.String()), nil

View File

@@ -0,0 +1,55 @@
package json_test
import (
"context"
"fmt"
"log"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/test"
)
func ExampleClient_Value() {
ctx := context.Background()
// read json config
jsonConfig, jerr := fixture.ReadFile("fixture/config.json")
if jerr != nil {
log.Printf("failed load file:%v", jerr)
return
}
config, err := config.New(
json.New(jsonConfig),
)
if err != nil {
log.Print(err)
return
}
title, err := config.Value(ctx, "app.name.title")
if err != nil {
log.Print("app.name.title", err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print("cfg ", err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("struct from json: %+v\n", cfg)
// Output:
// title from json: config title
// struct from json: {Duration:21m0s Enabled:true}
}

View File

@@ -1,19 +1,25 @@
package json_test package json_test
import ( import (
"embed"
"testing" "testing"
"time" "time"
provider "gitoa.ru/go-4devs/config/provider/json" "gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
) )
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
t.Parallel() t.Parallel()
js := test.ReadFile("config.json") js, err := fixture.ReadFile("fixture/config.json")
require.NoError(t, err)
prov := provider.New(js) prov := json.New(js)
sl := []string{} sl := []string{}
read := []test.Read{ read := []test.Read{
test.NewRead("config title", "app.name.title"), test.NewRead("config title", "app.name.title"),

15
provider/toml/go.mod Normal file
View File

@@ -0,0 +1,15 @@
module gitoa.ru/go-4devs/config/provider/toml
go 1.21
require (
github.com/pelletier/go-toml v1.9.5
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

14
provider/toml/go.sum Normal file
View File

@@ -0,0 +1,14 @@
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/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -62,7 +62,7 @@ func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
if k := p.key(path); p.tree.Has(k) { if k := p.key(path); p.tree.Has(k) {
return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil
} }

View File

@@ -1,17 +1,24 @@
package toml_test package toml_test
import ( import (
"embed"
"testing" "testing"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/toml" "gitoa.ru/go-4devs/config/provider/toml"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
) )
//go:embed fixture/*
var fixtures embed.FS
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
t.Parallel() t.Parallel()
prov, err := toml.NewFile(test.FixturePath("config.toml")) files, ferr := fixtures.ReadFile("fixture/config.toml")
require.NoError(t, ferr)
prov, err := toml.New(files)
require.NoError(t, err) require.NoError(t, err)
m := []int{} m := []int{}

29
provider/vault/go.mod Normal file
View File

@@ -0,0 +1,29 @@
module gitoa.ru/go-4devs/config/provider/vault
go 1.21
require (
github.com/hashicorp/vault/api v1.11.0
gitoa.ru/go-4devs/config v0.0.2
)
require (
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
)

92
provider/vault/go.sum Normal file
View File

@@ -0,0 +1,92 @@
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
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/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs=
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 h1:om4Al8Oy7kCm/B86rLCLah4Dt5Aa0Fr5rYBG60OzwHQ=
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.1/go.mod h1:gKOamz3EwoIoJq7mlMIRBpVTAUn8qPCrEclOKKWhD3U=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/vault/api v1.11.0 h1:AChWByeHf4/P9sX3Y1B7vFsQhZO2BgQiCMQ2SA1P1UY=
github.com/hashicorp/vault/api v1.11.0/go.mod h1:si+lJCYO7oGkIoNPAN8j3azBLTn9SjMGS+jFaHd1Cck=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
@@ -19,16 +18,16 @@ const (
ValueName = "value" ValueName = "value"
) )
var _ config.Provider = (*SecretKV2)(nil) var _ config.Provider = (*Provider)(nil)
type SecretOption func(*SecretKV2) type SecretOption func(*Provider)
func WithSecretResolve(f func(key []string) (string, string)) SecretOption { func WithSecretResolve(f func(key []string) (string, string)) SecretOption {
return func(s *SecretKV2) { s.resolve = f } return func(s *Provider) { s.resolve = f }
} }
func NewSecretKV2(namespace, appName string, client *api.Client, opts ...SecretOption) *SecretKV2 { func New(namespace, appName string, client *api.Client, opts ...SecretOption) *Provider {
prov := SecretKV2{ prov := Provider{
client: client, client: client,
resolve: func(key []string) (string, string) { resolve: func(key []string) (string, string) {
keysLen := len(key) keysLen := len(key)
@@ -49,17 +48,18 @@ func NewSecretKV2(namespace, appName string, client *api.Client, opts ...SecretO
return &prov return &prov
} }
type SecretKV2 struct { type Provider struct {
client *api.Client client *api.Client
resolve func(key []string) (string, string) resolve func(key []string) (string, string)
name string name string
prefix string prefix string
} }
func (p *SecretKV2) Name() string { func (p *Provider) Name() string {
return p.name return p.name
} }
func (p *SecretKV2) Key(in []string) (string, string) {
func (p *Provider) Key(in []string) (string, string) {
path, val := p.resolve(in) path, val := p.resolve(in)
if path == "" { if path == "" {
return p.prefix, val return p.prefix, val
@@ -67,19 +67,21 @@ func (p *SecretKV2) Key(in []string) (string, string) {
return p.prefix + Separator + path, val return p.prefix + Separator + path, val
} }
func (p *SecretKV2) read(path, key string) (*api.Secret, error) {
func (p *Provider) read(path, key string) (*api.Secret, error) {
secret, err := p.client.Logical().Read(path) secret, err := p.client.Logical().Read(path)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("read[%s:%s]:%w", path, key, err)
} }
if secret == nil && key != ValueName { if secret == nil && key != ValueName {
return p.read(path+Separator+key, ValueName) return p.read(path+Separator+key, ValueName)
} }
return secret, err return secret, nil
} }
func (p *SecretKV2) Value(ctx context.Context, key ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, key ...string) (config.Value, error) {
path, field := p.Key(key) path, field := p.Key(key)
secret, err := p.read(path, field) secret, err := p.read(path, field)
@@ -88,7 +90,6 @@ func (p *SecretKV2) Value(ctx context.Context, key ...string) (config.Value, err
} }
if secret == nil || len(secret.Data) == 0 { if secret == nil || len(secret.Data) == 0 {
log.Println(secret == nil)
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name()) return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
} }

View File

@@ -0,0 +1,47 @@
package vault_test
import (
"context"
"fmt"
"log"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/vault"
)
func ExampleClient_Value() {
const (
namespace = "fdevs"
appName = "config"
)
ctx := context.Background()
// configure vault client
vaultClient, err := NewVault()
if err != nil {
log.Print(err)
return
}
config, err := config.New(
vault.New(namespace, appName, vaultClient),
)
if err != nil {
log.Print(err)
return
}
dsn, err := config.Value(ctx, "example", "dsn")
if err != nil {
log.Print("example:dsn ", err)
return
}
fmt.Printf("dsn from vault: %s\n", dsn.String())
// Output:
// dsn from vault: pgsql://user@pass:127.0.0.1:5432
}

View File

@@ -4,18 +4,18 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/vault" "gitoa.ru/go-4devs/config/provider/vault"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
) )
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
t.Parallel() t.Parallel()
cl, err := test.NewVault() cl, err := NewVault()
require.NoError(t, err) require.NoError(t, err)
provider := vault.NewSecretKV2("fdevs", "config", cl) provider := vault.New("fdevs", "config", cl)
read := []test.Read{ read := []test.Read{
test.NewReadConfig("database"), test.NewReadConfig("database"),

View File

@@ -1,4 +1,4 @@
package test package vault_test
import ( import (
"bytes" "bytes"
@@ -8,6 +8,7 @@ import (
"os" "os"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
"gitoa.ru/go-4devs/config/test"
) )
const token = "dev" const token = "dev"
@@ -38,11 +39,11 @@ func NewVault() (*api.Client, error) {
"enabled": true, "enabled": true,
}, },
"db": { "db": {
"dsn": DSN, "dsn": test.DSN,
"timeout": "60s", "timeout": "60s",
}, },
"example": { "example": {
"dsn": DSN, "dsn": test.DSN,
"timeout": "60s", "timeout": "60s",
}, },
} }

View File

@@ -2,8 +2,9 @@ package watcher
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log" "log/slog"
"time" "time"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
@@ -14,13 +15,11 @@ var (
_ config.WatchProvider = (*Provider)(nil) _ config.WatchProvider = (*Provider)(nil)
) )
func New(duration time.Duration, provider config.NamedProvider, opts ...Option) *Provider { func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
prov := &Provider{ prov := &Provider{
NamedProvider: provider, Provider: provider,
ticker: time.NewTicker(duration), duration: duration,
logger: func(_ context.Context, msg string) { logger: slog.ErrorContext,
log.Print(msg)
},
} }
for _, opt := range opts { for _, opt := range opts {
@@ -30,7 +29,7 @@ func New(duration time.Duration, provider config.NamedProvider, opts ...Option)
return prov return prov
} }
func WithLogger(l func(context.Context, string)) Option { func WithLogger(l func(context.Context, string, ...any)) Option {
return func(p *Provider) { return func(p *Provider) {
p.logger = l p.logger = l
} }
@@ -39,33 +38,43 @@ func WithLogger(l func(context.Context, string)) Option {
type Option func(*Provider) type Option func(*Provider)
type Provider struct { type Provider struct {
config.NamedProvider config.Provider
ticker *time.Ticker duration time.Duration
logger func(context.Context, string) logger func(context.Context, string, ...any)
} }
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error { func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
oldVar, err := p.NamedProvider.Value(ctx, key...) old, err := p.Provider.Value(ctx, key...)
if err != nil { if err != nil {
return fmt.Errorf("failed watch variable: %w", err) return fmt.Errorf("failed watch variable: %w", err)
} }
go func() { go func(oldVar config.Value) {
ticker := time.NewTicker(p.duration)
defer func() {
ticker.Stop()
}()
for { for {
select { select {
case <-p.ticker.C: case <-ticker.C:
newVar, err := p.NamedProvider.Value(ctx, key...) newVar, err := p.Provider.Value(ctx, key...)
if err != nil { if err != nil {
p.logger(ctx, err.Error()) p.logger(ctx, "get value%v:%v", key, err.Error())
} else if !newVar.IsEquals(oldVar) { } else if !newVar.IsEquals(oldVar) {
callback(ctx, oldVar, newVar) if err := callback(ctx, oldVar, newVar); err != nil {
if errors.Is(err, config.ErrStopWatch) {
return
}
p.logger(ctx, "callback %v:%v", key, err)
}
oldVar = newVar oldVar = newVar
} }
case <-ctx.Done(): case <-ctx.Done():
return return
} }
} }
}() }(old)
return nil return nil
} }

View File

@@ -2,18 +2,20 @@ package watcher_test
import ( import (
"context" "context"
"fmt" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/watcher" "gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/test/require"
"gitoa.ru/go-4devs/config/value" "gitoa.ru/go-4devs/config/value"
) )
var _ config.Provider = (*provider)(nil)
type provider struct { type provider struct {
cnt int32 cnt int32
} }
@@ -25,13 +27,17 @@ func (p *provider) Name() string {
func (p *provider) Value(context.Context, ...string) (config.Value, error) { func (p *provider) Value(context.Context, ...string) (config.Value, error) {
p.cnt++ p.cnt++
return value.JString(fmt.Sprint(p.cnt)), nil return value.JString(strconv.Itoa(int(p.cnt))), nil
} }
func TestWatcher(t *testing.T) { func TestWatcher(t *testing.T) {
t.Parallel() t.Parallel()
ctx := context.Background() ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer func() {
cancel()
}()
prov := &provider{} prov := &provider{}
w := watcher.New(time.Second, prov) w := watcher.New(time.Second, prov)
@@ -42,14 +48,20 @@ func TestWatcher(t *testing.T) {
err := w.Watch( err := w.Watch(
ctx, ctx,
func(ctx context.Context, oldVar, newVar config.Value) { func(ctx context.Context, oldVar, newVar config.Value) error {
atomic.AddInt32(&cnt, 1) atomic.AddInt32(&cnt, 1)
wg.Done() wg.Done()
if atomic.LoadInt32(&cnt) == 2 {
return config.ErrStopWatch
}
return nil
}, },
"tmpname", "tmpname",
) )
require.NoError(t, err)
wg.Wait() wg.Wait()
require.NoError(t, err)
require.Equal(t, int32(2), cnt) require.Equal(t, int32(2), cnt)
} }

8
provider/yaml/go.mod Normal file
View File

@@ -0,0 +1,8 @@
module gitoa.ru/go-4devs/config/provider/yaml
go 1.21
require (
gitoa.ru/go-4devs/config v0.0.1
gopkg.in/yaml.v3 v3.0.1
)

6
provider/yaml/go.sum Normal file
View File

@@ -0,0 +1,6 @@
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -59,7 +59,6 @@ func (p *Provider) Name() string {
} }
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) { func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
return p.data.read(p.Name(), path) return p.data.read(p.Name(), path)
} }

View File

@@ -1,19 +1,25 @@
package yaml_test package yaml_test
import ( import (
"embed"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require" "gitoa.ru/go-4devs/config/provider/yaml"
provider "gitoa.ru/go-4devs/config/provider/yaml"
"gitoa.ru/go-4devs/config/test" "gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
) )
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) { func TestProvider(t *testing.T) {
t.Parallel() t.Parallel()
prov, err := provider.New(test.ReadFile("config.yaml")) data, err := fixture.ReadFile("fixture/config.yaml")
require.Nil(t, err) require.NoError(t, err)
prov, err := yaml.New(data)
require.NoError(t, err)
read := []test.Read{ read := []test.Read{
test.NewRead(21*time.Minute, "duration_var"), test.NewRead(21*time.Minute, "duration_var"),

View File

@@ -12,13 +12,13 @@ import (
const NameWatch = "yaml_watch" const NameWatch = "yaml_watch"
func NewWatch(name string, opts ...Option) *Watch { func NewWatch(name string, opts ...Option) *Watch {
f := Watch{ wath := Watch{
file: name, file: name,
prov: create(opts...), prov: create(opts...),
name: NameWatch, name: NameWatch,
} }
return &f return &wath
} }
type Watch struct { type Watch struct {

30
test/assert/equal.go Normal file
View File

@@ -0,0 +1,30 @@
package assert
import (
"reflect"
"testing"
)
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
t.Helper()
if reflect.DeepEqual(expected, actual) {
return true
}
t.Error(msgAndArgs...)
return false
}
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
t.Helper()
if reflect.DeepEqual(expected, actual) {
return true
}
t.Errorf(msg, args...)
return false
}

15
test/assert/nil.go Normal file
View File

@@ -0,0 +1,15 @@
package assert
import "testing"
func Nil(t *testing.T, data any, msgAndArgs ...interface{}) bool {
t.Helper()
if data != nil {
t.Error(msgAndArgs...)
return false
}
return true
}

View File

@@ -1,17 +0,0 @@
package test
import (
"path/filepath"
"runtime"
)
func FixturePath(file string) string {
path := "fixture/"
_, filename, _, ok := runtime.Caller(0)
if ok {
path = filepath.Dir(filename) + "/" + path
}
return path + file
}

View File

@@ -1,16 +0,0 @@
package test
import (
"log"
"gopkg.in/ini.v1"
)
func NewINI() *ini.File {
f, err := ini.Load(FixturePath("config.ini"))
if err != nil {
log.Fatal(err)
}
return f
}

View File

@@ -1,15 +0,0 @@
package test
import (
"io/ioutil"
"log"
)
func ReadFile(file string) []byte {
data, err := ioutil.ReadFile(FixturePath(file))
if err != nil {
log.Fatal(err)
}
return data
}

View File

@@ -2,14 +2,12 @@ package test
import ( import (
"context" "context"
"io/ioutil" "fmt"
"path/filepath"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
) )
const ( const (
@@ -21,17 +19,15 @@ const (
func Run(t *testing.T, provider config.Provider, read []Read) { func Run(t *testing.T, provider config.Provider, read []Read) {
t.Helper() t.Helper()
prov := &ProviderSuite{ ctx := context.Background()
provider: provider,
read: read,
}
suite.Run(t, prov)
}
type ProviderSuite struct { for idx, read := range read {
suite.Suite t.Run(fmt.Sprintf("%v:%v", idx, read.Key), func(t *testing.T) {
provider config.Provider val, err := provider.Value(ctx, read.Key...)
read []Read require.NoError(t, err, read.Key)
read.Assert(t, val)
})
}
} }
type Read struct { type Read struct {
@@ -39,8 +35,6 @@ type Read struct {
Assert func(t *testing.T, v config.Value) Assert func(t *testing.T, v config.Value)
} }
const ConfigJSON = `{"duration":1260000000000,"enabled":true}`
type Config struct { type Config struct {
Duration time.Duration Duration time.Duration
Enabled bool Enabled bool
@@ -112,7 +106,7 @@ func NewRead(expected interface{}, key ...string) Read {
val, err = v.ParseTime() val, err = v.ParseTime()
short = v.Time() short = v.Time()
default: default:
require.Fail(t, "unexpected type", "type:%+T", expected) require.Fail(t, "unexpected type:%+T", expected)
} }
require.Equalf(t, val, short, "type:%T", expected) require.Equalf(t, val, short, "type:%T", expected)
@@ -121,22 +115,3 @@ func NewRead(expected interface{}, key ...string) Read {
}, },
} }
} }
func (ps *ProviderSuite) TestReadKeys() {
ctx := context.Background()
for _, read := range ps.read {
val, err := ps.provider.Value(ctx, read.Key...)
require.NoError(ps.T(), err, read.Key)
read.Assert(ps.T(), val)
}
}
func LoadConfig(t *testing.T, path string) []byte {
t.Helper()
file, err := ioutil.ReadFile(filepath.Clean(path))
require.NoError(t, err)
return file
}

28
test/require/equal.go Normal file
View File

@@ -0,0 +1,28 @@
package require
import (
"testing"
"gitoa.ru/go-4devs/config/test/assert"
)
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
t.Helper()
if assert.Equal(t, expected, actual, msgAndArgs...) {
return
}
t.FailNow()
}
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
t.Helper()
if assert.Equalf(t, expected, actual, msg, args...) {
return
}
t.Errorf(msg, args...)
t.FailNow()
}

23
test/require/error.go Normal file
View File

@@ -0,0 +1,23 @@
package require
import (
"testing"
)
func NoError(t *testing.T, err error, msgAndArgs ...interface{}) {
t.Helper()
if err != nil {
t.Error(msgAndArgs...)
t.FailNow()
}
}
func NoErrorf(t *testing.T, err error, msg string, args ...interface{}) {
t.Helper()
if err != nil {
t.Errorf(msg, args...)
t.FailNow()
}
}

9
test/require/fail.go Normal file
View File

@@ -0,0 +1,9 @@
package require
import "testing"
func Fail(t *testing.T, msg string, args ...interface{}) {
t.Helper()
t.Errorf(msg, args...)
t.FailNow()
}

14
test/require/true.go Normal file
View File

@@ -0,0 +1,14 @@
package require
import (
"testing"
)
func Truef(t *testing.T, value bool, msg string, args ...interface{}) {
t.Helper()
if !value {
t.Errorf(msg, args...)
t.FailNow()
}
}

View File

@@ -8,7 +8,7 @@ type Value interface {
ReadValue ReadValue
ParseValue ParseValue
UnmarshalValue UnmarshalValue
IsEquals(Value) bool IsEquals(in Value) bool
} }
type UnmarshalValue interface { type UnmarshalValue interface {

View File

@@ -5,8 +5,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
"gitoa.ru/go-4devs/config/value" "gitoa.ru/go-4devs/config/value"
) )

View File

@@ -1,11 +0,0 @@
package config
type Variable struct {
Name string
Provider string
Value Value
}
func (v Variable) IsEquals(n Variable) bool {
return n.Name == v.Name && n.Provider == v.Provider && n.Value.String() == v.Value.String()
}