Compare commits
1 Commits
v0.0.5
...
1416ab1b3a
| Author | SHA1 | Date | |
|---|---|---|---|
| 1416ab1b3a |
29
.drone.yml
Normal file
29
.drone.yml
Normal file
@@ -0,0 +1,29 @@
|
||||
kind: pipeline
|
||||
name: default
|
||||
|
||||
services:
|
||||
- name: vault
|
||||
image: vault:1.7.1
|
||||
environment:
|
||||
VAULT_DEV_ROOT_TOKEN_ID: dev
|
||||
VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200
|
||||
- name: etcd
|
||||
image: bitnami/etcd:3
|
||||
environment:
|
||||
ALLOW_NONE_AUTHENTICATION: yes
|
||||
|
||||
environment:
|
||||
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200
|
||||
VAULT_DEV_ROOT_TOKEN_ID: dev
|
||||
FDEVS_CONFIG_ETCD_HOST: etcd:2379
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
commands:
|
||||
- go test -parallel 10 -race ./...
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.53
|
||||
commands:
|
||||
- golangci-lint run
|
||||
@@ -1,40 +0,0 @@
|
||||
name: Go Action
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
services:
|
||||
# The label "etcd-server" becomes the hostname for the service.
|
||||
etcd-server:
|
||||
# Use the official etcd Docker image
|
||||
image: quay.io/coreos/etcd:v3.6.7
|
||||
env:
|
||||
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
|
||||
ETCD_ADVERTISE_CLIENT_URLS: http://etcd-server:2379
|
||||
|
||||
env:
|
||||
FDEVS_CONFIG_ETCD_HOST: 'etcd-server:2379'
|
||||
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
working-directory: ./provider/etcd
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
working-directory: ./provider/etcd
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
name: Go Action
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
name: Go Action
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
working-directory: ./provider/ini
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
working-directory: ./provider/ini
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
name: Go Action
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
working-directory: ./provider/json
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
working-directory: ./provider/json
|
||||
|
||||
115
.golangci.yml
115
.golangci.yml
@@ -1,20 +1,7 @@
|
||||
version: "2"
|
||||
linters:
|
||||
default: all
|
||||
disable:
|
||||
- noinlineerr
|
||||
- depguard
|
||||
- ireturn
|
||||
- gochecknoglobals
|
||||
# deprecated
|
||||
- wsl
|
||||
settings:
|
||||
recvcheck:
|
||||
disable-builtin: true
|
||||
exclusions:
|
||||
- "*.String"
|
||||
funcorder:
|
||||
constructor: false
|
||||
run:
|
||||
timeout: 5m
|
||||
|
||||
linters-settings:
|
||||
dupl:
|
||||
threshold: 100
|
||||
funlen:
|
||||
@@ -23,60 +10,54 @@ linters:
|
||||
goconst:
|
||||
min-len: 2
|
||||
min-occurrences: 2
|
||||
cyclop:
|
||||
max-complexity: 15
|
||||
gocyclo:
|
||||
min-complexity: 15
|
||||
golint:
|
||||
min-confidence: 0
|
||||
govet:
|
||||
check-shadowing: true
|
||||
lll:
|
||||
line-length: 140
|
||||
maligned:
|
||||
suggest-new: true
|
||||
misspell:
|
||||
locale: US
|
||||
locale: US
|
||||
varnamelen:
|
||||
min-name-length: 2
|
||||
ignore-decls:
|
||||
- w io.Writer
|
||||
- t testing.T
|
||||
- e error
|
||||
- i int
|
||||
- b bytes.Buffer
|
||||
- h Handle
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- legacy
|
||||
- std-error-handling
|
||||
rules:
|
||||
- linters:
|
||||
- exhaustivestruct
|
||||
- exhaustruct
|
||||
- funlen
|
||||
- mnd
|
||||
- tenv
|
||||
- varnamelen
|
||||
- wrapcheck
|
||||
path: _test\.go
|
||||
- linters:
|
||||
- exhaustivestruct
|
||||
- exhaustruct
|
||||
- mnd
|
||||
- varnamelen
|
||||
- wrapcheck
|
||||
path: test/*
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
formatters:
|
||||
enable:
|
||||
- gci
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goimports
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- exhaustivestruct
|
||||
- maligned
|
||||
- interfacer
|
||||
- scopelint
|
||||
- exhaustruct
|
||||
- depguard
|
||||
#deprecated
|
||||
- structcheck
|
||||
- varcheck
|
||||
- golint
|
||||
- deadcode
|
||||
- ifshort
|
||||
- nosnakecase
|
||||
|
||||
issues:
|
||||
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||
exclude-rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gomnd
|
||||
- exhaustivestruct
|
||||
- wrapcheck
|
||||
- exhaustruct
|
||||
- varnamelen
|
||||
- tenv
|
||||
- funlen
|
||||
- path: test/*
|
||||
linters:
|
||||
- gomnd
|
||||
- exhaustivestruct
|
||||
- wrapcheck
|
||||
- exhaustruct
|
||||
- varnamelen
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# config
|
||||
|
||||

|
||||
[](https://drone.gitoa.ru/go-4devs/config)
|
||||
[](https://goreportcard.com/report/gitoa.ru/go-4devs/config)
|
||||
[](http://godoc.org/gitoa.ru/go-4devs/config)
|
||||
|
||||
133
client.go
133
client.go
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func Must(providers ...any) *Client {
|
||||
client, err := New(providers...)
|
||||
func Must(namespace, appName string, providers ...interface{}) *Client {
|
||||
client, err := New(namespace, appName, providers...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -15,8 +17,10 @@ func Must(providers ...any) *Client {
|
||||
return client
|
||||
}
|
||||
|
||||
func New(providers ...any) (*Client, error) {
|
||||
func New(namespace, appName string, providers ...interface{}) (*Client, error) {
|
||||
client := &Client{
|
||||
namespace: namespace,
|
||||
appName: appName,
|
||||
providers: make([]Provider, len(providers)),
|
||||
}
|
||||
|
||||
@@ -25,7 +29,11 @@ func New(providers ...any) (*Client, error) {
|
||||
case Provider:
|
||||
client.providers[idx] = current
|
||||
case Factory:
|
||||
client.providers[idx] = WrapFactory(current, client)
|
||||
client.providers[idx] = &provider{
|
||||
factory: func(ctx context.Context) (Provider, error) {
|
||||
return current(ctx, client)
|
||||
},
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("provier[%d]: %w %T", idx, ErrUnknowType, prov)
|
||||
}
|
||||
@@ -34,45 +42,119 @@ func New(providers ...any) (*Client, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
providers []Provider
|
||||
type provider struct {
|
||||
mu sync.Mutex
|
||||
done uint32
|
||||
provider Provider
|
||||
factory func(ctx context.Context) (Provider, error)
|
||||
}
|
||||
|
||||
func (c *Client) Name() string {
|
||||
return "client"
|
||||
func (p *provider) init(ctx context.Context) error {
|
||||
if atomic.LoadUint32(&p.done) == 0 {
|
||||
if !p.mu.TryLock() {
|
||||
return fmt.Errorf("%w", ErrInitFactory)
|
||||
}
|
||||
defer atomic.StoreUint32(&p.done, 1)
|
||||
defer p.mu.Unlock()
|
||||
|
||||
var err error
|
||||
if p.provider, err = p.factory(ctx); err != nil {
|
||||
return fmt.Errorf("init provider factory:%w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *provider) Watch(ctx context.Context, key Key, callback WatchCallback) error {
|
||||
if err := p.init(ctx); err != nil {
|
||||
return fmt.Errorf("init read:%w", err)
|
||||
}
|
||||
|
||||
watch, ok := p.provider.(WatchProvider)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := watch.Watch(ctx, key, callback); err != nil {
|
||||
return fmt.Errorf("factory provider: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *provider) Read(ctx context.Context, key Key) (Variable, error) {
|
||||
if err := p.init(ctx); err != nil {
|
||||
return Variable{}, fmt.Errorf("init read:%w", err)
|
||||
}
|
||||
|
||||
variable, err := p.provider.Read(ctx, key)
|
||||
if err != nil {
|
||||
return Variable{}, fmt.Errorf("factory provider: %w", err)
|
||||
}
|
||||
|
||||
return variable, nil
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
providers []Provider
|
||||
appName string
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (c *Client) key(name string) Key {
|
||||
return Key{
|
||||
Name: name,
|
||||
AppName: c.appName,
|
||||
Namespace: c.namespace,
|
||||
}
|
||||
}
|
||||
|
||||
// Value get value by name.
|
||||
func (c *Client) Value(ctx context.Context, path ...string) (Value, error) {
|
||||
// nolint: ireturn
|
||||
func (c *Client) Value(ctx context.Context, name string) (Value, error) {
|
||||
variable, err := c.Variable(ctx, name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("variable:%w", err)
|
||||
}
|
||||
|
||||
return variable.Value, nil
|
||||
}
|
||||
|
||||
func (c *Client) Variable(ctx context.Context, name string) (Variable, error) {
|
||||
var (
|
||||
value Value
|
||||
err error
|
||||
variable Variable
|
||||
err error
|
||||
)
|
||||
|
||||
key := c.key(name)
|
||||
|
||||
for _, provider := range c.providers {
|
||||
value, err = provider.Value(ctx, path...)
|
||||
if err == nil || (!errors.Is(err, ErrNotFound) && !errors.Is(err, ErrInitFactory)) {
|
||||
variable, err = provider.Read(ctx, key)
|
||||
if err == nil || !(errors.Is(err, ErrVariableNotFound) || errors.Is(err, ErrInitFactory)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return value, fmt.Errorf("client failed get value: %w", err)
|
||||
return variable, fmt.Errorf("client failed get variable: %w", err)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
return variable, nil
|
||||
}
|
||||
|
||||
func (c *Client) Watch(ctx context.Context, callback WatchCallback, path ...string) error {
|
||||
func (c *Client) Watch(ctx context.Context, name string, callback WatchCallback) error {
|
||||
key := c.key(name)
|
||||
|
||||
for idx, prov := range c.providers {
|
||||
provider, ok := prov.(WatchProvider)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
err := provider.Watch(ctx, callback, path...)
|
||||
err := provider.Watch(ctx, key, callback)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) || errors.Is(err, ErrInitFactory) {
|
||||
if errors.Is(err, ErrVariableNotFound) || errors.Is(err, ErrInitFactory) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -82,18 +164,3 @@ func (c *Client) Watch(ctx context.Context, callback WatchCallback, path ...stri
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Bind(ctx context.Context, data Variables) error {
|
||||
for idx, prov := range c.providers {
|
||||
provider, ok := prov.(BindProvider)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := provider.Bind(ctx, data); err != nil {
|
||||
return fmt.Errorf("bind[%d] %v:%w", idx, provider.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,11 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -28,11 +32,218 @@ func ExampleClient_Value() {
|
||||
|
||||
os.Args = []string{"main.go", "--host=gitoa.ru"}
|
||||
|
||||
// read json config
|
||||
// configure etcd client
|
||||
etcdClient, err := test.NewEtcd(ctx)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
config, err := config.New(
|
||||
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(test.Namespace, test.AppName,
|
||||
arg.New(),
|
||||
env.New(test.Namespace, test.AppName),
|
||||
env.New(),
|
||||
etcd.NewProvider(etcdClient),
|
||||
vault.NewSecretKV2(vaultClient),
|
||||
json.New(jsonConfig),
|
||||
)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
dsn, err := config.Value(ctx, "example:dsn")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
port, err := config.Value(ctx, "listen")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
enabled, err := config.Value(ctx, "maintain")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
title, err := config.Value(ctx, "app.name.title")
|
||||
if err != nil {
|
||||
log.Print(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() {
|
||||
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(test.Namespace, test.AppName,
|
||||
watcher.New(time.Microsecond, env.New()),
|
||||
watcher.New(time.Microsecond, yaml.NewWatch("test/fixture/config.yaml")),
|
||||
etcd.NewProvider(etcdClient),
|
||||
)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
err = watcher.Watch(ctx, "example_enable", func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
fmt.Println("update ", oldVar.Provider, " variable:", oldVar.Name, ", old: ", oldVar.Value.Bool(), " new:", newVar.Value.Bool())
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")
|
||||
|
||||
err = watcher.Watch(ctx, "example_db_dsn", func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
fmt.Println("update ", oldVar.Provider, " variable:", oldVar.Name, ", old: ", oldVar.Value.String(), " new:", newVar.Value.String())
|
||||
wg.Done()
|
||||
})
|
||||
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 env variable: FDEVS_CONFIG_EXAMPLE_ENABLE , old: true new: false
|
||||
// update etcd variable: fdevs/config/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(test.Namespace, test.AppName,
|
||||
arg.New(),
|
||||
env.New(),
|
||||
config.Factory(func(ctx context.Context, cfg config.ReadConfig) (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.ReadConfig) (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)
|
||||
@@ -42,133 +253,42 @@ func ExampleClient_Value() {
|
||||
|
||||
port, err := config.Value(ctx, "listen")
|
||||
if err != nil {
|
||||
log.Print("listen: ", err)
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
hostValue, err := config.Value(ctx, "host")
|
||||
title, err := config.Value(ctx, "app.name.title")
|
||||
if err != nil {
|
||||
log.Print("host:", err)
|
||||
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("replace env host by args: %v\n", hostValue.String())
|
||||
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
|
||||
// replace env host by args: gitoa.ru
|
||||
}
|
||||
|
||||
func ExampleClient_Watch() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")
|
||||
|
||||
watcher, err := config.New(
|
||||
watcher.New(time.Microsecond, env.New(test.Namespace, test.AppName)),
|
||||
)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
err = watcher.Watch(ctx, func(_ context.Context, oldVar, newVar config.Value) error {
|
||||
fmt.Println("update example_enable old: ", oldVar.Bool(), " new:", newVar.Bool())
|
||||
wg.Done()
|
||||
|
||||
return nil
|
||||
}, "example_enable")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")
|
||||
|
||||
err = watcher.Watch(ctx, func(_ context.Context, oldVar, newVar config.Value) error {
|
||||
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
|
||||
wg.Done()
|
||||
|
||||
return nil
|
||||
}, "example_db_dsn")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Output:
|
||||
// update example_enable old: true new: false
|
||||
}
|
||||
|
||||
func ExampleClient_Value_factory() {
|
||||
ctx := context.Background()
|
||||
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
|
||||
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
|
||||
_ = os.Setenv("FDEVS_GOLANG_HOST", "go.dev")
|
||||
|
||||
args := os.Args
|
||||
|
||||
defer func() {
|
||||
os.Args = args
|
||||
}()
|
||||
|
||||
os.Args = []string{"main.go", "--env=golang"}
|
||||
|
||||
config, err := config.New(
|
||||
arg.New(),
|
||||
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
|
||||
val, err := cfg.Value(ctx, "env")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed read config file:%w", err)
|
||||
}
|
||||
|
||||
return env.New(test.Namespace, val.String()), nil
|
||||
}),
|
||||
env.New(test.Namespace, test.AppName),
|
||||
)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
envName, err := config.Value(ctx, "env")
|
||||
if err != nil {
|
||||
log.Print("env ", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
host, err := config.Value(ctx, "host")
|
||||
if err != nil {
|
||||
log.Print("host ", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
listen, err := config.Value(ctx, "listen")
|
||||
if err != nil {
|
||||
log.Print("listen", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("envName from env: %s\n", envName.String())
|
||||
fmt.Printf("host from env with app name golang: %s\n", host.String())
|
||||
fmt.Printf("listen from env with default app name: %s\n", listen.String())
|
||||
// Output:
|
||||
// envName from env: golang
|
||||
// host from env with app name golang: go.dev
|
||||
// listen from env with default app name: 8080
|
||||
// title from json: config title
|
||||
// yaml title: yaml title
|
||||
// struct from json: {Duration:21m0s Enabled:true}
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package definition
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config"
|
||||
)
|
||||
|
||||
func New(opts ...config.Option) *Definition {
|
||||
return &Definition{
|
||||
options: opts,
|
||||
}
|
||||
}
|
||||
|
||||
type Definition struct {
|
||||
options []config.Option
|
||||
}
|
||||
|
||||
func (d *Definition) Add(opts ...config.Option) {
|
||||
d.options = append(d.options, opts...)
|
||||
}
|
||||
|
||||
func (d *Definition) Options() []config.Option {
|
||||
return d.options
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition/generate/pkg"
|
||||
)
|
||||
|
||||
//go:embed *.tpl
|
||||
var tpls embed.FS
|
||||
|
||||
type Boot struct {
|
||||
Config
|
||||
|
||||
imp *pkg.Imports
|
||||
Configure []string
|
||||
OutName string
|
||||
}
|
||||
|
||||
func (b Boot) Imports() []pkg.Import {
|
||||
return b.imp.Imports()
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
File() string
|
||||
Methods() []string
|
||||
SkipContext() bool
|
||||
Prefix() string
|
||||
Suffix() string
|
||||
FullPkg() string
|
||||
Pkg() string
|
||||
}
|
||||
|
||||
func Bootstrap(ctx context.Context, cfg Config) (string, error) {
|
||||
fInfo, err := os.Stat(cfg.File())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("stat:%w", err)
|
||||
}
|
||||
|
||||
pkgPath, err := pkg.ByPath(ctx, cfg.File(), fInfo.IsDir())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pkg by path:%w", err)
|
||||
}
|
||||
|
||||
tmpFile, err := os.CreateTemp(filepath.Dir(fInfo.Name()), "config-bootstrap")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create tmp file:%w", err)
|
||||
}
|
||||
|
||||
tpl, err := template.ParseFS(tpls, "bootstrap.go.tpl")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse template:%w", err)
|
||||
}
|
||||
|
||||
imports := pkg.NewImports("main").
|
||||
Adds(
|
||||
"context",
|
||||
"gitoa.ru/go-4devs/config/definition",
|
||||
"gitoa.ru/go-4devs/config/definition/generate",
|
||||
"os",
|
||||
"fmt",
|
||||
"go/format",
|
||||
pkgPath,
|
||||
)
|
||||
|
||||
data := Boot{
|
||||
imp: imports,
|
||||
Configure: cfg.Methods(),
|
||||
OutName: fInfo.Name()[0:len(fInfo.Name())-3] + "_config.go",
|
||||
Config: cfg,
|
||||
}
|
||||
|
||||
if err := tpl.Execute(tmpFile, data); err != nil {
|
||||
return "", fmt.Errorf("execute:%w", err)
|
||||
}
|
||||
|
||||
src := tmpFile.Name()
|
||||
if err := tmpFile.Close(); err != nil {
|
||||
return src, fmt.Errorf("close file:%w", err)
|
||||
}
|
||||
|
||||
dest := src + ".go"
|
||||
|
||||
if err := os.Rename(src, dest); err != nil {
|
||||
return dest, fmt.Errorf("rename idt:%w", err)
|
||||
}
|
||||
|
||||
return dest, nil
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
{{range .Imports}}
|
||||
{{- .Alias }}"{{ .Package }}"
|
||||
{{end}}
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
ctx := context.Background()
|
||||
|
||||
f, err := os.Create("{{.OutName}}")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defs:=make([]generate.Input,0)
|
||||
{{ range .Configure }}
|
||||
def{{.}} := definition.New()
|
||||
if err := {{$.Pkg}}.{{.}}(ctx, def{{.}}); err != nil {
|
||||
return err
|
||||
}
|
||||
defs = append(defs,generate.NewInput("{{.}}",def{{.}}))
|
||||
{{ end }}
|
||||
|
||||
opts := make([]generate.Option,0)
|
||||
{{ if .SkipContext }}opts = append(opts, generate.WithSkipContext){{ end }}
|
||||
opts = append(opts,
|
||||
generate.WithPrefix("{{.Prefix}}"),
|
||||
generate.WithSuffix("{{.Suffix}}"),
|
||||
generate.WithFullPkg("{{.FullPkg}}"),
|
||||
)
|
||||
|
||||
if gerr := generate.Run(ctx,generate.NewConfig(opts...),f, defs...);gerr != nil {
|
||||
return gerr
|
||||
}
|
||||
|
||||
in, err := os.ReadFile(f.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := format.Source(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(f.Name(), out, 0644)
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
"gitoa.ru/go-4devs/config/definition/group"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/definition/proto"
|
||||
)
|
||||
|
||||
type Level string
|
||||
|
||||
func (l *Level) UnmarshalText(in []byte) error {
|
||||
data := string(in)
|
||||
*l = Level(data)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Config(_ context.Context, def *definition.Definition) error {
|
||||
def.Add(
|
||||
option.String("test", "test string", view.WithSkipContext),
|
||||
group.New("user", "configure user",
|
||||
option.String("name", "name", option.Default("4devs")),
|
||||
option.String("pass", "password"),
|
||||
),
|
||||
|
||||
group.New("log", "configure logger",
|
||||
option.New("level", "log level", Level("")),
|
||||
proto.New("service", "servise logger", option.New("level", "log level", Level(""))),
|
||||
),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
|
||||
package example
|
||||
|
||||
import (
|
||||
context "context"
|
||||
fmt "fmt"
|
||||
config "gitoa.ru/go-4devs/config"
|
||||
)
|
||||
|
||||
func WithInputConfigLog(log func(context.Context, string, ...any)) func(*InputConfig) {
|
||||
return func(ci *InputConfig) {
|
||||
ci.log = log
|
||||
}
|
||||
}
|
||||
|
||||
func NewInputConfig(prov config.Provider, opts ...func(*InputConfig)) InputConfig {
|
||||
i := InputConfig{
|
||||
Provider: prov,
|
||||
log: func(_ context.Context, format string, args ...any) {
|
||||
fmt.Printf(format, args...)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&i)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
type InputConfig struct {
|
||||
config.Provider
|
||||
log func(context.Context, string, ...any)
|
||||
}
|
||||
|
||||
// readTest test string.
|
||||
func (i InputConfig) readTest(ctx context.Context) (v string, e error) {
|
||||
val, err := i.Value(ctx, "test")
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"test"}, err)
|
||||
|
||||
}
|
||||
|
||||
return val.ParseString()
|
||||
|
||||
}
|
||||
|
||||
// ReadTest test string.
|
||||
func (i InputConfig) ReadTest() (string, error) {
|
||||
return i.readTest(context.Background())
|
||||
}
|
||||
|
||||
// Test test string.
|
||||
func (i InputConfig) Test() string {
|
||||
val, err := i.readTest(context.Background())
|
||||
if err != nil {
|
||||
i.log(context.Background(), "get [%v]: %v", []string{"test"}, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
type InputConfigUser struct {
|
||||
InputConfig
|
||||
}
|
||||
|
||||
// User configure user.
|
||||
func (i InputConfig) User() InputConfigUser {
|
||||
return InputConfigUser{i}
|
||||
}
|
||||
|
||||
// readName name.
|
||||
func (i InputConfigUser) readName(ctx context.Context) (v string, e error) {
|
||||
val, err := i.Value(ctx, "user", "name")
|
||||
if err != nil {
|
||||
i.log(context.Background(), "read [%v]: %v", []string{"user", "name"}, err)
|
||||
|
||||
return "4devs", nil
|
||||
}
|
||||
|
||||
return val.ParseString()
|
||||
|
||||
}
|
||||
|
||||
// ReadName name.
|
||||
func (i InputConfigUser) ReadName(ctx context.Context) (string, error) {
|
||||
return i.readName(ctx)
|
||||
}
|
||||
|
||||
// Name name.
|
||||
func (i InputConfigUser) Name(ctx context.Context) string {
|
||||
val, err := i.readName(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get [%v]: %v", []string{"user", "name"}, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// readPass password.
|
||||
func (i InputConfigUser) readPass(ctx context.Context) (v string, e error) {
|
||||
val, err := i.Value(ctx, "user", "pass")
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"user", "pass"}, err)
|
||||
|
||||
}
|
||||
|
||||
return val.ParseString()
|
||||
|
||||
}
|
||||
|
||||
// ReadPass password.
|
||||
func (i InputConfigUser) ReadPass(ctx context.Context) (string, error) {
|
||||
return i.readPass(ctx)
|
||||
}
|
||||
|
||||
// Pass password.
|
||||
func (i InputConfigUser) Pass(ctx context.Context) string {
|
||||
val, err := i.readPass(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get [%v]: %v", []string{"user", "pass"}, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
type InputConfigLog struct {
|
||||
InputConfig
|
||||
}
|
||||
|
||||
// Log configure logger.
|
||||
func (i InputConfig) Log() InputConfigLog {
|
||||
return InputConfigLog{i}
|
||||
}
|
||||
|
||||
// readLevel log level.
|
||||
func (i InputConfigLog) readLevel(ctx context.Context) (v Level, e error) {
|
||||
val, err := i.Value(ctx, "log", "level")
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"log", "level"}, err)
|
||||
|
||||
}
|
||||
|
||||
pval, perr := val.ParseString()
|
||||
if perr != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"log", "level"}, perr)
|
||||
}
|
||||
|
||||
return v, v.UnmarshalText([]byte(pval))
|
||||
}
|
||||
|
||||
// ReadLevel log level.
|
||||
func (i InputConfigLog) ReadLevel(ctx context.Context) (Level, error) {
|
||||
return i.readLevel(ctx)
|
||||
}
|
||||
|
||||
// Level log level.
|
||||
func (i InputConfigLog) Level(ctx context.Context) Level {
|
||||
val, err := i.readLevel(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get [%v]: %v", []string{"log", "level"}, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
type InputConfigLogService struct {
|
||||
InputConfigLog
|
||||
service string
|
||||
}
|
||||
|
||||
// Service servise logger.
|
||||
func (i InputConfigLog) Service(key string) InputConfigLogService {
|
||||
return InputConfigLogService{i, key}
|
||||
}
|
||||
|
||||
// readLevel log level.
|
||||
func (i InputConfigLogService) readLevel(ctx context.Context) (v Level, e error) {
|
||||
val, err := i.Value(ctx, "log", i.service, "level")
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"log", i.service, "level"}, err)
|
||||
|
||||
}
|
||||
|
||||
pval, perr := val.ParseString()
|
||||
if perr != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"log", i.service, "level"}, perr)
|
||||
}
|
||||
|
||||
return v, v.UnmarshalText([]byte(pval))
|
||||
}
|
||||
|
||||
// ReadLevel log level.
|
||||
func (i InputConfigLogService) ReadLevel(ctx context.Context) (Level, error) {
|
||||
return i.readLevel(ctx)
|
||||
}
|
||||
|
||||
// Level log level.
|
||||
func (i InputConfigLogService) Level(ctx context.Context) Level {
|
||||
val, err := i.readLevel(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get [%v]: %v", []string{"log", i.service, "level"}, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package example_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/bootstrap"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/example"
|
||||
)
|
||||
|
||||
func TestGenerate_Bootstrap(t *testing.T) {
|
||||
t.SkipNow()
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
options := definition.New()
|
||||
_ = example.Config(ctx, options)
|
||||
|
||||
cfg, _ := generate.NewGConfig("./config.go",
|
||||
generate.WithMethods("Config"),
|
||||
generate.WithFullPkg("gitoa.ru/go-4devs/config/definition/generate/example"),
|
||||
)
|
||||
|
||||
path, err := bootstrap.Bootstrap(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
os.Remove(path)
|
||||
|
||||
t.Log(path)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func TestGenerate_Genereate(t *testing.T) {
|
||||
t.SkipNow()
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
options := definition.New()
|
||||
_ = example.Config(ctx, options)
|
||||
|
||||
cfg, _ := generate.NewGConfig("./config.go",
|
||||
generate.WithMethods("Config"),
|
||||
generate.WithFullPkg("gitoa.ru/go-4devs/config/definition/generate/example"),
|
||||
)
|
||||
|
||||
err := generate.Generate(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition/generate/bootstrap"
|
||||
)
|
||||
|
||||
func NewGConfig(fname string, opts ...Option) (Config, error) {
|
||||
opts = append([]Option{
|
||||
WithFile(fname),
|
||||
}, opts...)
|
||||
|
||||
return NewConfig(opts...), nil
|
||||
}
|
||||
|
||||
type GConfig interface {
|
||||
BuildTags() string
|
||||
OutName() string
|
||||
bootstrap.Config
|
||||
}
|
||||
|
||||
func Generate(ctx context.Context, cfg GConfig) error {
|
||||
path, err := bootstrap.Bootstrap(ctx, cfg)
|
||||
defer os.Remove(path)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("build bootstrap:%w", err)
|
||||
}
|
||||
|
||||
tmpFile, err := os.Create(cfg.File() + ".tmp")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create tmp file:%w", err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpFile.Name()) // will not remove after rename
|
||||
|
||||
execArgs := []string{"run"}
|
||||
if len(cfg.BuildTags()) > 0 {
|
||||
execArgs = append(execArgs, "-tags", cfg.BuildTags())
|
||||
}
|
||||
|
||||
execArgs = append(execArgs, filepath.Base(path))
|
||||
cmd := exec.CommandContext(ctx, "go", execArgs...)
|
||||
|
||||
cmd.Stdout = tmpFile
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
cmd.Dir = filepath.Dir(path)
|
||||
if err = cmd.Run(); err != nil {
|
||||
return fmt.Errorf("start cmd:%w", err)
|
||||
}
|
||||
|
||||
tmpFile.Close()
|
||||
|
||||
// format file and write to out path
|
||||
in, err := os.ReadFile(tmpFile.Name())
|
||||
if err != nil {
|
||||
return fmt.Errorf("read file: %w", err)
|
||||
}
|
||||
|
||||
out, err := format.Source(in)
|
||||
if err != nil {
|
||||
return fmt.Errorf("format source:%w", err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(cfg.OutName(), out, 0o644) //nolint:gosec,mnd
|
||||
if err != nil {
|
||||
return fmt.Errorf("write file:%w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"text/template"
|
||||
"unicode"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/pkg"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/render"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
type Option func(*Config)
|
||||
|
||||
func WithSkipContext(c *Config) {
|
||||
view.WithSkipContext(c.Params)
|
||||
}
|
||||
|
||||
func WithPrefix(name string) Option {
|
||||
return func(c *Config) {
|
||||
c.prefix = name
|
||||
}
|
||||
}
|
||||
|
||||
func WithSuffix(name string) Option {
|
||||
return func(c *Config) {
|
||||
c.suffix = name
|
||||
}
|
||||
}
|
||||
|
||||
// WithMethods set methosd.
|
||||
//
|
||||
// generate.WithMethods(runtime.FuncForPC(reflect.ValueOf(configure).Pointer()).Name()).
|
||||
func WithMethods(in ...string) Option {
|
||||
return func(c *Config) {
|
||||
c.methods = in
|
||||
}
|
||||
}
|
||||
|
||||
func WithOutName(in string) Option {
|
||||
return func(c *Config) {
|
||||
c.outName = in
|
||||
}
|
||||
}
|
||||
|
||||
func WithFile(in string) Option {
|
||||
return func(c *Config) {
|
||||
c.file = in
|
||||
}
|
||||
}
|
||||
|
||||
func WithFullPkg(in string) Option {
|
||||
return func(c *Config) {
|
||||
c.fullPkg = in
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfig(opts ...Option) Config {
|
||||
var cfg Config
|
||||
|
||||
cfg.Params = param.New()
|
||||
cfg.prefix = "Input"
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&cfg)
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
param.Params
|
||||
|
||||
methods []string
|
||||
prefix string
|
||||
suffix string
|
||||
fullPkg string
|
||||
pkg string
|
||||
file string
|
||||
buildTags string
|
||||
outName string
|
||||
}
|
||||
|
||||
func (c Config) BuildTags() string {
|
||||
return c.buildTags
|
||||
}
|
||||
|
||||
func (c Config) Pkg() string {
|
||||
if c.pkg == "" {
|
||||
if idx := strings.LastIndex(c.fullPkg, "/"); idx != -1 {
|
||||
c.pkg = c.fullPkg[idx+1:]
|
||||
}
|
||||
}
|
||||
|
||||
return c.pkg
|
||||
}
|
||||
|
||||
func (c Config) FullPkg() string {
|
||||
return c.fullPkg
|
||||
}
|
||||
|
||||
func (c Config) SkipContext() bool {
|
||||
return view.IsSkipContext(c.Params)
|
||||
}
|
||||
|
||||
func (c Config) Methods() []string {
|
||||
return c.methods
|
||||
}
|
||||
|
||||
func (c Config) Prefix() string {
|
||||
return c.prefix
|
||||
}
|
||||
|
||||
func (c Config) Suffix() string {
|
||||
return c.suffix
|
||||
}
|
||||
|
||||
func (c Config) File() string {
|
||||
return c.file
|
||||
}
|
||||
|
||||
func (c Config) OutName() string {
|
||||
return c.outName
|
||||
}
|
||||
|
||||
//go:embed tpl/*
|
||||
var tpls embed.FS
|
||||
|
||||
var initTpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tpl")).Lookup("init.go.tpl")
|
||||
|
||||
func Run(_ context.Context, cfg Config, w io.Writer, inputs ...Input) error {
|
||||
data := Data{
|
||||
Config: cfg,
|
||||
imp: pkg.NewImports(cfg.FullPkg()).Adds("fmt", "context", "gitoa.ru/go-4devs/config"),
|
||||
}
|
||||
|
||||
var buff bytes.Buffer
|
||||
|
||||
for _, in := range inputs {
|
||||
vi := view.NewViews(in.Method(), data.Params, in.Options(), view.WithKeys())
|
||||
|
||||
if err := render.Render(&buff, vi, data); err != nil {
|
||||
return fmt.Errorf("render:%w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := initTpl.Execute(w, data); err != nil {
|
||||
return fmt.Errorf("render base:%w", err)
|
||||
}
|
||||
|
||||
if _, err := io.Copy(w, &buff); err != nil {
|
||||
return fmt.Errorf("copy:%w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Config
|
||||
|
||||
imp *pkg.Imports
|
||||
}
|
||||
|
||||
func (f Data) Imports() []pkg.Import {
|
||||
return f.imp.Imports()
|
||||
}
|
||||
|
||||
func (f Data) StructName(name string) string {
|
||||
return f.Prefix() + FuncName(name) + f.Suffix()
|
||||
}
|
||||
|
||||
func (f Data) FuncName(in string) string {
|
||||
return FuncName(in)
|
||||
}
|
||||
|
||||
func (f Data) AddType(pkg string) (string, error) {
|
||||
short, err := f.imp.AddType(pkg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("data: %w", err)
|
||||
}
|
||||
|
||||
return short, nil
|
||||
}
|
||||
|
||||
func FuncName(name string) string {
|
||||
data := strings.Builder{}
|
||||
toUp := true
|
||||
|
||||
for _, char := range name {
|
||||
isLeter := unicode.IsLetter(char)
|
||||
isAllowed := isLeter || unicode.IsDigit(char)
|
||||
|
||||
switch {
|
||||
case isAllowed && !toUp:
|
||||
data.WriteRune(char)
|
||||
case !isAllowed:
|
||||
toUp = true
|
||||
case toUp:
|
||||
data.WriteString(strings.ToUpper(string(char)))
|
||||
|
||||
toUp = false
|
||||
}
|
||||
}
|
||||
|
||||
return data.String()
|
||||
}
|
||||
|
||||
func NewInput(method string, options config.Options) Input {
|
||||
return Input{
|
||||
method: method,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
options config.Options
|
||||
method string
|
||||
}
|
||||
|
||||
func (i Input) Method() string {
|
||||
return i.method
|
||||
}
|
||||
|
||||
func (i Input) Options() config.Options {
|
||||
return i.options
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package generate_test
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
"gitoa.ru/go-4devs/config/definition/group"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/definition/proto"
|
||||
)
|
||||
|
||||
type LogLevel string
|
||||
|
||||
func (l *LogLevel) UnmarshalText(in []byte) error {
|
||||
data := string(in)
|
||||
*l = LogLevel(data)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Configure(def *definition.Definition) {
|
||||
def.Add(
|
||||
option.String("test", "test string", view.WithSkipContext),
|
||||
group.New("user", "configure user",
|
||||
option.String("name", "name", option.Default("4devs")),
|
||||
option.String("pass", "password"),
|
||||
),
|
||||
|
||||
group.New("log", "configure logger",
|
||||
option.New("level", "log level", LogLevel("")),
|
||||
proto.New("service", "servise logger", option.New("level", "log level", LogLevel(""))),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func AliasName(name string) string {
|
||||
data := strings.Builder{}
|
||||
toUp := false
|
||||
|
||||
for _, char := range name {
|
||||
isLeter := unicode.IsLetter(char)
|
||||
isAllowed := isLeter || unicode.IsDigit(char)
|
||||
|
||||
switch {
|
||||
case isAllowed && !toUp:
|
||||
data.WriteRune(char)
|
||||
case !isAllowed && data.Len() > 0:
|
||||
toUp = true
|
||||
case toUp:
|
||||
data.WriteString(strings.ToUpper(string(char)))
|
||||
|
||||
toUp = false
|
||||
}
|
||||
}
|
||||
|
||||
return data.String()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package pkg
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrWrongFormat = errors.New("wrong format")
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
@@ -1,106 +0,0 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewImports(pkg string) *Imports {
|
||||
imp := Imports{
|
||||
data: make(map[string]string),
|
||||
pkg: pkg,
|
||||
}
|
||||
|
||||
return &imp
|
||||
}
|
||||
|
||||
type Imports struct {
|
||||
data map[string]string
|
||||
pkg string
|
||||
}
|
||||
|
||||
func (i *Imports) Imports() []Import {
|
||||
imports := make([]Import, 0, len(i.data))
|
||||
for name, alias := range i.data {
|
||||
imports = append(imports, Import{
|
||||
Package: name,
|
||||
Alias: alias,
|
||||
})
|
||||
}
|
||||
|
||||
return imports
|
||||
}
|
||||
|
||||
func (i *Imports) Short(fullType string) (string, error) {
|
||||
idx := strings.LastIndexByte(fullType, '.')
|
||||
if idx == -1 {
|
||||
return "", fmt.Errorf("%w: expect package.Type", ErrWrongFormat)
|
||||
}
|
||||
|
||||
if alias, ok := i.data[fullType[:idx]]; ok {
|
||||
return alias + fullType[idx:], nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("%w alias for pkg %v", ErrNotFound, fullType[:idx])
|
||||
}
|
||||
|
||||
func (i *Imports) AddType(fullType string) (string, error) {
|
||||
idx := strings.LastIndexByte(fullType, '.')
|
||||
if idx == -1 {
|
||||
return "", fmt.Errorf("%w: expect pckage.Type got %v", ErrWrongFormat, fullType)
|
||||
}
|
||||
|
||||
imp := i.Add(fullType[:idx])
|
||||
|
||||
if imp.Alias == "" {
|
||||
return fullType[idx+1:], nil
|
||||
}
|
||||
|
||||
return imp.Alias + fullType[idx:], nil
|
||||
}
|
||||
|
||||
func (i *Imports) Adds(pkgs ...string) *Imports {
|
||||
for _, pkg := range pkgs {
|
||||
i.Add(pkg)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *Imports) Add(pkg string) Import {
|
||||
if pkg == i.pkg {
|
||||
return Import{
|
||||
Alias: "",
|
||||
Package: pkg,
|
||||
}
|
||||
}
|
||||
|
||||
alias := pkg
|
||||
|
||||
if idx := strings.LastIndexByte(pkg, '/'); idx != -1 {
|
||||
alias = AliasName(pkg[idx+1:])
|
||||
}
|
||||
|
||||
if al, ok := i.data[pkg]; ok {
|
||||
return Import{Package: pkg, Alias: al}
|
||||
}
|
||||
|
||||
for _, al := range i.data {
|
||||
if al == alias {
|
||||
alias += strconv.Itoa(len(i.data))
|
||||
}
|
||||
}
|
||||
|
||||
i.data[pkg] = alias
|
||||
|
||||
return Import{
|
||||
Alias: alias,
|
||||
Package: pkg,
|
||||
}
|
||||
}
|
||||
|
||||
type Import struct {
|
||||
Alias string
|
||||
Package string
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var cache = sync.Map{}
|
||||
|
||||
func ByPath(ctx context.Context, fname string, isDir bool) (string, error) {
|
||||
if !filepath.IsAbs(fname) {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
fname = filepath.Join(pwd, fname)
|
||||
}
|
||||
|
||||
goModPath, _ := goModPath(ctx, fname, isDir)
|
||||
if strings.Contains(goModPath, "go.mod") {
|
||||
pkgPath, err := getPkgPathFromGoMod(fname, isDir, goModPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return pkgPath, nil
|
||||
}
|
||||
|
||||
return getPkgPathFromGOPATH(fname, isDir)
|
||||
}
|
||||
|
||||
// empty if no go.mod, GO111MODULE=off or go without go modules support.
|
||||
func goModPath(ctx context.Context, fname string, isDir bool) (string, error) {
|
||||
root := fname
|
||||
if !isDir {
|
||||
root = filepath.Dir(fname)
|
||||
}
|
||||
|
||||
var modPath string
|
||||
|
||||
loadModPath, ok := cache.Load(root)
|
||||
if ok {
|
||||
modPath, _ = loadModPath.(string)
|
||||
|
||||
return modPath, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
cache.Store(root, modPath)
|
||||
}()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "go", "env", "GOMOD")
|
||||
cmd.Dir = root
|
||||
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
modPath = string(bytes.TrimSpace(stdout))
|
||||
|
||||
return modPath, nil
|
||||
}
|
||||
|
||||
func getPkgPathFromGoMod(fname string, isDir bool, goModPath string) (string, error) {
|
||||
modulePath := getModulePath(goModPath)
|
||||
if modulePath == "" {
|
||||
return "", fmt.Errorf("c%w module path from %s", ErrNotFound, goModPath)
|
||||
}
|
||||
|
||||
rel := path.Join(modulePath, filePathToPackagePath(strings.TrimPrefix(fname, filepath.Dir(goModPath))))
|
||||
|
||||
if !isDir {
|
||||
return path.Dir(rel), nil
|
||||
}
|
||||
|
||||
return path.Clean(rel), nil
|
||||
}
|
||||
|
||||
func getModulePath(goModPath string) string {
|
||||
var pkgPath string
|
||||
|
||||
cacheOkgPath, ok := cache.Load(goModPath)
|
||||
if ok {
|
||||
pkgPath, _ = cacheOkgPath.(string)
|
||||
|
||||
return pkgPath
|
||||
}
|
||||
|
||||
defer func() {
|
||||
cache.Store(goModPath, pkgPath)
|
||||
}()
|
||||
|
||||
data, err := os.ReadFile(goModPath)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
pkgPath = modulePath(data)
|
||||
|
||||
return pkgPath
|
||||
}
|
||||
|
||||
func getPkgPathFromGOPATH(fname string, isDir bool) (string, error) {
|
||||
gopath := os.Getenv("GOPATH")
|
||||
if gopath == "" {
|
||||
gopath = build.Default.GOPATH
|
||||
}
|
||||
|
||||
for _, p := range strings.Split(gopath, string(filepath.ListSeparator)) {
|
||||
prefix := filepath.Join(p, "src") + string(filepath.Separator)
|
||||
|
||||
rel, err := filepath.Rel(prefix, fname)
|
||||
if err == nil && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
if !isDir {
|
||||
return path.Dir(filePathToPackagePath(rel)), nil
|
||||
}
|
||||
|
||||
return path.Clean(filePathToPackagePath(rel)), nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("%w: file '%v' is not in GOPATH '%v'", ErrNotFound, fname, gopath)
|
||||
}
|
||||
|
||||
func filePathToPackagePath(path string) string {
|
||||
return filepath.ToSlash(path)
|
||||
}
|
||||
|
||||
var (
|
||||
slashSlash = []byte("//")
|
||||
moduleStr = []byte("module")
|
||||
)
|
||||
|
||||
// modulePath returns the module path from the gomod file text.
|
||||
// If it cannot find a module path, it returns an empty string.
|
||||
// It is tolerant of unrelated problems in the go.mod file.
|
||||
func modulePath(mod []byte) string {
|
||||
for len(mod) > 0 {
|
||||
line := mod
|
||||
|
||||
mod = nil
|
||||
if i := bytes.IndexByte(line, '\n'); i >= 0 {
|
||||
line, mod = line[:i], line[i+1:]
|
||||
}
|
||||
|
||||
if i := bytes.Index(line, slashSlash); i >= 0 {
|
||||
line = line[:i]
|
||||
}
|
||||
|
||||
line = bytes.TrimSpace(line)
|
||||
if !bytes.HasPrefix(line, moduleStr) {
|
||||
continue
|
||||
}
|
||||
|
||||
line = line[len(moduleStr):]
|
||||
n := len(line)
|
||||
|
||||
line = bytes.TrimSpace(line)
|
||||
if len(line) == n || len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if line[0] == '"' || line[0] == '`' {
|
||||
p, err := strconv.Unquote(string(line))
|
||||
if err != nil {
|
||||
return "" // malformed quoted string or multiline module path
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
return string(line)
|
||||
}
|
||||
|
||||
return "" // missing module path
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/definition/generate/pkg"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
)
|
||||
|
||||
func NewViewData(render Rendering, view view.View) ViewData {
|
||||
return ViewData{
|
||||
Rendering: render,
|
||||
View: view,
|
||||
}
|
||||
}
|
||||
|
||||
type ViewData struct {
|
||||
Rendering
|
||||
view.View
|
||||
}
|
||||
|
||||
func (d ViewData) StructName() string {
|
||||
return d.Rendering.StructName(d.View.ParentName() + "_" + d.View.Name())
|
||||
}
|
||||
|
||||
func (d ViewData) FuncName() string {
|
||||
return d.Rendering.FuncName(d.View.FuncName())
|
||||
}
|
||||
|
||||
func (d ViewData) ParentName() string {
|
||||
name := d.View.ParentName()
|
||||
if name == "" {
|
||||
name = d.Name()
|
||||
}
|
||||
|
||||
return d.Rendering.StructName(name)
|
||||
}
|
||||
|
||||
func (d ViewData) Name() string {
|
||||
return pkg.AliasName(d.View.Name())
|
||||
}
|
||||
|
||||
func (d ViewData) Type() string {
|
||||
return Type(d)
|
||||
}
|
||||
|
||||
func (d ViewData) Keys(parent string) string {
|
||||
return Keys(append(d.View.Keys(), d.Name()), parent)
|
||||
}
|
||||
|
||||
func (d ViewData) Value(name, val string) string {
|
||||
return Value(name, val, d)
|
||||
}
|
||||
|
||||
func (d ViewData) Default(name string) string {
|
||||
return Data(d.View.Default(), name, d)
|
||||
}
|
||||
|
||||
type Rendering interface {
|
||||
StructName(name string) string
|
||||
FuncName(name string) string
|
||||
AddType(pkg string) (string, error)
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package render
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
@@ -1,34 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition/generate/pkg"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
)
|
||||
|
||||
func Keys(keys []string, val string) string {
|
||||
if len(keys) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var out strings.Builder
|
||||
|
||||
for idx, one := range keys {
|
||||
if key.IsWild(one) {
|
||||
out.WriteString(val)
|
||||
out.WriteString(".")
|
||||
out.WriteString(pkg.AliasName(one))
|
||||
} else {
|
||||
out.WriteString("\"")
|
||||
out.WriteString(one)
|
||||
out.WriteString("\"")
|
||||
}
|
||||
|
||||
if len(keys)-1 != idx {
|
||||
out.WriteString(", ")
|
||||
}
|
||||
}
|
||||
|
||||
return out.String()
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
"gitoa.ru/go-4devs/config/definition/group"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/definition/proto"
|
||||
)
|
||||
|
||||
type Execute func(w io.Writer, vi view.View, rnd Rendering) error
|
||||
|
||||
var randders = map[reflect.Type]Execute{
|
||||
reflect.TypeFor[*definition.Definition](): Template(defTpl),
|
||||
reflect.TypeFor[group.Group](): Template(groupTpl),
|
||||
reflect.TypeFor[option.Option](): Template(optTpl),
|
||||
reflect.TypeFor[proto.Proto](): Template(protoTpl),
|
||||
}
|
||||
|
||||
func Renders() map[reflect.Type]Execute {
|
||||
return randders
|
||||
}
|
||||
|
||||
func Add(rt reflect.Type, fn Execute) {
|
||||
randders[rt] = fn
|
||||
}
|
||||
|
||||
func Render(w io.Writer, view view.View, data Rendering) error {
|
||||
rnd, ok := randders[view.Kind()]
|
||||
if !ok {
|
||||
return fmt.Errorf("%w:%v", ErrNotFound, view.Kind())
|
||||
}
|
||||
|
||||
if err := rnd(w, view, data); err != nil {
|
||||
return fmt.Errorf("render:%v, err:%w", view.Kind(), err)
|
||||
}
|
||||
|
||||
for _, ch := range view.Views() {
|
||||
if err := Render(w, ch, data); err != nil {
|
||||
return fmt.Errorf("render[%v]:%w", ch.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
)
|
||||
|
||||
//go:embed tpl/*
|
||||
var tplFS embed.FS
|
||||
|
||||
var (
|
||||
tpls = template.Must(
|
||||
template.New("tpls").
|
||||
Funcs(template.FuncMap{
|
||||
"trim": strings.Trim,
|
||||
}).
|
||||
ParseFS(tplFS, "tpl/*.go.tpl"),
|
||||
)
|
||||
defTpl = tpls.Lookup("definition.go.tpl")
|
||||
groupTpl = tpls.Lookup("group.go.tpl")
|
||||
protoTpl = tpls.Lookup("proto.go.tpl")
|
||||
optTpl = template.Must(
|
||||
template.New("opt").ParseFS(tplFS, "tpl/option/option.go.tpl"),
|
||||
).Lookup("option.go.tpl")
|
||||
parceTpls = template.Must(template.New("data").ParseFS(tplFS, "tpl/data/*.go.tpl"))
|
||||
)
|
||||
|
||||
func Template(tpl *template.Template) Execute {
|
||||
return func(w io.Writer, v view.View, rnd Rendering) error {
|
||||
if err := tpl.Execute(w, NewViewData(rnd, v)); err != nil {
|
||||
return fmt.Errorf("template[%v]:%w", tpl.Name(), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{{block "Any" . }}
|
||||
return {{.ValName}}.Any(), nil
|
||||
{{end}}
|
||||
@@ -1,8 +0,0 @@
|
||||
{{ block "FlagValue" . -}}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read [%v]:%w",[]string{ {{- .Keys "i" -}} }, perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.Set(pval)
|
||||
{{- end }}
|
||||
@@ -1,3 +0,0 @@
|
||||
{{block "Parse" .}}
|
||||
return {{.ValName}}.Parse{{ .FuncType}}()
|
||||
{{end}}
|
||||
@@ -1,3 +0,0 @@
|
||||
{{ block "ScanValue" . -}}
|
||||
return {{.Value}}, {{.Value}}.Scan({{.ValName}}.Any())
|
||||
{{- end }}
|
||||
@@ -1,8 +0,0 @@
|
||||
{{block "UnmarshalJSON" . -}}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read [%v]:%w", []string{ {{- .Keys "i" -}} }, perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalJSON([]byte(pval))
|
||||
{{- end }}
|
||||
@@ -1,8 +0,0 @@
|
||||
{{ block "UnmarshalText" . -}}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read [%v]:%w", []string{ {{- .Keys "i" -}} }, perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalText([]byte(pval))
|
||||
{{- end }}
|
||||
@@ -1,25 +0,0 @@
|
||||
func With{{.StructName}}Log(log func(context.Context, string, ...any)) func(*{{.StructName}}) {
|
||||
return func(ci *{{.StructName}}) {
|
||||
ci.log = log
|
||||
}
|
||||
}
|
||||
|
||||
func New{{.StructName}}(prov config.Provider, opts ...func(*{{.StructName}})) {{.StructName}} {
|
||||
i := {{.StructName}}{
|
||||
Provider: prov,
|
||||
log: func(_ context.Context, format string, args ...any) {
|
||||
fmt.Printf(format, args...)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&i)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
config.Provider
|
||||
log func(context.Context, string, ...any)
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
type {{.StructName}} struct {
|
||||
{{.ParentName}}
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.ParentName}}) {{.FuncName}}() {{.StructName}} {
|
||||
return {{.StructName}}{i}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.ParentName}}) read{{.FuncName}}(ctx context.Context) (v {{.Type}},e error) {
|
||||
val, err := i.Value(ctx, {{ .Keys "i" }})
|
||||
if err != nil {
|
||||
{{- if .HasDefault }}
|
||||
i.log({{ if not .SkipContext }}context.Background(){{else}}ctx{{ end }}, "read [%v]: %v",[]string{ {{- .Keys "i" -}} }, err)
|
||||
|
||||
{{ .Default "val" -}}
|
||||
{{ else }}
|
||||
return v, fmt.Errorf("read [%v]:%w",[]string{ {{- .Keys "i" -}} }, err)
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
{{ .Value "val" "v" }}
|
||||
}
|
||||
|
||||
// Read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.ParentName}}) Read{{.FuncName}}({{if not .SkipContext}} ctx context.Context {{end}}) ({{.Type}}, error) {
|
||||
return i.read{{.FuncName}}({{if .SkipContext}}context.Background(){{else}}ctx{{end}})
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.ParentName}}) {{.FuncName}}({{if not .SkipContext}} ctx context.Context {{end}}) {{.Type}} {
|
||||
val, err := i.read{{.FuncName}}({{ if .SkipContext }}context.Background(){{else}}ctx{{ end }})
|
||||
if err != nil {
|
||||
i.log({{ if .SkipContext }}context.Background(){{else}}ctx{{ end }}, "get [%v]: %v",[]string{ {{- .Keys "i" -}} }, err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
type {{.StructName}} struct {
|
||||
{{.ParentName}}
|
||||
{{ .Name }} string
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.ParentName}}) {{.FuncName}}(key string) {{.StructName}} {
|
||||
return {{.StructName}}{i,key}
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Value(name, val string, data ViewData) string {
|
||||
rnd := renderType(data)
|
||||
|
||||
res, err := rnd(ValueData{ValName: name, Value: val, ViewData: data})
|
||||
if err != nil {
|
||||
return fmt.Sprintf("render value:%v", err)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func Type(data ViewData) string {
|
||||
dt := data.View.Type()
|
||||
rtype := reflect.TypeOf(dt)
|
||||
|
||||
slice := ""
|
||||
if rtype.Kind() == reflect.Slice {
|
||||
slice = "[]"
|
||||
rtype = rtype.Elem()
|
||||
}
|
||||
|
||||
short := rtype.Name()
|
||||
|
||||
if rtype.PkgPath() != "" {
|
||||
var err error
|
||||
|
||||
short, err = data.AddType(rtype.PkgPath() + "." + rtype.Name())
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return slice + short
|
||||
}
|
||||
|
||||
func Data(val any, name string, view ViewData) string {
|
||||
fn := renderData(view)
|
||||
|
||||
data, err := fn(val, ValueData{ValName: name, Value: "", ViewData: view})
|
||||
if err != nil {
|
||||
return fmt.Sprintf("render dara:%v", err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func renderDataTime(val any, _ ValueData) (string, error) {
|
||||
data, _ := val.(time.Time)
|
||||
|
||||
return fmt.Sprintf("time.Parse(%q,time.RFC3339Nano)", data.Format(time.RFC3339Nano)), nil
|
||||
}
|
||||
|
||||
func renderDataDuration(val any, _ ValueData) (string, error) {
|
||||
data, _ := val.(time.Duration)
|
||||
|
||||
return fmt.Sprintf("time.ParseDuration(%q)", data), nil
|
||||
}
|
||||
|
||||
func renderDataUnmarhal(val any, view ValueData) (string, error) {
|
||||
res, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("render data unmarshal:%w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("return {{.%[1]s}}, {{.%[1]s}}.UnmarshalJSON(%q)", view.ValName, res), nil
|
||||
}
|
||||
|
||||
func renderDataUnmarhalText(val any, view ValueData) (string, error) {
|
||||
res, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("render data unmarshal:%w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("return {{.%[1]s}}, {{.%[1]s}}.UnmarshalText(%s)", view.ValName, res), nil
|
||||
}
|
||||
|
||||
func renderDataFlag(val any, view ValueData) (string, error) {
|
||||
return fmt.Sprintf("return {{.%[1]s}}, {{.%[1]s}}.Set(%[2]q)", view.ValName, val), nil
|
||||
}
|
||||
|
||||
func renderType(view ViewData) func(data ValueData) (string, error) {
|
||||
return dataRender(view).Type
|
||||
}
|
||||
|
||||
func renderData(view ViewData) func(in any, data ValueData) (string, error) {
|
||||
return dataRender(view).Value
|
||||
}
|
||||
|
||||
func dataRender(view ViewData) DataRender {
|
||||
data := view.View.Type()
|
||||
vtype := reflect.TypeOf(data)
|
||||
|
||||
if vtype.Kind() == reflect.Slice {
|
||||
return render[reflect.TypeFor[json.Unmarshaler]()]
|
||||
}
|
||||
|
||||
if h, ok := render[vtype]; ok {
|
||||
return h
|
||||
}
|
||||
|
||||
if vtype.Kind() != reflect.Interface && vtype.Kind() != reflect.Ptr {
|
||||
vtype = reflect.PointerTo(vtype)
|
||||
}
|
||||
|
||||
for extypes := range render {
|
||||
if extypes == nil || extypes.Kind() != reflect.Interface {
|
||||
continue
|
||||
}
|
||||
|
||||
if vtype.Implements(extypes) {
|
||||
return render[extypes]
|
||||
}
|
||||
}
|
||||
|
||||
return render[reflect.TypeOf((any)(nil))]
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var render = map[reflect.Type]DataRender{
|
||||
reflect.TypeFor[encoding.TextUnmarshaler](): NewDataRender(unmarshalTextType, renderDataUnmarhalText),
|
||||
reflect.TypeFor[json.Unmarshaler](): NewDataRender(unmarshalType, renderDataUnmarhal),
|
||||
reflect.TypeFor[flag.Value](): NewDataRender(flagType, renderDataFlag),
|
||||
reflect.TypeFor[sql.Scanner](): NewDataRender(scanType, nil),
|
||||
reflect.TypeFor[int](): NewDataRender(internalType, nil),
|
||||
reflect.TypeFor[int64](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[bool](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[string](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[float64](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[uint](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[int64](): NewDataRender(internalType, anyValue),
|
||||
reflect.TypeFor[time.Duration](): NewDataRender(durationType, renderDataDuration),
|
||||
reflect.TypeFor[time.Time](): NewDataRender(timeType, renderDataTime),
|
||||
reflect.TypeOf((any)(nil)): NewDataRender(anyType, anyValue),
|
||||
}
|
||||
|
||||
func timeType(data ValueData) (string, error) {
|
||||
return fmt.Sprintf("return %s.ParseTime()", data.ValName), nil
|
||||
}
|
||||
|
||||
func durationType(data ValueData) (string, error) {
|
||||
return fmt.Sprintf("return %s.ParseDuration()", data.ValName), nil
|
||||
}
|
||||
|
||||
func scanType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.Lookup("scan_value.go.tpl").Execute(&b, data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute scan value:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func flagType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.Lookup("flag_value.go.tpl").Execute(&b, data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute flag value:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func anyType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.ExecuteTemplate(&b, "any.go.tpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unmarshal execute any.go.tpl:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func anyValue(data any, _ ValueData) (string, error) {
|
||||
return fmt.Sprintf("return %#v, nil", data), nil
|
||||
}
|
||||
|
||||
func unmarshalType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.ExecuteTemplate(&b, "unmarshal_json.go.tpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unmarshal execute unmarshal_json.go.tpl:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func unmarshalTextType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.Lookup("unmarshal_text.go.tpl").Execute(&b, data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute unmarshal text:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func internalType(data ValueData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := parceTpls.Lookup("parse.go.tpl").Execute(&b, data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("internal execute parce.go.tpl:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
type ValueData struct {
|
||||
ViewData
|
||||
|
||||
ValName string
|
||||
Value string
|
||||
}
|
||||
|
||||
func (v ValueData) FuncType() string {
|
||||
name := reflect.TypeOf(v.ViewData.Type()).Name()
|
||||
|
||||
return v.Rendering.FuncName(name)
|
||||
}
|
||||
|
||||
type DataRender struct {
|
||||
renderType func(data ValueData) (string, error)
|
||||
renderValue func(data any, view ValueData) (string, error)
|
||||
}
|
||||
|
||||
func (d DataRender) Type(data ValueData) (string, error) {
|
||||
return d.renderType(data)
|
||||
}
|
||||
|
||||
func (d DataRender) Value(data any, view ValueData) (string, error) {
|
||||
return d.renderValue(data, view)
|
||||
}
|
||||
|
||||
func NewDataRender(rendeType func(data ValueData) (string, error), renderValue func(data any, view ValueData) (string, error)) DataRender {
|
||||
if rendeType == nil {
|
||||
rendeType = anyType
|
||||
}
|
||||
|
||||
if renderValue == nil {
|
||||
renderValue = anyValue
|
||||
}
|
||||
|
||||
return DataRender{
|
||||
renderType: rendeType,
|
||||
renderValue: renderValue,
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package render_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition/generate/render"
|
||||
"gitoa.ru/go-4devs/config/definition/generate/view"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
)
|
||||
|
||||
type flagValue int
|
||||
|
||||
func (f flagValue) String() string {
|
||||
return strconv.Itoa(int(f))
|
||||
}
|
||||
|
||||
func (f *flagValue) Set(in string) error {
|
||||
data, err := strconv.Atoi(in)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
*f = flagValue(data)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestValue_FlagType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const ex = `pval, perr := val.ParseString()
|
||||
if perr != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w",[]string{"flagValue"}, perr)
|
||||
}
|
||||
|
||||
return v, v.Set(pval)`
|
||||
|
||||
viewData := render.NewViewData(nil, view.NewView(option.New("flag_value", "flag desc", flagValue(0)), nil))
|
||||
result := render.Value("val", "v", viewData)
|
||||
|
||||
if result != ex {
|
||||
t.Errorf("failed render flag type ex:%s, res:%s", ex, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestData_Flag(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const ex = `return {{.val}}, {{.val}}.Set("42")`
|
||||
|
||||
viewData := render.NewViewData(nil, view.NewView(option.New("flag_value", "flag desc", flagValue(0)), nil))
|
||||
result := render.Data(flagValue(42), "val", viewData)
|
||||
|
||||
if result != ex {
|
||||
t.Errorf("failed render flag value ex:%s, res:%s", ex, result)
|
||||
}
|
||||
}
|
||||
|
||||
type scanValue int
|
||||
|
||||
func (s *scanValue) Scan(src any) error {
|
||||
res, _ := src.(string)
|
||||
|
||||
data, err := strconv.Atoi(res)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
*s = scanValue(data)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestValue_Scan(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const ex = `return v, v.Scan(val.Any())`
|
||||
|
||||
viewData := render.NewViewData(nil, view.NewView(option.New("scan_value", "scan desc", scanValue(42)), nil))
|
||||
result := render.Value("val", "v", viewData)
|
||||
|
||||
if result != ex {
|
||||
t.Errorf("failed render flag value ex:%s, res:%s", ex, result)
|
||||
}
|
||||
}
|
||||
|
||||
type textData string
|
||||
|
||||
func (j *textData) UnmarshalText(in []byte) error {
|
||||
val := string(in)
|
||||
|
||||
*j = textData(val)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestData_UnmarshalText(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const ex = `return {{.val}}, {{.val}}.UnmarshalText("4devs")`
|
||||
|
||||
data := textData("4devs")
|
||||
viewData := render.NewViewData(nil, view.NewView(option.New("tvalue", "unmarshal text desc", textData("")), nil))
|
||||
result := render.Data(data, "val", viewData)
|
||||
|
||||
if result != ex {
|
||||
t.Errorf("failed render flag value ex:%s, res:%s", ex, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValue_UnmarshalText(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const ex = `pval, perr := val.ParseString()
|
||||
if perr != nil {
|
||||
return v, fmt.Errorf("read [%v]:%w", []string{"tvalue"}, perr)
|
||||
}
|
||||
|
||||
return v, v.UnmarshalText([]byte(pval))`
|
||||
|
||||
viewData := render.NewViewData(nil, view.NewView(option.New("tvalue", "unmarshal text desc", textData("")), nil))
|
||||
result := render.Value("val", "v", viewData)
|
||||
|
||||
if result != ex {
|
||||
t.Errorf("failed render flag value ex:%s, res:%s", ex, result)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
|
||||
package {{.Pkg}}
|
||||
|
||||
import (
|
||||
{{range .Imports}}
|
||||
{{- .Alias }} "{{ .Package }}"
|
||||
{{end}}
|
||||
)
|
||||
@@ -1,191 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
viewParamFunctName key = iota + 1
|
||||
viewParamSkipContext
|
||||
)
|
||||
|
||||
func WithSkipContext(p param.Params) param.Params {
|
||||
return param.With(p, viewParamSkipContext, true)
|
||||
}
|
||||
|
||||
func WithContext(p param.Params) param.Params {
|
||||
return param.With(p, viewParamSkipContext, false)
|
||||
}
|
||||
|
||||
func IsSkipContext(p param.Params) bool {
|
||||
data, has := p.Param(viewParamSkipContext)
|
||||
|
||||
if has {
|
||||
skip, ok := data.(bool)
|
||||
|
||||
return ok && skip
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
type Option func(*View)
|
||||
|
||||
func WithParent(name string) Option {
|
||||
return func(v *View) {
|
||||
v.parent = name
|
||||
}
|
||||
}
|
||||
|
||||
func WithKeys(keys ...string) Option {
|
||||
return func(v *View) {
|
||||
v.keys = keys
|
||||
}
|
||||
}
|
||||
|
||||
func NewViews(name string, get param.Params, option config.Options, opts ...Option) View {
|
||||
view := newView(name, get, option, opts...)
|
||||
|
||||
for _, op := range option.Options() {
|
||||
view.children = append(view.children, NewView(op, get, WithParent(name)))
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
type IOption any
|
||||
|
||||
func newView(name string, get param.Params, in any, opts ...Option) View {
|
||||
vi := View{
|
||||
kind: reflect.TypeOf(in),
|
||||
name: name,
|
||||
Params: get,
|
||||
dtype: param.Type(get),
|
||||
children: nil,
|
||||
keys: nil,
|
||||
parent: "",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&vi)
|
||||
}
|
||||
|
||||
return vi
|
||||
}
|
||||
|
||||
func NewView(opt config.Option, get param.Params, opts ...Option) View {
|
||||
vi := newView(opt.Name(), param.Chain(get, opt), opt, opts...)
|
||||
|
||||
if data, ok := opt.(config.Group); ok {
|
||||
for _, chi := range data.Options() {
|
||||
vi.children = append(vi.children, NewView(
|
||||
chi,
|
||||
param.Chain(vi.Params, chi),
|
||||
WithParent(vi.ParentName()+"_"+opt.Name()),
|
||||
WithKeys(append(vi.keys, opt.Name())...),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
return vi
|
||||
}
|
||||
|
||||
type View struct {
|
||||
param.Params
|
||||
|
||||
children []View
|
||||
keys []string
|
||||
kind reflect.Type
|
||||
name string
|
||||
parent string
|
||||
dtype any
|
||||
}
|
||||
|
||||
func (v View) Types() []any {
|
||||
types := make([]any, 0)
|
||||
if v.dtype != nil {
|
||||
types = append(types, v.dtype)
|
||||
}
|
||||
|
||||
for _, child := range v.children {
|
||||
types = append(types, child.Types()...)
|
||||
}
|
||||
|
||||
return types
|
||||
}
|
||||
|
||||
func (v View) Kind() reflect.Type {
|
||||
return v.kind
|
||||
}
|
||||
|
||||
func (v View) Views() []View {
|
||||
return v.children
|
||||
}
|
||||
|
||||
func (v View) Param(key any) string {
|
||||
data, ok := v.Params.Param(key)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
if res, ok := data.(string); ok {
|
||||
return res
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v", data)
|
||||
}
|
||||
|
||||
func (v View) SkipContext() bool {
|
||||
return IsSkipContext(v.Params)
|
||||
}
|
||||
|
||||
func (v View) Name() string {
|
||||
return v.name
|
||||
}
|
||||
|
||||
func (v View) Keys() []string {
|
||||
return v.keys
|
||||
}
|
||||
|
||||
func (v View) Type() any {
|
||||
return v.dtype
|
||||
}
|
||||
|
||||
func (v View) FuncName() string {
|
||||
data, ok := v.Params.Param(viewParamFunctName)
|
||||
name, valid := data.(string)
|
||||
|
||||
if !ok || !valid {
|
||||
return v.name
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func (v View) ParentName() string {
|
||||
return v.parent
|
||||
}
|
||||
|
||||
func (v View) Description() string {
|
||||
return option.DataDescription(v.Params)
|
||||
}
|
||||
|
||||
func (v View) Default() any {
|
||||
data, ok := option.DataDefaut(v.Params)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func (v View) HasDefault() bool {
|
||||
return option.HasDefaut(v.Params)
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
var _ config.Group = New("", "")
|
||||
|
||||
func New(name, desc string, opts ...config.Option) Group {
|
||||
group := Group{
|
||||
name: name,
|
||||
opts: opts,
|
||||
Params: param.New(option.Description(desc)),
|
||||
}
|
||||
|
||||
return group
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
param.Params
|
||||
|
||||
name string
|
||||
opts []config.Option
|
||||
}
|
||||
|
||||
func (g Group) Name() string {
|
||||
return g.name
|
||||
}
|
||||
|
||||
func (g Group) Options() []config.Option {
|
||||
return g.opts
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Key []string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (o Error) Error() string {
|
||||
return fmt.Sprintf("%s: %s", o.Key, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Is(err error) bool {
|
||||
return errors.Is(err, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Unwrap() error {
|
||||
return o.Err
|
||||
}
|
||||
|
||||
func Err(err error, key []string) Error {
|
||||
return Error{
|
||||
Key: key,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
var _ config.Option = New("", "", nil)
|
||||
|
||||
func New(name, desc string, vtype any, opts ...param.Option) Option {
|
||||
opts = append(opts, Description(desc), WithType(vtype))
|
||||
res := Option{
|
||||
name: name,
|
||||
Params: param.New(opts...),
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
param.Params
|
||||
|
||||
name string
|
||||
}
|
||||
|
||||
func (o Option) Name() string {
|
||||
return o.name
|
||||
}
|
||||
|
||||
func String(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, "", opts...)
|
||||
}
|
||||
|
||||
func Bool(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, false, opts...)
|
||||
}
|
||||
|
||||
func Duration(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, time.Duration(0), opts...)
|
||||
}
|
||||
|
||||
func Float64(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, float64(0), opts...)
|
||||
}
|
||||
|
||||
func Int(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, int(0), opts...)
|
||||
}
|
||||
|
||||
func Int64(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, int64(0), opts...)
|
||||
}
|
||||
|
||||
func Time(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, time.Time{}, opts...)
|
||||
}
|
||||
|
||||
func Uint(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, uint(0), opts...)
|
||||
}
|
||||
|
||||
func Uint64(name, descriontion string, opts ...param.Option) Option {
|
||||
return New(name, descriontion, uint64(0), opts...)
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
paramHidden key = iota + 1
|
||||
paramDefault
|
||||
paramDesc
|
||||
paramRequired
|
||||
paramSlice
|
||||
paramBool
|
||||
paramPos
|
||||
paramShort
|
||||
)
|
||||
|
||||
func Short(in rune) param.Option {
|
||||
return func(v param.Params) param.Params {
|
||||
return param.With(v, paramShort, string(in))
|
||||
}
|
||||
}
|
||||
|
||||
func ParamShort(fn param.Params) (string, bool) {
|
||||
data, ok := param.String(fn, paramShort)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func HasShort(short string) param.Has {
|
||||
return func(fn param.Params) bool {
|
||||
data, ok := param.String(fn, paramShort)
|
||||
|
||||
return ok && data == short
|
||||
}
|
||||
}
|
||||
|
||||
func WithType(in any) param.Option {
|
||||
return func(v param.Params) param.Params {
|
||||
out := param.WithType(in)(v)
|
||||
if _, ok := in.(bool); ok {
|
||||
return param.With(out, paramBool, ok)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
func Position(pos uint64) param.Option {
|
||||
return func(p param.Params) param.Params {
|
||||
return param.With(p, paramPos, pos)
|
||||
}
|
||||
}
|
||||
|
||||
func Hidden(v param.Params) param.Params {
|
||||
return param.With(v, paramHidden, true)
|
||||
}
|
||||
|
||||
func Required(v param.Params) param.Params {
|
||||
return param.With(v, paramRequired, true)
|
||||
}
|
||||
|
||||
func Slice(v param.Params) param.Params {
|
||||
return param.With(v, paramSlice, true)
|
||||
}
|
||||
|
||||
func Default(in any) param.Option {
|
||||
return func(v param.Params) param.Params {
|
||||
return param.With(v, paramDefault, in)
|
||||
}
|
||||
}
|
||||
|
||||
func Description(in string) param.Option {
|
||||
return func(v param.Params) param.Params {
|
||||
return param.With(v, paramDesc, in)
|
||||
}
|
||||
}
|
||||
|
||||
func HasDefaut(fn param.Params) bool {
|
||||
_, ok := fn.Param(paramDefault)
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func DataPosition(fn param.Params) (uint64, bool) {
|
||||
return param.Uint64(paramPos, fn)
|
||||
}
|
||||
|
||||
func DataDefaut(fn param.Params) (any, bool) {
|
||||
data, ok := fn.Param(paramDefault)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func IsSlice(fn param.Params) bool {
|
||||
data, ok := param.Bool(paramSlice, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsBool(fn param.Params) bool {
|
||||
data, ok := param.Bool(paramBool, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsHidden(fn param.Params) bool {
|
||||
data, ok := param.Bool(paramHidden, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsRequired(fn param.Params) bool {
|
||||
data, ok := param.Bool(paramRequired, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func DataDescription(fn param.Params) string {
|
||||
data, _ := param.String(fn, paramDesc)
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
var _ config.Group = New("", "")
|
||||
|
||||
func New(name string, desc string, opts ...config.Option) Proto {
|
||||
return Proto{
|
||||
name: key.Wild(name),
|
||||
opts: opts,
|
||||
Params: param.New(option.Description(desc)),
|
||||
}
|
||||
}
|
||||
|
||||
type Proto struct {
|
||||
param.Params
|
||||
|
||||
opts []config.Option
|
||||
name string
|
||||
}
|
||||
|
||||
func (p Proto) Options() []config.Option {
|
||||
return p.opts
|
||||
}
|
||||
|
||||
func (p Proto) Name() string {
|
||||
return p.name
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
vault:
|
||||
container_name: vault
|
||||
image: vault:1.13.3
|
||||
image: vault:latest
|
||||
cap_add:
|
||||
- IPC_LOCK
|
||||
ports:
|
||||
@@ -9,11 +10,8 @@ services:
|
||||
environment:
|
||||
VAULT_DEV_ROOT_TOKEN_ID: "dev"
|
||||
etcd:
|
||||
container_name: etcd
|
||||
image: quay.io/coreos/etcd:v3.6.7
|
||||
image: bitnami/etcd
|
||||
environment:
|
||||
ALLOW_NONE_AUTHENTICATION: "yes"
|
||||
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
|
||||
ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379"
|
||||
ports:
|
||||
- "2379:2379"
|
||||
20
error.go
20
error.go
@@ -1,20 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalidValue = errors.New("invalid value")
|
||||
ErrUnknowType = errors.New("unknow type")
|
||||
ErrInitFactory = errors.New("init factory")
|
||||
ErrStopWatch = errors.New("stop watch")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrValueNotFound = fmt.Errorf("value %w", ErrNotFound)
|
||||
ErrToManyArgs = errors.New("to many args")
|
||||
ErrWrongType = errors.New("wrong type")
|
||||
ErrInvalidName = errors.New("ivalid name")
|
||||
ErrUnexpectedType = errors.New("unexpected type")
|
||||
ErrRequired = errors.New("required")
|
||||
ErrVariableNotFound = errors.New("variable not found")
|
||||
ErrInvalidValue = errors.New("invalid value")
|
||||
ErrUnknowType = errors.New("unknow type")
|
||||
ErrInitFactory = errors.New("init factory")
|
||||
)
|
||||
|
||||
98
factory.go
98
factory.go
@@ -1,98 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func WrapFactory(fn Factory, prov Provider) *WrapProvider {
|
||||
return &WrapProvider{
|
||||
factory: func(ctx context.Context) (Provider, error) {
|
||||
return fn(ctx, prov)
|
||||
},
|
||||
mu: sync.Mutex{},
|
||||
done: 0,
|
||||
provider: nil,
|
||||
}
|
||||
}
|
||||
|
||||
type WrapProvider struct {
|
||||
mu sync.Mutex
|
||||
done uint32
|
||||
provider Provider
|
||||
factory func(ctx context.Context) (Provider, error)
|
||||
}
|
||||
|
||||
func (p *WrapProvider) Watch(ctx context.Context, callback WatchCallback, path ...string) error {
|
||||
if err := p.init(ctx); err != nil {
|
||||
return fmt.Errorf("init read:%w", err)
|
||||
}
|
||||
|
||||
watch, ok := p.provider.(WatchProvider)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := watch.Watch(ctx, callback, path...); err != nil {
|
||||
return fmt.Errorf("factory provider: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *WrapProvider) Value(ctx context.Context, path ...string) (Value, error) {
|
||||
if err := p.init(ctx); err != nil {
|
||||
return nil, fmt.Errorf("init read:%w", err)
|
||||
}
|
||||
|
||||
variable, err := p.provider.Value(ctx, path...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("factory provider: %w", err)
|
||||
}
|
||||
|
||||
return variable, nil
|
||||
}
|
||||
|
||||
func (p *WrapProvider) Name() string {
|
||||
if err := p.init(context.Background()); err != nil {
|
||||
return fmt.Sprintf("%T", p.provider)
|
||||
}
|
||||
|
||||
return p.provider.Name()
|
||||
}
|
||||
|
||||
func (p *WrapProvider) Bind(ctx context.Context, data Variables) error {
|
||||
if err := p.init(ctx); err != nil {
|
||||
return fmt.Errorf("init bind: %w", err)
|
||||
}
|
||||
|
||||
prov, ok := p.provider.(BindProvider)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if perr := prov.Bind(ctx, data); perr != nil {
|
||||
return fmt.Errorf("init bind provider: %w", perr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *WrapProvider) init(ctx context.Context) error {
|
||||
if atomic.LoadUint32(&p.done) == 0 {
|
||||
if !p.mu.TryLock() {
|
||||
return fmt.Errorf("%w", ErrInitFactory)
|
||||
}
|
||||
defer atomic.StoreUint32(&p.done, 1)
|
||||
defer p.mu.Unlock()
|
||||
|
||||
var err error
|
||||
if p.provider, err = p.factory(ctx); err != nil {
|
||||
return fmt.Errorf("init provider factory:%w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
14
go.mod
14
go.mod
@@ -1,3 +1,15 @@
|
||||
module gitoa.ru/go-4devs/config
|
||||
|
||||
go 1.23
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/hashicorp/vault/api v1.1.0
|
||||
github.com/pelletier/go-toml v1.9.0
|
||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||
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
|
||||
)
|
||||
|
||||
317
go.sum
317
go.sum
@@ -0,0 +1,317 @@
|
||||
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/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 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
|
||||
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
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
Normal file
15
key.go
Normal file
@@ -0,0 +1,15 @@
|
||||
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
|
||||
}
|
||||
32
key/helpers.go
Normal file
32
key/helpers.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
64
key/helpers_test.go
Normal file
64
key/helpers_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
46
key/key.go
Normal file
46
key/key.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
121
key/map.go
121
key/map.go
@@ -1,121 +0,0 @@
|
||||
package key
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
prefixByPath = "byPath"
|
||||
)
|
||||
|
||||
func newMap() *Map {
|
||||
return &Map{
|
||||
idx: 0,
|
||||
wild: nil,
|
||||
children: nil,
|
||||
}
|
||||
}
|
||||
|
||||
type Map struct {
|
||||
idx int
|
||||
wild *Map
|
||||
children map[string]*Map
|
||||
}
|
||||
|
||||
func ByPath(name, sep string) []string {
|
||||
return []string{prefixByPath, name, sep}
|
||||
}
|
||||
|
||||
func (m *Map) Index(path []string) (int, bool) {
|
||||
if data, ok := m.find(path); ok {
|
||||
return data.idx, true
|
||||
}
|
||||
|
||||
if len(path) == 3 && path[0] == prefixByPath {
|
||||
data, ok := m.byPath(path[1], path[2])
|
||||
|
||||
return data.idx, ok
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (m *Map) Add(idx int, path []string) {
|
||||
m.add(path).idx = idx
|
||||
}
|
||||
|
||||
func (m *Map) add(path []string) *Map {
|
||||
name, path := path[0], path[1:]
|
||||
|
||||
if IsWild(name) {
|
||||
m.wild = newMap()
|
||||
|
||||
return m.wild.add(path)
|
||||
}
|
||||
|
||||
if m.children == nil {
|
||||
m.children = map[string]*Map{}
|
||||
}
|
||||
|
||||
if _, ok := m.children[name]; !ok {
|
||||
m.children[name] = newMap()
|
||||
}
|
||||
|
||||
if len(path) > 0 {
|
||||
return m.children[name].add(path)
|
||||
}
|
||||
|
||||
return m.children[name]
|
||||
}
|
||||
|
||||
func (m *Map) byPath(path, sep string) (*Map, bool) {
|
||||
for name := range m.children {
|
||||
if after, ok := strings.CutPrefix(path, name); ok {
|
||||
data := m.children[name]
|
||||
if len(after) == 0 {
|
||||
return data, true
|
||||
}
|
||||
|
||||
after, ok = strings.CutPrefix(after, sep)
|
||||
if !ok {
|
||||
return data, false
|
||||
}
|
||||
|
||||
if data.wild == nil {
|
||||
return data.byPath(after, sep)
|
||||
}
|
||||
|
||||
if idx := strings.Index(after, sep); idx != -1 {
|
||||
return data.wild.byPath(after[idx+1:], sep)
|
||||
}
|
||||
|
||||
return data, false
|
||||
}
|
||||
}
|
||||
|
||||
return m, false
|
||||
}
|
||||
|
||||
func (m *Map) find(path []string) (*Map, bool) {
|
||||
name := path[0]
|
||||
|
||||
last := len(path) == 1
|
||||
if !last {
|
||||
path = path[1:]
|
||||
}
|
||||
|
||||
if m.wild != nil {
|
||||
return m.wild.find(path)
|
||||
}
|
||||
|
||||
data, ok := m.children[name]
|
||||
if !ok {
|
||||
return data, false
|
||||
}
|
||||
|
||||
if last {
|
||||
return data, data.children == nil
|
||||
}
|
||||
|
||||
return data.find(path)
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package key_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
)
|
||||
|
||||
func TestMap_ByPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
expID int = 1
|
||||
newID int = 42
|
||||
)
|
||||
|
||||
data := key.Map{}
|
||||
data.Add(expID, []string{"test", "data", "three"})
|
||||
data.Add(expID, []string{"test", "other"})
|
||||
data.Add(newID, []string{"new", "{data}", "test"})
|
||||
|
||||
idx, ok := data.Index(key.ByPath("test-other", "-"))
|
||||
if !ok {
|
||||
t.Error("key not found")
|
||||
}
|
||||
|
||||
if idx != expID {
|
||||
t.Errorf("idx exp:%v got:%v", expID, idx)
|
||||
}
|
||||
|
||||
if nidx, nok := data.Index(key.ByPath("new-service-test", "-")); !nok && nidx != newID {
|
||||
t.Errorf("idx exp:%v got:%v", newID, nidx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap_Add(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
expID int = 1
|
||||
newID int = 42
|
||||
)
|
||||
|
||||
data := key.Map{}
|
||||
data.Add(expID, []string{"test", "data"})
|
||||
data.Add(expID, []string{"test", "other"})
|
||||
data.Add(newID, []string{"new"})
|
||||
|
||||
idx, ok := data.Index([]string{"test", "data"})
|
||||
if !ok {
|
||||
t.Error("key not found")
|
||||
}
|
||||
|
||||
if idx != expID {
|
||||
t.Errorf("idx exp:%v got:%v", expID, idx)
|
||||
}
|
||||
|
||||
if nidx, nok := data.Index([]string{"new"}); !nok && nidx != newID {
|
||||
t.Errorf("idx exp:%v got:%v", newID, nidx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap_Wild(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
expID int = 1
|
||||
newID int = 42
|
||||
)
|
||||
|
||||
data := key.Map{}
|
||||
data.Add(expID, []string{"test", "{data}", "id"})
|
||||
data.Add(newID, []string{"new", "data"})
|
||||
|
||||
idx, ok := data.Index([]string{"test", "data", "id"})
|
||||
if !ok {
|
||||
t.Error("key not found")
|
||||
}
|
||||
|
||||
if idx != expID {
|
||||
t.Errorf("idx exp:%v got:%v", expID, idx)
|
||||
}
|
||||
}
|
||||
15
key/wild.go
15
key/wild.go
@@ -1,15 +0,0 @@
|
||||
package key
|
||||
|
||||
const minWildCount = 3
|
||||
|
||||
func IsWild(name string) bool {
|
||||
if len(name) < minWildCount {
|
||||
return false
|
||||
}
|
||||
|
||||
return name[0] == '{' && name[len(name)-1] == '}'
|
||||
}
|
||||
|
||||
func Wild(name string) string {
|
||||
return "{" + name + "}"
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package param
|
||||
|
||||
func String(fn Params, key any) (string, bool) {
|
||||
val, ok := fn.Param(key)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
data, ok := val.(string)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func Bool(key any, fn Params) (bool, bool) {
|
||||
val, ok := fn.Param(key)
|
||||
if !ok {
|
||||
return false, false
|
||||
}
|
||||
|
||||
data, ok := val.(bool)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func Uint64(key any, fn Params) (uint64, bool) {
|
||||
data, ok := fn.Param(key)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
res, ok := data.(uint64)
|
||||
|
||||
return res, ok
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package param
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
paramTimeFormat key = iota + 1
|
||||
paramType
|
||||
)
|
||||
|
||||
func WithTimeFormat(format string) Option {
|
||||
return func(p Params) Params {
|
||||
return With(p, paramTimeFormat, format)
|
||||
}
|
||||
}
|
||||
|
||||
func TimeFormat(fn Params) (string, bool) {
|
||||
return String(fn, paramTimeFormat)
|
||||
}
|
||||
|
||||
func WithType(in any) Option {
|
||||
return func(v Params) Params {
|
||||
return With(v, paramType, in)
|
||||
}
|
||||
}
|
||||
|
||||
func Type(fn Params) any {
|
||||
param, _ := fn.Param(paramType)
|
||||
|
||||
return param
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
var emptyParam = empty{}
|
||||
|
||||
type (
|
||||
Option func(p Params) Params
|
||||
Has func(Params) bool
|
||||
Params interface {
|
||||
Param(key any) (any, bool)
|
||||
}
|
||||
)
|
||||
|
||||
func Chain(vals ...Params) Params {
|
||||
slices.Reverse(vals)
|
||||
|
||||
return chain(vals)
|
||||
}
|
||||
|
||||
func With(parent Params, key, val any) Params {
|
||||
return value{
|
||||
Params: parent,
|
||||
key: key,
|
||||
val: val,
|
||||
}
|
||||
}
|
||||
|
||||
func New(opts ...Option) Params {
|
||||
var parms Params
|
||||
|
||||
parms = emptyParam
|
||||
|
||||
for _, opt := range opts {
|
||||
parms = opt(parms)
|
||||
}
|
||||
|
||||
return parms
|
||||
}
|
||||
|
||||
type empty struct{}
|
||||
|
||||
func (v empty) Param(_ any) (any, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
type value struct {
|
||||
Params
|
||||
|
||||
key, val any
|
||||
}
|
||||
|
||||
func (v value) Param(key any) (any, bool) {
|
||||
if v.key == key {
|
||||
return v.val, true
|
||||
}
|
||||
|
||||
return v.Params.Param(key)
|
||||
}
|
||||
|
||||
type chain []Params
|
||||
|
||||
func (c chain) Param(key any) (any, bool) {
|
||||
for _, p := range c {
|
||||
val, ok := p.Param(key)
|
||||
if ok {
|
||||
return val, ok
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package param_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
func TestChainReplace(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
replaceParam = "param1"
|
||||
replaceValue = "replace"
|
||||
)
|
||||
|
||||
params1 := param.With(param.New(), replaceParam, "param1")
|
||||
params2 := param.With(param.New(), replaceParam, replaceValue)
|
||||
|
||||
data, ok := param.String(param.Chain(params1, params2), replaceParam)
|
||||
if !ok {
|
||||
t.Errorf("param %v: not found", replaceParam)
|
||||
}
|
||||
|
||||
if data != replaceValue {
|
||||
t.Errorf("got:%v, expect:%v", data, replaceValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChainExtend(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
extendParam = "param1"
|
||||
extendValue = "replace"
|
||||
)
|
||||
|
||||
params1 := param.With(param.New(), extendParam, extendValue)
|
||||
params2 := param.With(param.New(), "new_value", "param2")
|
||||
|
||||
data1, ok := param.String(param.Chain(params1, params2), extendParam)
|
||||
if !ok {
|
||||
t.Errorf("param %v: not found", extendParam)
|
||||
}
|
||||
|
||||
if data1 != extendValue {
|
||||
t.Errorf("got:%v, expect:%v", data1, extendParam)
|
||||
}
|
||||
}
|
||||
45
provider.go
45
provider.go
@@ -1,50 +1,19 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
import "context"
|
||||
|
||||
type Provider interface {
|
||||
Value(ctx context.Context, path ...string) (Value, error)
|
||||
Name() string
|
||||
Read(ctx context.Context, key Key) (Variable, error)
|
||||
}
|
||||
|
||||
type WatchCallback func(ctx context.Context, oldVar, newVar Value) error
|
||||
type WatchCallback func(ctx context.Context, oldVar, newVar Variable)
|
||||
|
||||
type WatchProvider interface {
|
||||
Watch(ctx context.Context, callback WatchCallback, path ...string) error
|
||||
Watch(ctx context.Context, key Key, callback WatchCallback) error
|
||||
}
|
||||
|
||||
type Factory func(ctx context.Context, cfg Provider) (Provider, error)
|
||||
|
||||
type Option interface {
|
||||
Name() string
|
||||
Param(key any) (any, bool)
|
||||
type ReadConfig interface {
|
||||
Value(ctx context.Context, name string) (Value, error)
|
||||
}
|
||||
|
||||
type Group interface {
|
||||
Option
|
||||
Options
|
||||
}
|
||||
|
||||
type Options interface {
|
||||
Options() []Option
|
||||
}
|
||||
|
||||
type BindProvider interface {
|
||||
Provider
|
||||
|
||||
Bind(ctx context.Context, data Variables) error
|
||||
}
|
||||
|
||||
type Variables interface {
|
||||
ByName(name ...string) (Variable, error)
|
||||
ByParam(filter param.Has) (Variable, error)
|
||||
Variables() []Variable
|
||||
}
|
||||
|
||||
type Definition interface {
|
||||
Add(opts ...Option)
|
||||
}
|
||||
type Factory func(ctx context.Context, cfg ReadConfig) (Provider, error)
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package arg
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
func Default(in any) param.Option {
|
||||
return option.Default(value.New(in))
|
||||
}
|
||||
|
||||
func Required(v param.Params) {
|
||||
option.Required(v)
|
||||
}
|
||||
|
||||
func Slice(v param.Params) {
|
||||
option.Slice(v)
|
||||
}
|
||||
|
||||
func String(name, description string, opts ...param.Option) option.Option {
|
||||
return option.String(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Bool(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Bool(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Duration(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Duration(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Float64(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Float64(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Int(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Int(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Int64(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Int64(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Time(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Time(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Uint(name, description string, opts ...param.Option) option.Option {
|
||||
return option.Uint(name, description, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Uint64(name, descriontion string, opts ...param.Option) option.Option {
|
||||
return option.Uint64(name, descriontion, append(opts, Argument)...)
|
||||
}
|
||||
|
||||
func Err(err error, key ...string) option.Error {
|
||||
return option.Err(err, key)
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package arg
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
type keyParam int
|
||||
|
||||
const (
|
||||
paramArgument keyParam = iota + 1
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var argNum uint64
|
||||
|
||||
func Argument(v param.Params) param.Params {
|
||||
return param.With(v, paramArgument, atomic.AddUint64(&argNum, 1))
|
||||
}
|
||||
|
||||
func ParamArgument(fn param.Params) (uint64, bool) {
|
||||
return param.Uint64(paramArgument, fn)
|
||||
}
|
||||
|
||||
func PosArgument(in uint64) param.Has {
|
||||
return func(p param.Params) bool {
|
||||
idx, ok := ParamArgument(p)
|
||||
|
||||
return ok && idx == in
|
||||
}
|
||||
}
|
||||
|
||||
func HasArgument(fn param.Params) bool {
|
||||
_, ok := ParamArgument(fn)
|
||||
|
||||
return ok
|
||||
}
|
||||
@@ -7,225 +7,40 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
doubleDash = `--`
|
||||
defaultLenLognOption = 2
|
||||
dash = `-`
|
||||
)
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func WithSkip(skip int) func(*Argv) {
|
||||
return func(ar *Argv) {
|
||||
res := 2
|
||||
type Option func(*Provider)
|
||||
|
||||
switch {
|
||||
case skip > 0 && len(os.Args) > skip:
|
||||
res = skip
|
||||
case skip > 0:
|
||||
res = len(os.Args)
|
||||
case len(os.Args) == 1:
|
||||
res = 1
|
||||
case len(os.Args) > 1 && os.Args[1][0] == '-':
|
||||
res = 1
|
||||
}
|
||||
|
||||
ar.skip = res
|
||||
}
|
||||
func WithKeyFactory(factory config.KeyFactory) Option {
|
||||
return func(p *Provider) { p.key = factory }
|
||||
}
|
||||
|
||||
func New(opts ...func(*Argv)) *Argv {
|
||||
arg := &Argv{
|
||||
args: nil,
|
||||
pos: 0,
|
||||
Map: memory.Map{},
|
||||
skip: 1,
|
||||
func New(opts ...Option) *Provider {
|
||||
prov := Provider{
|
||||
key: key.Name,
|
||||
args: make(map[string][]string, len(os.Args[1:])),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(arg)
|
||||
opt(&prov)
|
||||
}
|
||||
|
||||
arg.args = os.Args[arg.skip:]
|
||||
|
||||
return arg
|
||||
return &prov
|
||||
}
|
||||
|
||||
type Argv struct {
|
||||
memory.Map
|
||||
|
||||
args []string
|
||||
pos uint64
|
||||
skip int
|
||||
type Provider struct {
|
||||
args map[string][]string
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (i *Argv) Value(ctx context.Context, key ...string) (config.Value, error) {
|
||||
if err := i.parse(); err != nil {
|
||||
return nil, fmt.Errorf("parse:%w", err)
|
||||
}
|
||||
|
||||
data, err := i.Map.Value(ctx, key...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (i *Argv) Bind(ctx context.Context, def config.Variables) error {
|
||||
options := true
|
||||
|
||||
for len(i.args) > 0 {
|
||||
var err error
|
||||
|
||||
arg := i.args[0]
|
||||
i.args = i.args[1:]
|
||||
|
||||
switch {
|
||||
case options && arg == doubleDash:
|
||||
options = false
|
||||
case options && len(arg) > 2 && arg[0:2] == doubleDash:
|
||||
err = i.parseLongOption(arg[2:], def)
|
||||
case options && arg[0:1] == "-":
|
||||
if len(arg) == 1 {
|
||||
return fmt.Errorf("%w: option name required given '-'", config.ErrInvalidName)
|
||||
}
|
||||
|
||||
err = i.parseShortOption(arg[1:], def)
|
||||
default:
|
||||
err = i.parseArgument(arg, def)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := i.Map.Bind(ctx, def); err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Argv) parseLongOption(arg string, def config.Variables) error {
|
||||
var value *string
|
||||
|
||||
name := arg
|
||||
|
||||
if strings.Contains(arg, "=") {
|
||||
vals := strings.SplitN(arg, "=", defaultLenLognOption)
|
||||
name = vals[0]
|
||||
value = &vals[1]
|
||||
}
|
||||
|
||||
opt, err := def.ByName(key.ByPath(name, dash)...)
|
||||
if err != nil {
|
||||
return Err(err, name)
|
||||
}
|
||||
|
||||
return i.appendOption(value, opt)
|
||||
}
|
||||
|
||||
func (i *Argv) appendOption(data *string, opt config.Variable) error {
|
||||
if i.HasOption(opt.Key()...) && !option.IsSlice(opt) {
|
||||
return fmt.Errorf("%w: got: array, expect: %T", config.ErrUnexpectedType, param.Type(opt))
|
||||
}
|
||||
|
||||
var val string
|
||||
|
||||
switch {
|
||||
case data != nil:
|
||||
val = *data
|
||||
case option.IsBool(opt):
|
||||
val = "true"
|
||||
case len(i.args) > 0 && len(i.args[0]) > 0 && i.args[0][0:1] != "-":
|
||||
val = i.args[0]
|
||||
i.args = i.args[1:]
|
||||
default:
|
||||
return Err(config.ErrRequired, opt.Key()...)
|
||||
}
|
||||
|
||||
err := i.AppendOption(val, opt.Key()...)
|
||||
if err != nil {
|
||||
return Err(err, opt.Key()...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Argv) parseShortOption(arg string, def config.Variables) error {
|
||||
name := arg
|
||||
|
||||
var value string
|
||||
|
||||
if len(name) > 1 {
|
||||
name, value = arg[0:1], arg[1:]
|
||||
}
|
||||
|
||||
opt, err := def.ByParam(option.HasShort(name))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
if option.IsBool(opt) && value != "" {
|
||||
err := i.parseShortOption(value, def)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value = ""
|
||||
}
|
||||
|
||||
if value == "" {
|
||||
return i.appendOption(nil, opt)
|
||||
}
|
||||
|
||||
return i.appendOption(&value, opt)
|
||||
}
|
||||
|
||||
func (i *Argv) parseArgument(arg string, def config.Variables) error {
|
||||
opt, err := def.ByParam(PosArgument(i.pos))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
i.pos++
|
||||
|
||||
if err := i.AppendOption(arg, opt.Key()...); err != nil {
|
||||
return Err(err, opt.Key()...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Argv) parse() error {
|
||||
if i.Len() > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, arg := range i.args {
|
||||
name, value, err := i.parseOne(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
if err := i.AppendOption(value, name); err != nil {
|
||||
return fmt.Errorf("append %v: %w", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseOne return name, value, error.
|
||||
func (i *Argv) parseOne(arg string) (string, string, error) {
|
||||
// nolint: cyclop
|
||||
// return name, value, error.
|
||||
func (p *Provider) parseOne(arg string) (string, string, error) {
|
||||
if arg[0] != '-' {
|
||||
return "", "", nil
|
||||
}
|
||||
@@ -262,3 +77,66 @@ func (i *Argv) parseOne(arg string) (string, string, error) {
|
||||
|
||||
return name, val, nil
|
||||
}
|
||||
|
||||
func (p *Provider) parse() error {
|
||||
if len(p.args) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, arg := range os.Args[1:] {
|
||||
name, value, err := p.parseOne(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
p.args[name] = append(p.args[name], value)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "arg"
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
if err := p.parse(); err != nil {
|
||||
return config.Variable{
|
||||
Name: "",
|
||||
Value: nil,
|
||||
Provider: p.Name(),
|
||||
}, err
|
||||
}
|
||||
|
||||
name := p.key(ctx, key)
|
||||
if val, ok := p.args[name]; ok {
|
||||
switch {
|
||||
case len(val) == 1:
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val[0]),
|
||||
}, nil
|
||||
default:
|
||||
var yNode yaml.Node
|
||||
|
||||
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &yNode); err != nil {
|
||||
return config.Variable{}, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.Decode(yNode.Decode),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrVariableNotFound, name)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package arg_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -33,37 +31,18 @@ func TestProvider(t *testing.T) {
|
||||
"--end-after=2008-01-02T15:04:05+03:00",
|
||||
}
|
||||
read := []test.Read{
|
||||
test.NewRead(8080, "listen"),
|
||||
test.NewRead("config.hcl", "config"),
|
||||
test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"),
|
||||
test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"),
|
||||
test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"),
|
||||
test.NewReadUnmarshal(&[]time.Time{
|
||||
test.NewRead("listen", 8080),
|
||||
test.NewRead("config", "config.hcl"),
|
||||
test.NewRead("start-at", test.Time("2010-01-02T15:04:05Z")),
|
||||
test.NewReadUnmarshal("url", &[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}),
|
||||
test.NewReadUnmarshal("timeout", &[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}),
|
||||
test.NewReadUnmarshal("end-after", &[]time.Time{
|
||||
test.Time("2009-01-02T15:04:05Z"),
|
||||
test.Time("2008-01-02T15:04:05+03:00"),
|
||||
}, &[]time.Time{}, "end-after"),
|
||||
}, &[]time.Time{}),
|
||||
}
|
||||
|
||||
prov := arg.New()
|
||||
|
||||
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 fmt.Appendf(nil, "%q", d), nil
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package chain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
)
|
||||
|
||||
const Name = "chain"
|
||||
|
||||
func New(c ...config.Provider) config.BindProvider {
|
||||
return chain(c)
|
||||
}
|
||||
|
||||
type chain []config.Provider
|
||||
|
||||
func (c chain) Value(ctx context.Context, name ...string) (config.Value, error) {
|
||||
for _, in := range c {
|
||||
if val, err := in.Value(ctx, name...); err == nil {
|
||||
return val, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w", config.ErrNotFound)
|
||||
}
|
||||
|
||||
func (c chain) Bind(ctx context.Context, def config.Variables) error {
|
||||
for _, input := range c {
|
||||
if prov, ok := input.(config.BindProvider); ok {
|
||||
if err := prov.Bind(ctx, def); err != nil {
|
||||
return fmt.Errorf("%T:%w", input, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c chain) Name() string {
|
||||
return Name
|
||||
}
|
||||
36
provider/env/provider.go
vendored
36
provider/env/provider.go
vendored
@@ -2,31 +2,27 @@ package env
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const Name = "env"
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
func WithKeyFactory(factory func(...string) string) Option {
|
||||
func WithKeyFactory(factory config.KeyFactory) Option {
|
||||
return func(p *Provider) { p.key = factory }
|
||||
}
|
||||
|
||||
func New(namespace, appName string, opts ...Option) *Provider {
|
||||
func New(opts ...Option) *Provider {
|
||||
provider := Provider{
|
||||
key: func(path ...string) string {
|
||||
return strings.ToUpper(strings.Join(path, "_"))
|
||||
key: func(ctx context.Context, k config.Key) string {
|
||||
return strings.ToUpper(key.NsAppName("_")(ctx, k))
|
||||
},
|
||||
prefix: strings.ToUpper(namespace + "_" + appName + "_"),
|
||||
name: "",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -37,20 +33,26 @@ func New(namespace, appName string, opts ...Option) *Provider {
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
key func(...string) string
|
||||
name string
|
||||
prefix string
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
return "env"
|
||||
}
|
||||
|
||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||
name := p.prefix + p.key(path...)
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
name := p.key(ctx, key)
|
||||
if val, ok := os.LookupEnv(name); ok {
|
||||
return value.JString(val), nil
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrNotFound)
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
}
|
||||
|
||||
13
provider/env/provider_test.go
vendored
13
provider/env/provider_test.go
vendored
@@ -1,6 +1,7 @@
|
||||
package env_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/env"
|
||||
@@ -8,14 +9,16 @@ import (
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Setenv("FDEVS_CONFIG_DSN", test.DSN)
|
||||
t.Setenv("FDEVS_CONFIG_PORT", "8080")
|
||||
t.Parallel()
|
||||
|
||||
provider := env.New("fdevs", "config")
|
||||
os.Setenv("FDEVS_CONFIG_DSN", test.DSN)
|
||||
os.Setenv("FDEVS_CONFIG_PORT", "8080")
|
||||
|
||||
provider := env.New()
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead(test.DSN, "dsn"),
|
||||
test.NewRead(8080, "port"),
|
||||
test.NewRead("dsn", test.DSN),
|
||||
test.NewRead("port", 8080),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/provider/etcd
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.8.4
|
||||
gitoa.ru/go-4devs/config v0.0.3
|
||||
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
|
||||
)
|
||||
@@ -1,99 +0,0 @@
|
||||
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=
|
||||
gitoa.ru/go-4devs/config v0.0.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
|
||||
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
||||
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=
|
||||
@@ -3,20 +3,14 @@ package etcd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
pb "go.etcd.io/etcd/api/v3/mvccpb"
|
||||
client "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "etcd"
|
||||
Separator = "/"
|
||||
)
|
||||
|
||||
var (
|
||||
_ config.Provider = (*Provider)(nil)
|
||||
_ config.WatchProvider = (*Provider)(nil)
|
||||
@@ -27,55 +21,45 @@ type Client interface {
|
||||
client.Watcher
|
||||
}
|
||||
|
||||
func New(namespace, appName string, client Client) *Provider {
|
||||
prov := Provider{
|
||||
func NewProvider(client Client) *Provider {
|
||||
p := Provider{
|
||||
client: client,
|
||||
key: func(s ...string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
name: Name,
|
||||
prefix: namespace + Separator + appName,
|
||||
log: func(_ context.Context, format string, args ...any) {
|
||||
log.Printf(format, args...)
|
||||
},
|
||||
key: key.NsAppName("/"),
|
||||
}
|
||||
|
||||
return &prov
|
||||
return &p
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
client Client
|
||||
key func(...string) string
|
||||
name string
|
||||
prefix string
|
||||
log func(context.Context, string, ...any)
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
return "etcd"
|
||||
}
|
||||
|
||||
func (p *Provider) Key(s []string) string {
|
||||
return p.prefix + Separator + p.key(s...)
|
||||
}
|
||||
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
name := p.Key(path)
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
name := p.key(ctx, key)
|
||||
|
||||
resp, err := p.client.Get(ctx, name, client.WithPrefix())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
}
|
||||
|
||||
val, err := p.resolve(name, resp.Kvs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, path ...string) error {
|
||||
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
|
||||
go func(ctx context.Context, key string, callback config.WatchCallback) {
|
||||
watch := p.client.Watch(ctx, key, client.WithPrevKV(), client.WithPrefix())
|
||||
for w := range watch {
|
||||
@@ -83,13 +67,10 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, pat
|
||||
if len(kvs) > 0 {
|
||||
newVar, _ := p.resolve(key, kvs)
|
||||
oldVar, _ := p.resolve(key, olds)
|
||||
|
||||
if err := callback(ctx, oldVar, newVar); err != nil {
|
||||
p.log(ctx, "watch callback[%v] %v:%v", p.Name(), path, err)
|
||||
}
|
||||
callback(ctx, oldVar, newVar)
|
||||
}
|
||||
}
|
||||
}(ctx, p.Key(path), callback)
|
||||
}(ctx, p.key(ctx, key), callback)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -106,16 +87,23 @@ func (p *Provider) getEventKvs(events []*client.Event) ([]*pb.KeyValue, []*pb.Ke
|
||||
return kvs, old
|
||||
}
|
||||
|
||||
//nolint:nilnil
|
||||
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Value, error) {
|
||||
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Variable, error) {
|
||||
for _, kv := range kvs {
|
||||
switch {
|
||||
case kv == nil:
|
||||
return nil, nil
|
||||
return config.Variable{
|
||||
Name: key,
|
||||
Provider: p.Name(),
|
||||
Value: nil,
|
||||
}, nil
|
||||
case string(kv.Key) == key:
|
||||
return value.JBytes(kv.Value), nil
|
||||
return config.Variable{
|
||||
Value: value.JBytes(kv.Value),
|
||||
Name: key,
|
||||
Provider: p.Name(),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: name %s", config.ErrValueNotFound, key)
|
||||
return config.Variable{}, fmt.Errorf("%w: name %s", config.ErrVariableNotFound, key)
|
||||
}
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
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.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.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.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
|
||||
}
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/provider/etcd"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
@@ -20,20 +20,20 @@ func TestProvider(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
et, err := NewEtcd(ctx)
|
||||
et, err := test.NewEtcd(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
provider := etcd.New("fdevs", "config", et)
|
||||
provider := etcd.NewProvider(et)
|
||||
read := []test.Read{
|
||||
test.NewRead(test.DSN, "db_dsn"),
|
||||
test.NewRead(12*time.Minute, "duration"),
|
||||
test.NewRead(8080, "port"),
|
||||
test.NewRead(true, "maintain"),
|
||||
test.NewRead(test.Time("2020-01-02T15:04:05Z"), "start_at"),
|
||||
test.NewRead(.064, "percent"),
|
||||
test.NewRead(uint(2020), "count"),
|
||||
test.NewRead(int64(2021), "int64"),
|
||||
test.NewRead(int64(2022), "uint64"),
|
||||
test.NewRead("db_dsn", test.DSN),
|
||||
test.NewRead("duration", 12*time.Minute),
|
||||
test.NewRead("port", 8080),
|
||||
test.NewRead("maintain", true),
|
||||
test.NewRead("start_at", test.Time("2020-01-02T15:04:05Z")),
|
||||
test.NewRead("percent", .064),
|
||||
test.NewRead("count", uint(2020)),
|
||||
test.NewRead("int64", int64(2021)),
|
||||
test.NewRead("uint64", int64(2022)),
|
||||
test.NewReadConfig("config"),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
@@ -49,58 +49,59 @@ func TestWatcher(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
key := "test_watch"
|
||||
key := config.Key{
|
||||
AppName: "config",
|
||||
Namespace: "fdevs",
|
||||
Name: "test_watch",
|
||||
}
|
||||
|
||||
et, err := NewEtcd(ctx)
|
||||
et, err := test.NewEtcd(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
_, err = et.Delete(context.Background(), "fdevs/config/test_watch")
|
||||
_, err = et.KV.Delete(context.Background(), "fdevs/config/test_watch")
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
|
||||
var cnt, cnt2 int32
|
||||
|
||||
prov := etcd.New("fdevs", "config", et)
|
||||
prov := etcd.NewProvider(et)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(6)
|
||||
|
||||
watch := func(cnt *int32) config.WatchCallback {
|
||||
return func(_ context.Context, oldVar, newVar config.Value) error {
|
||||
watch := func(cnt *int32) func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
return func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
switch *cnt {
|
||||
case 0:
|
||||
assert.Equal(t, value(*cnt), newVar.String())
|
||||
assert.Nil(t, oldVar)
|
||||
assert.Equal(t, value(*cnt), newVar.Value.String())
|
||||
assert.Nil(t, oldVar.Value)
|
||||
case 1:
|
||||
assert.Equal(t, value(*cnt), newVar.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||
assert.Equal(t, value(*cnt), newVar.Value.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
|
||||
case 2:
|
||||
_, perr := newVar.ParseString()
|
||||
require.NoError(t, perr)
|
||||
assert.Empty(t, newVar.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||
_, perr := newVar.Value.ParseString()
|
||||
assert.NoError(t, perr)
|
||||
assert.Equal(t, "", newVar.Value.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
|
||||
default:
|
||||
t.Error("unexpected watch")
|
||||
t.Fail()
|
||||
assert.Fail(t, "unexpected watch")
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
atomic.AddInt32(cnt, 1)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
err = prov.Watch(ctx, watch(&cnt), key)
|
||||
err = prov.Watch(ctx, watch(&cnt2), key)
|
||||
err = prov.Watch(ctx, key, watch(&cnt))
|
||||
err = prov.Watch(ctx, key, watch(&cnt2))
|
||||
require.NoError(t, err)
|
||||
|
||||
time.AfterFunc(time.Second, func() {
|
||||
_, err = et.Put(ctx, "fdevs/config/test_watch", value(0))
|
||||
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(0))
|
||||
require.NoError(t, err)
|
||||
_, err = et.Put(ctx, "fdevs/config/test_watch", value(1))
|
||||
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(1))
|
||||
require.NoError(t, err)
|
||||
_, err = et.Delete(ctx, "fdevs/config/test_watch")
|
||||
_, err = et.KV.Delete(ctx, "fdevs/config/test_watch")
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/provider/ini
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
gitoa.ru/go-4devs/config v0.0.3
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.11.1 // indirect
|
||||
@@ -1,12 +0,0 @@
|
||||
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.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
gitoa.ru/go-4devs/config v0.0.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
|
||||
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
||||
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=
|
||||
@@ -10,49 +10,55 @@ import (
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "ini"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(data *ini.File) *Provider {
|
||||
const nameParts = 2
|
||||
|
||||
return &Provider{
|
||||
data: data,
|
||||
resolve: func(path []string) (string, string) {
|
||||
if len(path) == 1 {
|
||||
return "", path[0]
|
||||
resolve: func(ctx context.Context, key config.Key) (string, string) {
|
||||
keys := strings.SplitN(key.Name, "/", nameParts)
|
||||
if len(keys) == 1 {
|
||||
return "", keys[0]
|
||||
}
|
||||
|
||||
return strings.Join(path[:len(path)-1], Separator), strings.ToUpper(path[len(path)-1])
|
||||
return keys[0], keys[1]
|
||||
},
|
||||
name: Name,
|
||||
}
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
data *ini.File
|
||||
resolve func(path []string) (string, string)
|
||||
name string
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
section, name := p.resolve(ctx, key)
|
||||
|
||||
return section != "" && name != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
return "ini"
|
||||
}
|
||||
|
||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||
section, name := p.resolve(path)
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
section, name := p.resolve(ctx, key)
|
||||
|
||||
iniSection, err := p.data.GetSection(section)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
iniKey, err := iniSection.GetKey(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
return value.JString(iniKey.String()), nil
|
||||
return config.Variable{
|
||||
Name: section + ":" + name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(iniKey.String()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1,36 +1,26 @@
|
||||
package ini_test
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/ini"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
lib "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
//go:embed fixture/*
|
||||
var fixtures embed.FS
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
data, derr := fixtures.ReadFile("fixture/config.ini")
|
||||
require.NoError(t, derr)
|
||||
|
||||
file, err := lib.Load(data)
|
||||
require.NoError(t, err)
|
||||
file := test.NewINI()
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("To Do, In Progress, Done", "project", "PROJECT_BOARD_BASIC_KANBAN_TYPE"),
|
||||
test.NewRead("markdown", "repository.editor", "PREVIEWABLE_FILE_MODES"),
|
||||
test.NewRead("http://0.0.0.0:3000/", "server", "LOCAL_ROOT_URL"),
|
||||
test.NewRead(20*time.Minute, "server", "LFS_HTTP_AUTH_EXPIRY"),
|
||||
test.NewRead(5120, "repository.pull-request", "DEFAULT_MERGE_MESSAGE_SIZE"),
|
||||
test.NewRead(true, "ui", "SHOW_USER_EMAIL"),
|
||||
test.NewRead(false, "cors", "enabled"),
|
||||
test.NewRead("project/PROJECT_BOARD_BASIC_KANBAN_TYPE", "To Do, In Progress, Done"),
|
||||
test.NewRead("repository.editor/PREVIEWABLE_FILE_MODES", "markdown"),
|
||||
test.NewRead("server/LOCAL_ROOT_URL", "http://0.0.0.0:3000/"),
|
||||
test.NewRead("server/LFS_HTTP_AUTH_EXPIRY", 20*time.Minute),
|
||||
test.NewRead("repository.pull-request/DEFAULT_MERGE_MESSAGE_SIZE", 5120),
|
||||
test.NewRead("ui/SHOW_USER_EMAIL", true),
|
||||
test.NewRead("cors/ENABLED", false),
|
||||
}
|
||||
|
||||
prov := ini.New(file)
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/provider/json
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
gitoa.ru/go-4devs/config v0.0.3
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
)
|
||||
@@ -1,10 +0,0 @@
|
||||
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/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.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.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
|
||||
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
||||
@@ -1,31 +1,23 @@
|
||||
package json //nolint:revive
|
||||
package json
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "json"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(json []byte, opts ...Option) *Provider {
|
||||
provider := Provider{
|
||||
key: func(s ...string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
key: key.Name,
|
||||
data: json,
|
||||
name: Name,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -36,7 +28,7 @@ func New(json []byte, opts ...Option) *Provider {
|
||||
}
|
||||
|
||||
func NewFile(path string, opts ...Option) (*Provider, error) {
|
||||
file, err := os.ReadFile(filepath.Clean(path))
|
||||
file, err := ioutil.ReadFile(filepath.Clean(path))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: unable to read config file %#q: file not found or unreadable", err, path)
|
||||
}
|
||||
@@ -48,19 +40,27 @@ type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
data []byte
|
||||
key func(...string) string
|
||||
name string
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||
key := p.key(path...)
|
||||
if val := gjson.GetBytes(p.data, key); val.Exists() {
|
||||
return value.JString(val.String()), nil
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path := p.key(ctx, key)
|
||||
|
||||
if val := gjson.GetBytes(p.data, path); val.Exists() {
|
||||
return config.Variable{
|
||||
Name: path,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val.String()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrValueNotFound)
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
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}
|
||||
}
|
||||
@@ -1,32 +1,26 @@
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/json"
|
||||
provider "gitoa.ru/go-4devs/config/provider/json"
|
||||
"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) {
|
||||
t.Parallel()
|
||||
|
||||
js, err := fixture.ReadFile("fixture/config.json")
|
||||
require.NoError(t, err)
|
||||
js := test.ReadFile("config.json")
|
||||
|
||||
prov := json.New(js)
|
||||
prov := provider.New(js)
|
||||
sl := []string{}
|
||||
read := []test.Read{
|
||||
test.NewRead("config title", "app.name.title"),
|
||||
test.NewRead(time.Minute, "app.name.timeout"),
|
||||
test.NewReadUnmarshal(&[]string{"name"}, &sl, "app.name.var"),
|
||||
test.NewRead("app.name.title", "config title"),
|
||||
test.NewRead("app.name.timeout", time.Minute),
|
||||
test.NewReadUnmarshal("app.name.var", &[]string{"name"}, &sl),
|
||||
test.NewReadConfig("cfg"),
|
||||
test.NewRead(true, "app", "name", "success"),
|
||||
test.NewRead("app.name.success", true),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
)
|
||||
|
||||
const NameDefault = "default"
|
||||
|
||||
var _ config.BindProvider = (*Default)(nil)
|
||||
|
||||
type Default struct {
|
||||
data Map
|
||||
name string
|
||||
}
|
||||
|
||||
func (a *Default) Value(ctx context.Context, key ...string) (config.Value, error) {
|
||||
if v, err := a.data.Value(ctx, key...); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w", config.ErrNotFound)
|
||||
}
|
||||
|
||||
func (a *Default) Bind(_ context.Context, def config.Variables) error {
|
||||
for _, opt := range def.Variables() {
|
||||
if data, ok := option.DataDefaut(opt); ok {
|
||||
a.data.SetOption(data, opt.Key()...)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Default) Name() string {
|
||||
if a.name != "" {
|
||||
return a.name
|
||||
}
|
||||
|
||||
return NameDefault
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const NameMap = "map"
|
||||
|
||||
var _ config.BindProvider = (*Map)(nil)
|
||||
|
||||
type Map struct {
|
||||
mu sync.Mutex
|
||||
vals []config.Value
|
||||
idx key.Map
|
||||
name string
|
||||
}
|
||||
|
||||
func (m *Map) Len() int {
|
||||
return len(m.vals)
|
||||
}
|
||||
|
||||
func (m *Map) Value(_ context.Context, key ...string) (config.Value, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
idx, ok := m.idx.Index(key)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w", config.ErrNotFound)
|
||||
}
|
||||
|
||||
val := m.vals[idx]
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (m *Map) Bind(_ context.Context, _ config.Variables) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Map) Name() string {
|
||||
if m.name != "" {
|
||||
return m.name
|
||||
}
|
||||
|
||||
return NameMap
|
||||
}
|
||||
|
||||
func (m *Map) HasOption(key ...string) bool {
|
||||
_, ok := m.idx.Index(key)
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *Map) SetOption(val any, key ...string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if id, ok := m.idx.Index(key); ok {
|
||||
m.vals[id] = value.New(val)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
m.idx.Add(len(m.vals), key)
|
||||
m.vals = append(m.vals, value.New(val))
|
||||
}
|
||||
|
||||
func (m *Map) AppendOption(val string, keys ...string) error {
|
||||
id, ok := m.idx.Index(keys)
|
||||
if !ok {
|
||||
data := value.Strings{val}
|
||||
|
||||
m.SetOption(data, keys...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
old, tok := m.vals[id].(value.Strings)
|
||||
if !tok {
|
||||
return fmt.Errorf("%w:%T", config.ErrWrongType, m.vals[id])
|
||||
}
|
||||
|
||||
m.SetOption(old.Append(val), keys...)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/validator"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
var _ config.BindProvider = (*Map)(nil)
|
||||
|
||||
func Valid(in config.Provider) Validate {
|
||||
return Validate{
|
||||
Provider: in,
|
||||
name: "",
|
||||
}
|
||||
}
|
||||
|
||||
type Validate struct {
|
||||
config.Provider
|
||||
|
||||
name string
|
||||
}
|
||||
|
||||
func (a Validate) Bind(ctx context.Context, def config.Variables) error {
|
||||
for _, opt := range def.Variables() {
|
||||
if val, err := a.Value(ctx, opt.Key()...); err != nil && !value.IsEmpty(val) {
|
||||
if err := validator.Validate(opt.Key(), opt, val); err != nil {
|
||||
return option.Err(err, opt.Key())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Validate) Name() string {
|
||||
if a.name != "" {
|
||||
return a.name
|
||||
}
|
||||
|
||||
return a.Provider.Name()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/provider/toml
|
||||
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/pelletier/go-toml v1.9.5
|
||||
gitoa.ru/go-4devs/config v0.0.1
|
||||
)
|
||||
@@ -1,4 +0,0 @@
|
||||
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=
|
||||
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||
@@ -3,18 +3,13 @@ package toml
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "toml"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func NewFile(file string, opts ...Option) (*Provider, error) {
|
||||
@@ -31,10 +26,7 @@ type Option func(*Provider)
|
||||
func configure(tree *toml.Tree, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
tree: tree,
|
||||
key: func(s []string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
name: Name,
|
||||
key: key.Name,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -55,18 +47,25 @@ func New(data []byte, opts ...Option) (*Provider, error) {
|
||||
|
||||
type Provider struct {
|
||||
tree *toml.Tree
|
||||
key func([]string) string
|
||||
name string
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
return "toml"
|
||||
}
|
||||
|
||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||
if k := p.key(path); p.tree.Has(k) {
|
||||
return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
if k := p.key(ctx, key); p.tree.Has(k) {
|
||||
return config.Variable{
|
||||
Name: k,
|
||||
Provider: p.Name(),
|
||||
Value: Value{Value: value.Value{Val: p.tree.Get(k)}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, config.ErrValueNotFound
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
}
|
||||
|
||||
@@ -1,35 +1,28 @@
|
||||
package toml_test
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config/provider/toml"
|
||||
"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) {
|
||||
t.Parallel()
|
||||
|
||||
files, ferr := fixtures.ReadFile("fixture/config.toml")
|
||||
require.NoError(t, ferr)
|
||||
|
||||
prov, err := toml.New(files)
|
||||
prov, err := toml.NewFile(test.FixturePath("config.toml"))
|
||||
require.NoError(t, err)
|
||||
|
||||
m := []int{}
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("192.168.1.1", "database.server"),
|
||||
test.NewRead("TOML Example", "title"),
|
||||
test.NewRead("10.0.0.1", "servers.alpha.ip"),
|
||||
test.NewRead(true, "database.enabled"),
|
||||
test.NewRead(5000, "database.connection_max"),
|
||||
test.NewReadUnmarshal(&[]int{8001, 8001, 8002}, &m, "database", "ports"),
|
||||
test.NewRead("database.server", "192.168.1.1"),
|
||||
test.NewRead("title", "TOML Example"),
|
||||
test.NewRead("servers.alpha.ip", "10.0.0.1"),
|
||||
test.NewRead("database.enabled", true),
|
||||
test.NewRead("database.connection_max", 5000),
|
||||
test.NewReadUnmarshal("database.ports", &[]int{8001, 8001, 8002}, &m),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
|
||||
@@ -27,7 +27,7 @@ func (s Value) ParseInt() (int, error) {
|
||||
return int(v), nil
|
||||
}
|
||||
|
||||
func (s Value) Unmarshal(target any) error {
|
||||
func (s Value) Unmarshal(target interface{}) error {
|
||||
b, err := json.Marshal(s.Raw())
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/provider/vault
|
||||
|
||||
go 1.22
|
||||
|
||||
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
|
||||
)
|
||||
@@ -1,92 +0,0 @@
|
||||
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=
|
||||
@@ -1,120 +0,0 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "vault"
|
||||
Separator = "/"
|
||||
Prefix = "secret/data/"
|
||||
ValueName = "value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
type SecretOption func(*Provider)
|
||||
|
||||
func WithSecretResolve(f func(key []string) (string, string)) SecretOption {
|
||||
return func(s *Provider) { s.resolve = f }
|
||||
}
|
||||
|
||||
func New(namespace, appName string, client *api.Client, opts ...SecretOption) *Provider {
|
||||
prov := Provider{
|
||||
client: client,
|
||||
resolve: func(key []string) (string, string) {
|
||||
keysLen := len(key)
|
||||
if keysLen == 1 {
|
||||
return "", key[0]
|
||||
}
|
||||
|
||||
return strings.Join(key[:keysLen-1], Separator), key[keysLen-1]
|
||||
},
|
||||
name: Name,
|
||||
prefix: Prefix + namespace + Separator + appName,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&prov)
|
||||
}
|
||||
|
||||
return &prov
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
client *api.Client
|
||||
resolve func(key []string) (string, string)
|
||||
name string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Key(in []string) (string, string) {
|
||||
path, val := p.resolve(in)
|
||||
if path == "" {
|
||||
return p.prefix, val
|
||||
}
|
||||
|
||||
return p.prefix + Separator + path, val
|
||||
}
|
||||
|
||||
func (p *Provider) Value(_ context.Context, key ...string) (config.Value, error) {
|
||||
path, field := p.Key(key)
|
||||
|
||||
secret, err := p.read(path, field)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
|
||||
}
|
||||
|
||||
if secret == nil || len(secret.Data) == 0 {
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if len(secret.Warnings) > 0 {
|
||||
return nil,
|
||||
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrValueNotFound, secret.Warnings, path, field, p.Name())
|
||||
}
|
||||
|
||||
data, ok := secret.Data["data"].(map[string]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if val, ok := data[field]; ok {
|
||||
return value.JString(fmt.Sprint(val)), nil
|
||||
}
|
||||
|
||||
if val, ok := data[ValueName]; ok {
|
||||
return value.JString(fmt.Sprint(val)), nil
|
||||
}
|
||||
|
||||
md, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return value.JBytes(md), nil
|
||||
}
|
||||
|
||||
func (p *Provider) read(path, key string) (*api.Secret, error) {
|
||||
secret, err := p.client.Logical().Read(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read[%s:%s]:%w", path, key, err)
|
||||
}
|
||||
|
||||
if secret == nil && key != ValueName {
|
||||
return p.read(path+Separator+key, ValueName)
|
||||
}
|
||||
|
||||
return secret, nil
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
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
|
||||
}
|
||||
90
provider/vault/secret.go
Normal file
90
provider/vault/secret.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*SecretKV2)(nil)
|
||||
|
||||
type SecretOption func(*SecretKV2)
|
||||
|
||||
func WithSecretResolve(f func(context.Context, config.Key) (string, string)) SecretOption {
|
||||
return func(s *SecretKV2) { s.resolve = f }
|
||||
}
|
||||
|
||||
func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
|
||||
prov := SecretKV2{
|
||||
client: client,
|
||||
resolve: key.LastIndexField(":", "value", key.PrefixName("secret/data/", key.NsAppName("/"))),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&prov)
|
||||
}
|
||||
|
||||
return &prov
|
||||
}
|
||||
|
||||
type SecretKV2 struct {
|
||||
client *api.Client
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *SecretKV2) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
path, _ := p.resolve(ctx, key)
|
||||
|
||||
return path != ""
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Name() string {
|
||||
return "vault"
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path, field := p.resolve(ctx, key)
|
||||
|
||||
secret, err := p.client.Logical().Read(path)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
|
||||
}
|
||||
|
||||
if secret == nil || len(secret.Data) == 0 {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if len(secret.Warnings) > 0 {
|
||||
return config.Variable{},
|
||||
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrVariableNotFound, secret.Warnings, path, field, p.Name())
|
||||
}
|
||||
|
||||
data, ok := secret.Data["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if val, ok := data[field]; ok {
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(fmt.Sprint(val)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
md, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JBytes(md),
|
||||
}, nil
|
||||
}
|
||||
@@ -4,23 +4,23 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config/provider/vault"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cl, err := NewVault()
|
||||
cl, err := test.NewVault()
|
||||
require.NoError(t, err)
|
||||
|
||||
provider := vault.New("fdevs", "config", cl)
|
||||
provider := vault.NewSecretKV2(cl)
|
||||
|
||||
read := []test.Read{
|
||||
test.NewReadConfig("database"),
|
||||
test.NewRead(test.DSN, "db", "dsn"),
|
||||
test.NewRead(time.Minute, "db", "timeout"),
|
||||
test.NewRead("db:dsn", test.DSN),
|
||||
test.NewRead("db:timeout", time.Minute),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
}
|
||||
@@ -2,9 +2,8 @@ package watcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
@@ -18,8 +17,10 @@ var (
|
||||
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
Provider: provider,
|
||||
duration: duration,
|
||||
logger: slog.ErrorContext,
|
||||
ticker: time.NewTicker(duration),
|
||||
logger: func(_ context.Context, msg string) {
|
||||
log.Print(msg)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -29,7 +30,7 @@ func New(duration time.Duration, provider config.Provider, opts ...Option) *Prov
|
||||
return prov
|
||||
}
|
||||
|
||||
func WithLogger(l func(context.Context, string, ...any)) Option {
|
||||
func WithLogger(l func(context.Context, string)) Option {
|
||||
return func(p *Provider) {
|
||||
p.logger = l
|
||||
}
|
||||
@@ -39,46 +40,32 @@ type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
config.Provider
|
||||
|
||||
duration time.Duration
|
||||
logger func(context.Context, string, ...any)
|
||||
ticker *time.Ticker
|
||||
logger func(context.Context, string)
|
||||
}
|
||||
|
||||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
|
||||
old, err := p.Value(ctx, key...)
|
||||
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
|
||||
oldVar, err := p.Provider.Read(ctx, key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed watch variable: %w", err)
|
||||
}
|
||||
|
||||
go func(oldVar config.Value) {
|
||||
ticker := time.NewTicker(p.duration)
|
||||
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
newVar, err := p.Value(ctx, key...)
|
||||
case <-p.ticker.C:
|
||||
newVar, err := p.Provider.Read(ctx, key)
|
||||
if err != nil {
|
||||
p.logger(ctx, "get value%v:%v", key, err.Error())
|
||||
p.logger(ctx, err.Error())
|
||||
} else if !newVar.IsEquals(oldVar) {
|
||||
if err := callback(ctx, oldVar, newVar); err != nil {
|
||||
if errors.Is(err, config.ErrStopWatch) {
|
||||
return
|
||||
}
|
||||
|
||||
p.logger(ctx, "callback %v:%v", key, err)
|
||||
}
|
||||
|
||||
callback(ctx, oldVar, newVar)
|
||||
oldVar = newVar
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}(old)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,20 +2,18 @@ package watcher_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/provider/watcher"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*provider)(nil)
|
||||
|
||||
type provider struct {
|
||||
cnt int32
|
||||
}
|
||||
@@ -24,24 +22,23 @@ func (p *provider) Name() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func (p *provider) Value(context.Context, ...string) (config.Value, error) {
|
||||
func (p *provider) Read(context.Context, config.Key) (config.Variable, error) {
|
||||
p.cnt++
|
||||
|
||||
return value.JString(strconv.Itoa(int(p.cnt))), nil
|
||||
return config.Variable{
|
||||
Name: "tmpname",
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(fmt.Sprint(p.cnt)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestWatcher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
|
||||
defer func() {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
ctx := context.Background()
|
||||
prov := &provider{}
|
||||
|
||||
w := watcher.New(time.Second/3, prov)
|
||||
w := watcher.New(time.Second, prov)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
@@ -49,21 +46,14 @@ func TestWatcher(t *testing.T) {
|
||||
|
||||
err := w.Watch(
|
||||
ctx,
|
||||
func(_ context.Context, _, _ config.Value) error {
|
||||
config.Key{Name: "tmpname"},
|
||||
func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
atomic.AddInt32(&cnt, 1)
|
||||
wg.Done()
|
||||
|
||||
if atomic.LoadInt32(&cnt) == 2 {
|
||||
return config.ErrStopWatch
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
"tmpname",
|
||||
)
|
||||
|
||||
require.NoError(t, err)
|
||||
wg.Wait()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int32(2), cnt)
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user