Compare commits
16 Commits
v0.0.1
...
504a2369de
| Author | SHA1 | Date | |
|---|---|---|---|
| 504a2369de | |||
| b0ec158da2 | |||
| 5586adc4e3 | |||
| aeb90ceaa6 | |||
| 80b0244b52 | |||
| 947c40d918 | |||
| 788e2928fe | |||
| 66c1eef44a | |||
| b7736b7d4c | |||
| fdab23b756 | |||
| 18ce790363 | |||
| 5cb46f5030 | |||
| dd37b51974 | |||
| 1088e26bcf | |||
| 089f43dcb9 | |||
| 59f97e1681 |
157
.drone.yml
157
.drone.yml
@@ -1,20 +1,165 @@
|
|||||||
|
---
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
name: default
|
name: default
|
||||||
|
|
||||||
environment:
|
|
||||||
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200
|
|
||||||
VAULT_DEV_ROOT_TOKEN_ID: dev
|
|
||||||
FDEVS_CONFIG_ETCD_HOST: etcd:2379
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: test
|
- name: test
|
||||||
image: golang
|
image: golang
|
||||||
commands:
|
commands:
|
||||||
# - go test -parallel 10 -race ./...
|
# - go test -parallel 10 -race ./...
|
||||||
# - go test -race ./...
|
|
||||||
- go test ./...
|
- go test ./...
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
image: golangci/golangci-lint:v1.55
|
image: golangci/golangci-lint:v1.55
|
||||||
commands:
|
commands:
|
||||||
- golangci-lint run
|
- golangci-lint run
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
name: json
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- cd provider/json
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/json
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
name: yaml
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- cd provider/yaml
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/yaml
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: ini
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
|
||||||
|
commands:
|
||||||
|
- cd provider/ini
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/ini
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: toml
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- cd provider/toml
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/toml
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: etcd
|
||||||
|
|
||||||
|
environment:
|
||||||
|
FDEVS_CONFIG_ETCD_HOST: etcd:2379
|
||||||
|
|
||||||
|
services:
|
||||||
|
- name: etcd
|
||||||
|
image: bitnami/etcd:3.5.11
|
||||||
|
environment:
|
||||||
|
ALLOW_NONE_AUTHENTICATION: yes
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
|
||||||
|
commands:
|
||||||
|
- cd provider/etcd
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/etcd
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: vault
|
||||||
|
|
||||||
|
environment:
|
||||||
|
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200
|
||||||
|
VAULT_DEV_ROOT_TOKEN_ID: dev
|
||||||
|
|
||||||
|
services:
|
||||||
|
- name: vault
|
||||||
|
image: vault:1.13.3
|
||||||
|
environment:
|
||||||
|
VAULT_DEV_ROOT_TOKEN_ID: dev
|
||||||
|
VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
|
||||||
|
commands:
|
||||||
|
- cd provider/vault
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/vault
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: definition
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- cd provider/definition
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
image: golangci/golangci-lint:v1.55
|
||||||
|
commands:
|
||||||
|
- cd provider/definition
|
||||||
|
- golangci-lint run
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ linters-settings:
|
|||||||
locale: US
|
locale: US
|
||||||
varnamelen:
|
varnamelen:
|
||||||
min-name-length: 2
|
min-name-length: 2
|
||||||
|
ignore-decls:
|
||||||
|
- w io.Writer
|
||||||
|
- t testing.T
|
||||||
|
- e error
|
||||||
|
- i int
|
||||||
|
- b bytes.Buffer
|
||||||
|
- h Handle
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
enable-all: true
|
enable-all: true
|
||||||
|
|||||||
172
client_example_test.go
Normal file
172
client_example_test.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package config_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/arg"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/env"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/watcher"
|
||||||
|
"gitoa.ru/go-4devs/config/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleClient_Value() {
|
||||||
|
ctx := context.Background()
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
|
||||||
|
|
||||||
|
args := os.Args
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
os.Args = args
|
||||||
|
}()
|
||||||
|
|
||||||
|
os.Args = []string{"main.go", "--host=gitoa.ru"}
|
||||||
|
|
||||||
|
// read json config
|
||||||
|
|
||||||
|
config, err := config.New(
|
||||||
|
arg.New(),
|
||||||
|
env.New(test.Namespace, test.AppName),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := config.Value(ctx, "listen")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("listen", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hostValue, err := config.Value(ctx, "host")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("host ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("listen from env: %d\n", port.Int())
|
||||||
|
fmt.Printf("replace env host by args: %v\n", hostValue.String())
|
||||||
|
// Output:
|
||||||
|
// listen from env: 8080
|
||||||
|
// replace env host by args: gitoa.ru
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_Watch() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")
|
||||||
|
|
||||||
|
watcher, err := config.New(
|
||||||
|
watcher.New(time.Microsecond, env.New(test.Namespace, test.AppName)),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) error {
|
||||||
|
fmt.Println("update example_enable old: ", oldVar.Bool(), " new:", newVar.Bool())
|
||||||
|
wg.Done()
|
||||||
|
return nil
|
||||||
|
}, "example_enable")
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")
|
||||||
|
|
||||||
|
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) error {
|
||||||
|
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
|
||||||
|
wg.Done()
|
||||||
|
return nil
|
||||||
|
}, "example_db_dsn")
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// update example_enable old: true new: false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_Value_factory() {
|
||||||
|
ctx := context.Background()
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
|
||||||
|
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
|
||||||
|
_ = os.Setenv("FDEVS_GOLANG_HOST", "go.dev")
|
||||||
|
|
||||||
|
args := os.Args
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
os.Args = args
|
||||||
|
}()
|
||||||
|
|
||||||
|
os.Args = []string{"main.go", "--env=golang"}
|
||||||
|
|
||||||
|
config, err := config.New(
|
||||||
|
arg.New(),
|
||||||
|
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
|
||||||
|
val, err := cfg.Value(ctx, "env")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed read config file:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return env.New(test.Namespace, val.String()), nil
|
||||||
|
}),
|
||||||
|
env.New(test.Namespace, test.AppName),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
envName, err := config.Value(ctx, "env")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("env ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
host, err := config.Value(ctx, "host")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("host ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
listen, err := config.Value(ctx, "listen")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("listen", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("envName from env: %s\n", envName.String())
|
||||||
|
fmt.Printf("host from env with app name golang: %s\n", host.String())
|
||||||
|
fmt.Printf("listen from env with default app name: %s\n", listen.String())
|
||||||
|
// Output:
|
||||||
|
// envName from env: golang
|
||||||
|
// host from env with app name golang: go.dev
|
||||||
|
// listen from env with default app name: 8080
|
||||||
|
}
|
||||||
29
definition/defenition.go
Executable file
29
definition/defenition.go
Executable file
@@ -0,0 +1,29 @@
|
|||||||
|
package definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New() Definition {
|
||||||
|
return Definition{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Definition struct {
|
||||||
|
options Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Definition) Add(opts ...Option) *Definition {
|
||||||
|
d.options = append(d.options, opts...)
|
||||||
|
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Definition) View(handle func(Option) error) error {
|
||||||
|
for idx, opt := range d.options {
|
||||||
|
if err := handle(opt); err != nil {
|
||||||
|
return fmt.Errorf("%s[%d]:%w", opt.Kind(), idx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
70
definition/generate/generator.go
Normal file
70
definition/generate/generator.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Generator struct {
|
||||||
|
pkg string
|
||||||
|
ViewOption
|
||||||
|
Imp Imports
|
||||||
|
errs []error
|
||||||
|
defaultErrors []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) Pkg() string {
|
||||||
|
return g.pkg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) Imports() []Import {
|
||||||
|
return g.Imp.Imports()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) Handle(w io.Writer, data Handler, opt definition.Option) error {
|
||||||
|
handle := get(opt.Kind())
|
||||||
|
|
||||||
|
return handle(w, data, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) StructName() string {
|
||||||
|
return FuncName(g.Prefix + "_" + g.Struct + "_" + g.Suffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) Options() ViewOption {
|
||||||
|
return g.ViewOption
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) Keys() []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Generator) DefaultErrors() []string {
|
||||||
|
if len(g.defaultErrors) > 0 {
|
||||||
|
return g.defaultErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(g.ViewOption.Errors.Default) > 0 {
|
||||||
|
g.Imp.Adds("errors")
|
||||||
|
}
|
||||||
|
|
||||||
|
g.defaultErrors = make([]string, len(g.ViewOption.Errors.Default))
|
||||||
|
for idx, name := range g.ViewOption.Errors.Default {
|
||||||
|
short, err := g.AddType(name)
|
||||||
|
if err != nil {
|
||||||
|
g.errs = append(g.errs, fmt.Errorf("add default error[%d]:%w", idx, err))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
g.defaultErrors[idx] = short
|
||||||
|
}
|
||||||
|
|
||||||
|
return g.defaultErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) AddType(pkg string) (string, error) {
|
||||||
|
return g.Imp.AddType(pkg)
|
||||||
|
}
|
||||||
18
definition/generate/helpers.go
Normal file
18
definition/generate/helpers.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/iancoleman/strcase"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
ErrAlreadyExist = errors.New("already exist")
|
||||||
|
ErrWrongType = errors.New("wrong type")
|
||||||
|
ErrWrongFormat = errors.New("wrong format")
|
||||||
|
)
|
||||||
|
|
||||||
|
func FuncName(in string) string {
|
||||||
|
return strcase.ToCamel(in)
|
||||||
|
}
|
||||||
89
definition/generate/imports.go
Normal file
89
definition/generate/imports.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewImports() Imports {
|
||||||
|
return Imports{
|
||||||
|
data: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Imports struct {
|
||||||
|
data map[string]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", ErrWrongFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
imp := i.Add(fullType[:idx])
|
||||||
|
|
||||||
|
return imp.Alias + fullType[idx:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Imports) Adds(pkgs ...string) {
|
||||||
|
for _, pkg := range pkgs {
|
||||||
|
i.Add(pkg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Imports) Add(pkg string) Import {
|
||||||
|
alias := pkg
|
||||||
|
|
||||||
|
if idx := strings.LastIndexByte(pkg, '/'); idx != -1 {
|
||||||
|
alias = 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
|
||||||
|
}
|
||||||
39
definition/generate/run.go
Normal file
39
definition/generate/run.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(w io.Writer, pkgName string, defs definition.Definition, viewOpt ViewOption) error {
|
||||||
|
gen := Generator{
|
||||||
|
pkg: pkgName,
|
||||||
|
ViewOption: viewOpt,
|
||||||
|
Imp: NewImports(),
|
||||||
|
}
|
||||||
|
|
||||||
|
gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context")
|
||||||
|
|
||||||
|
var view bytes.Buffer
|
||||||
|
|
||||||
|
err := defs.View(func(o definition.Option) error {
|
||||||
|
return gen.Handle(&view, &gen, o)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("render options:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tpl.Execute(w, gen); err != nil {
|
||||||
|
return fmt.Errorf("render base:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, cerr := io.Copy(w, &view)
|
||||||
|
if cerr != nil {
|
||||||
|
return fmt.Errorf("copy error:%w", cerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
43
definition/generate/template.go
Normal file
43
definition/generate/template.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import "text/template"
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var (
|
||||||
|
tpl = template.Must(template.New("tpls").Parse(baseTemplate))
|
||||||
|
baseTemplate = `// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
|
||||||
|
package {{.Pkg}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
{{range .Imports}}
|
||||||
|
{{- .Alias }}"{{ .Package }}"
|
||||||
|
{{end}}
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
)
|
||||||
63
definition/generate/view.go
Normal file
63
definition/generate/view.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package generate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var handlers = sync.Map{}
|
||||||
|
|
||||||
|
func Add(kind string, h Handle) error {
|
||||||
|
_, ok := handlers.Load(kind)
|
||||||
|
if ok {
|
||||||
|
return fmt.Errorf("kind %v: %w", kind, ErrAlreadyExist)
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers.Store(kind, h)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:forcetypeassert
|
||||||
|
func get(kind string) Handle {
|
||||||
|
handler, ok := handlers.Load(kind)
|
||||||
|
if !ok {
|
||||||
|
return func(w io.Writer, h Handler, o definition.Option) error {
|
||||||
|
return fmt.Errorf("handler by %v:%w", kind, ErrNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler.(Handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustAdd(kind string, h Handle) {
|
||||||
|
if err := Add(kind, h); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handle func(io.Writer, Handler, definition.Option) error
|
||||||
|
|
||||||
|
type Handler interface {
|
||||||
|
StructName() string
|
||||||
|
Handle(w io.Writer, handler Handler, opt definition.Option) error
|
||||||
|
Options() ViewOption
|
||||||
|
Keys() []string
|
||||||
|
AddType(fullName string) (string, error)
|
||||||
|
DefaultErrors() []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ViewOption struct {
|
||||||
|
Prefix, Suffix string
|
||||||
|
Context bool
|
||||||
|
Struct string
|
||||||
|
Errors ViewErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
type ViewErrors struct {
|
||||||
|
Default []string
|
||||||
|
}
|
||||||
5
definition/go.mod
Normal file
5
definition/go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/definition
|
||||||
|
|
||||||
|
go 1.21.5
|
||||||
|
|
||||||
|
require github.com/iancoleman/strcase v0.3.0
|
||||||
2
definition/go.sum
Normal file
2
definition/go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||||
|
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||||
27
definition/group/group.go
Executable file
27
definition/group/group.go
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
package group
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
const Kind = "group"
|
||||||
|
|
||||||
|
var _ definition.Option = Group{}
|
||||||
|
|
||||||
|
func New(name, desc string, opts ...definition.Option) Group {
|
||||||
|
return Group{
|
||||||
|
Name: name,
|
||||||
|
Description: desc,
|
||||||
|
Options: opts,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Group struct {
|
||||||
|
Options definition.Options
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Group) Kind() string {
|
||||||
|
return Kind
|
||||||
|
}
|
||||||
88
definition/group/view.go
Normal file
88
definition/group/view.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package group
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/generate"
|
||||||
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoinits
|
||||||
|
func init() {
|
||||||
|
generate.MustAdd(Kind, handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handle(w io.Writer, data generate.Handler, option definition.Option) error {
|
||||||
|
group, ok := option.(Group)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%w:%T", generate.ErrWrongType, option)
|
||||||
|
}
|
||||||
|
|
||||||
|
viewData := View{
|
||||||
|
Group: group,
|
||||||
|
ParentName: data.StructName(),
|
||||||
|
ViewOption: data.Options(),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tpl.Execute(w, viewData)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("render group:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
childData := ChildData{
|
||||||
|
Handler: data,
|
||||||
|
structName: viewData.StructName(),
|
||||||
|
keys: append(data.Keys(), group.Name),
|
||||||
|
}
|
||||||
|
for idx, child := range group.Options {
|
||||||
|
if cerr := data.Handle(w, childData, child); cerr != nil {
|
||||||
|
return fmt.Errorf("render group child[%d]:%w", idx, cerr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChildData struct {
|
||||||
|
generate.Handler
|
||||||
|
structName string
|
||||||
|
keys []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c ChildData) StructName() string {
|
||||||
|
return c.structName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c ChildData) Keys() []string {
|
||||||
|
return c.keys
|
||||||
|
}
|
||||||
|
|
||||||
|
type View struct {
|
||||||
|
Group
|
||||||
|
ParentName string
|
||||||
|
generate.ViewOption
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) FuncName() string {
|
||||||
|
return generate.FuncName(v.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) StructName() string {
|
||||||
|
return generate.FuncName(v.Prefix + v.Name + v.Suffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var (
|
||||||
|
tpl = template.Must(template.New("tpls").Parse(gpoupTemplate))
|
||||||
|
gpoupTemplate = `type {{.StructName}} struct {
|
||||||
|
{{.ParentName}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.ParentName}}) {{.FuncName}}() {{.StructName}} {
|
||||||
|
return {{.StructName}}{i}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
)
|
||||||
27
definition/option.go
Executable file
27
definition/option.go
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
package definition
|
||||||
|
|
||||||
|
type Option interface {
|
||||||
|
Kind() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options []Option
|
||||||
|
|
||||||
|
func (s Options) Len() int { return len(s) }
|
||||||
|
func (s Options) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
|
||||||
|
type Params []Param
|
||||||
|
|
||||||
|
func (p Params) Get(name string) (any, bool) {
|
||||||
|
for _, param := range p {
|
||||||
|
if param.Name == name {
|
||||||
|
return param.Value, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
type Param struct {
|
||||||
|
Name string
|
||||||
|
Value any
|
||||||
|
}
|
||||||
100
definition/option/option.go
Executable file
100
definition/option/option.go
Executable file
@@ -0,0 +1,100 @@
|
|||||||
|
package option
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ definition.Option = Option{}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Kind = "option"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TypeString = "string"
|
||||||
|
TypeInt = "int"
|
||||||
|
TypeInt64 = "int64"
|
||||||
|
TypeUint = "uint"
|
||||||
|
TypeUint64 = "uint64"
|
||||||
|
TypeFloat64 = "float64"
|
||||||
|
TypeBool = "bool"
|
||||||
|
TypeTime = "time.Time"
|
||||||
|
TypeDuration = "time.Duration"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Default(v any) func(*Option) {
|
||||||
|
return func(o *Option) {
|
||||||
|
o.Default = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(name, desc string, vtype any, opts ...func(*Option)) Option {
|
||||||
|
option := Option{
|
||||||
|
Name: name,
|
||||||
|
Description: desc,
|
||||||
|
Type: vtype,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&option)
|
||||||
|
}
|
||||||
|
|
||||||
|
return option
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option struct {
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Type any
|
||||||
|
Default any
|
||||||
|
Params definition.Params
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option) WithParams(params ...definition.Param) Option {
|
||||||
|
return Option{
|
||||||
|
Name: o.Name,
|
||||||
|
Description: o.Description,
|
||||||
|
Type: o.Type,
|
||||||
|
Params: append(params, o.Params...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option) Kind() string {
|
||||||
|
return Kind
|
||||||
|
}
|
||||||
|
|
||||||
|
func Time(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeTime, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Duration(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeDuration, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func String(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeString, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeInt, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int64(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeInt64, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeUint, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint64(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeUint64, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Float64(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeFloat64, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Bool(name, desc string, opts ...func(*Option)) Option {
|
||||||
|
return New(name, desc, TypeBool, opts...)
|
||||||
|
}
|
||||||
33
definition/option/tpl/option.tmpl
Normal file
33
definition/option/tpl/option.tmpl
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// read{{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context) (v {{.Type}},e error) {
|
||||||
|
val, err := i.Value(ctx, {{ .ParentKeys }}"{{ .Name }}")
|
||||||
|
if err != nil {
|
||||||
|
{{if .HasDefault}}
|
||||||
|
{{$default := .Default}}
|
||||||
|
{{range .DefaultErrors}}
|
||||||
|
if errors.Is(err,{{.}}){
|
||||||
|
return {{$default}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
return v, fmt.Errorf("read {{.Keys}}:%w",err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{{.Parse "val" "v" .Keys }}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read{{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context) ({{.Type}}, error) {
|
||||||
|
return i.read{{.FuncName}}(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context {{end}}) {{.Type}} {
|
||||||
|
{{if not .Context}} ctx := context.Background() {{end}}
|
||||||
|
val, err := i.read{{.FuncName}}(ctx)
|
||||||
|
if err != nil {
|
||||||
|
i.log(ctx, "get {{.Keys}}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
3
definition/option/tpl/parse.tmpl
Normal file
3
definition/option/tpl/parse.tmpl
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{{block "Parse" .}}
|
||||||
|
return {{.ValName}}.Parse{{ .FuncType}}()
|
||||||
|
{{end}}
|
||||||
8
definition/option/tpl/unmarshal_json.tmpl
Normal file
8
definition/option/tpl/unmarshal_json.tmpl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{{block "UnmarshalJSON" . }}
|
||||||
|
pval, perr := {{.ValName}}.ParseString()
|
||||||
|
if perr != nil {
|
||||||
|
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{.Value}}, {{.Value}}.UnmarshalJSON([]byte(pval))
|
||||||
|
{{end}}
|
||||||
8
definition/option/tpl/unmarshal_text.tmpl
Normal file
8
definition/option/tpl/unmarshal_text.tmpl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{{block "UnmarshalText" . }}
|
||||||
|
pval, perr := {{.ValName}}.ParseString()
|
||||||
|
if perr != nil {
|
||||||
|
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{.Value}}, {{.Value}}.UnmarshalText([]byte(pval))
|
||||||
|
{{end}}
|
||||||
235
definition/option/view.go
Normal file
235
definition/option/view.go
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
package option
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"embed"
|
||||||
|
"encoding"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/generate"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed tpl/*
|
||||||
|
var tpls embed.FS
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var tpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tmpl"))
|
||||||
|
|
||||||
|
//nolint:gochecknoinits
|
||||||
|
func init() {
|
||||||
|
generate.MustAdd(Kind, Handle(tpl.Lookup("option.tmpl")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handle(tpl *template.Template) generate.Handle {
|
||||||
|
return func(w io.Writer, h generate.Handler, o definition.Option) error {
|
||||||
|
opt, _ := o.(Option)
|
||||||
|
if err := tpl.Execute(w, View{Option: opt, Handler: h}); err != nil {
|
||||||
|
return fmt.Errorf("option tpl:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type View struct {
|
||||||
|
Option
|
||||||
|
generate.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) Context() bool {
|
||||||
|
return v.Options().Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) FuncName() string {
|
||||||
|
if funcName, ok := v.Option.Params.Get(ViewParamFunctName); ok {
|
||||||
|
name, _ := funcName.(string)
|
||||||
|
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
return generate.FuncName(v.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) Description() string {
|
||||||
|
if desc, ok := v.Option.Params.Get(ViewParamDescription); ok {
|
||||||
|
description, _ := desc.(string)
|
||||||
|
|
||||||
|
return description
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.Option.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) Default() string {
|
||||||
|
switch data := v.Option.Default.(type) {
|
||||||
|
case time.Time:
|
||||||
|
return fmt.Sprintf("time.Parse(%q,time.RFC3339Nano)", data.Format(time.RFC3339Nano))
|
||||||
|
case time.Duration:
|
||||||
|
return fmt.Sprintf("time.ParseDuration(%q)", data)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%#v, nil", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) HasDefault() bool {
|
||||||
|
return v.Option.Default != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) ParentKeys() string {
|
||||||
|
if len(v.Handler.Keys()) > 0 {
|
||||||
|
return `"` + strings.Join(v.Handler.Keys(), `","`) + `",`
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) Type() string {
|
||||||
|
slice := ""
|
||||||
|
|
||||||
|
if vtype, ok := v.Option.Type.(string); ok {
|
||||||
|
if strings.Contains(vtype, ".") {
|
||||||
|
if name, err := v.AddType(vtype); err == nil {
|
||||||
|
return slice + name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return vtype
|
||||||
|
}
|
||||||
|
|
||||||
|
rtype := reflect.TypeOf(v.Option.Type)
|
||||||
|
|
||||||
|
if rtype.PkgPath() == "" {
|
||||||
|
return rtype.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if rtype.Kind() == reflect.Slice {
|
||||||
|
slice = "[]"
|
||||||
|
}
|
||||||
|
|
||||||
|
short, err := v.AddType(rtype.PkgPath() + "." + rtype.Name())
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return slice + short
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) FuncType() string {
|
||||||
|
return generate.FuncName(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v View) Parse(valName string, value string, keys []string) string {
|
||||||
|
h := parser(v.Option.Type)
|
||||||
|
|
||||||
|
data, err := h(ParseData{
|
||||||
|
Value: value,
|
||||||
|
ValName: valName,
|
||||||
|
Keys: keys,
|
||||||
|
View: v,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals,unparam
|
||||||
|
var parses = map[string]func(data ParseData) (string, error){
|
||||||
|
typesIntreface[0].Name(): func(data ParseData) (string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
err := tpl.ExecuteTemplate(&b, "unmarshal_text.tmpl", data)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("execute unmarshal text:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String(), nil
|
||||||
|
},
|
||||||
|
typesIntreface[1].Name(): func(data ParseData) (string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
err := tpl.ExecuteTemplate(&b, "unmarshal_json.tmpl", data)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("execute unmarshal json:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String(), nil
|
||||||
|
},
|
||||||
|
TypeInt: internal,
|
||||||
|
TypeInt64: internal,
|
||||||
|
TypeBool: internal,
|
||||||
|
TypeString: internal,
|
||||||
|
TypeFloat64: internal,
|
||||||
|
TypeUint: internal,
|
||||||
|
TypeUint64: internal,
|
||||||
|
"time.Duration": func(data ParseData) (string, error) {
|
||||||
|
return fmt.Sprintf("return %s.ParseDuration()", data.ValName), nil
|
||||||
|
},
|
||||||
|
"time.Time": func(data ParseData) (string, error) {
|
||||||
|
return fmt.Sprintf("return %s.ParseTime()", data.ValName), nil
|
||||||
|
},
|
||||||
|
"any": func(data ParseData) (string, error) {
|
||||||
|
return fmt.Sprintf("return %[2]s, %[1]s.Unmarshal(&%[2]s)", data.ValName, data.Value), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func internal(data ParseData) (string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
err := tpl.ExecuteTemplate(&b, "parse.tmpl", data)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("execute parse.tmpl:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var typesIntreface = [...]reflect.Type{
|
||||||
|
reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(),
|
||||||
|
reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(),
|
||||||
|
}
|
||||||
|
|
||||||
|
func parser(data any) func(ParseData) (string, error) {
|
||||||
|
vtype := reflect.TypeOf(data)
|
||||||
|
name := vtype.Name()
|
||||||
|
|
||||||
|
if v, ok := data.(string); ok {
|
||||||
|
name = v
|
||||||
|
}
|
||||||
|
|
||||||
|
if vtype.Kind() == reflect.Slice {
|
||||||
|
return parses["any"]
|
||||||
|
}
|
||||||
|
|
||||||
|
if h, ok := parses[name]; ok {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, extypes := range typesIntreface {
|
||||||
|
if vtype.Implements(extypes) {
|
||||||
|
return parses[extypes.Name()]
|
||||||
|
}
|
||||||
|
|
||||||
|
if vtype.Kind() != reflect.Ptr && reflect.PointerTo(vtype).Implements(extypes) {
|
||||||
|
return parses[extypes.Name()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parses["any"]
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParseData struct {
|
||||||
|
Value string
|
||||||
|
ValName string
|
||||||
|
Keys []string
|
||||||
|
View
|
||||||
|
}
|
||||||
6
definition/option/view_params.go
Normal file
6
definition/option/view_params.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package option
|
||||||
|
|
||||||
|
const (
|
||||||
|
ViewParamFunctName = "view.funcName"
|
||||||
|
ViewParamDescription = "view.description"
|
||||||
|
)
|
||||||
27
definition/proto/proto.go
Normal file
27
definition/proto/proto.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package proto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
const Kind = "proto"
|
||||||
|
|
||||||
|
func New(name, desc string, opt definition.Option) Proto {
|
||||||
|
pr := Proto{
|
||||||
|
Name: name,
|
||||||
|
Description: desc,
|
||||||
|
Option: opt,
|
||||||
|
}
|
||||||
|
|
||||||
|
return pr
|
||||||
|
}
|
||||||
|
|
||||||
|
type Proto struct {
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Option definition.Option
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Proto) Kind() string {
|
||||||
|
return Kind
|
||||||
|
}
|
||||||
80
definition/proto/view.go
Normal file
80
definition/proto/view.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package proto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/generate"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoinits
|
||||||
|
func init() {
|
||||||
|
generate.MustAdd(Kind, handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handle(w io.Writer, data generate.Handler, opt definition.Option) error {
|
||||||
|
proto, ok := opt.(Proto)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
if viewOpt, ok := proto.Option.(option.Option); ok {
|
||||||
|
viewOpt = viewOpt.WithParams(
|
||||||
|
definition.Param{
|
||||||
|
Name: option.ViewParamFunctName,
|
||||||
|
Value: generate.FuncName(proto.Name) + generate.FuncName(viewOpt.Name),
|
||||||
|
},
|
||||||
|
definition.Param{
|
||||||
|
Name: option.ViewParamDescription,
|
||||||
|
Value: proto.Description + " " + viewOpt.Description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return option.Handle(tpl)(w, data, viewOpt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
|
var (
|
||||||
|
tpl = template.Must(template.New("tpls").Funcs(template.FuncMap{"join": strings.Join}).Parse(templateOption))
|
||||||
|
templateOption = `// read{{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context, key string) (v {{.Type}},e error) {
|
||||||
|
val, err := i.Value(ctx, {{ .ParentKeys }} key, "{{.Name}}")
|
||||||
|
if err != nil {
|
||||||
|
{{if .HasDefault}}
|
||||||
|
{{$default := .Default}}
|
||||||
|
{{range .DefaultErrors}}
|
||||||
|
if errors.Is(err,{{.}}){
|
||||||
|
return {{$default}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
return v, fmt.Errorf("read {{.Keys}}:%w",err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{{.Parse "val" "v" .Keys }}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read{{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context, key string) ({{.Type}}, error) {
|
||||||
|
return i.read{{.FuncName}}(ctx, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.FuncName}} {{.Description}}.
|
||||||
|
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context, {{end}} key string) {{.Type}} {
|
||||||
|
{{if not .Context}} ctx := context.Background() {{end}}
|
||||||
|
val, err := i.read{{.FuncName}}(ctx, key)
|
||||||
|
if err != nil {
|
||||||
|
i.log(ctx, "get {{.Keys}}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
`
|
||||||
|
)
|
||||||
32
example/definition/defenition.go
Normal file
32
example/definition/defenition.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/group"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/proto"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Configure(ctx context.Context, def *definition.Definition) error {
|
||||||
|
def.Add(
|
||||||
|
option.String("test", "test description", option.Default("defult")),
|
||||||
|
option.Int("int", "test int description", option.Default(2)),
|
||||||
|
group.New("group", "group description", option.String("test", "test description")),
|
||||||
|
option.Time("start", "start at", option.Default(time.Now())),
|
||||||
|
option.Duration("timer", "timer", option.Default(time.Hour)),
|
||||||
|
group.New("log", "logger",
|
||||||
|
option.New("level", "log level", level.Level(0), option.Default(level.Debug)),
|
||||||
|
option.New("logrus", "logrus level", logrus.Level(0), option.Default(logrus.DebugLevel)),
|
||||||
|
proto.New("sevice", "cutom service log", option.New("level", "log level", level.Level(0), option.Default(level.Debug))),
|
||||||
|
),
|
||||||
|
option.New("erors", "skiped errors", []string{}),
|
||||||
|
proto.New("proto_errors", "proto errors", option.New("erors", "skiped errors", []string{})),
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
61
example/definition/defenition_config_bootstrap.go
Normal file
61
example/definition/defenition_config_bootstrap.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
//go:build ignore
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"go/format"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/definition"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/generate"
|
||||||
|
"gitoa.ru/go-4devs/config/eample"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := run(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stdout, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
def := definition.New()
|
||||||
|
if err := eample.Configure(ctx, &def); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create("eample/defenition_input.go")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
gerr := generate.Run(f, "eample", def, generate.ViewOption{
|
||||||
|
Struct: "Configure",
|
||||||
|
Suffix: "Input",
|
||||||
|
Errors: generate.ViewErrors{
|
||||||
|
Default: []string{
|
||||||
|
"gitoa.ru/go-4devs/config.ErrVariableNotFound",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if gerr != nil {
|
||||||
|
return gerr
|
||||||
|
}
|
||||||
|
|
||||||
|
in, err := os.ReadFile(f.Name())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := format.Source(in)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(f.Name(), out, 0644)
|
||||||
|
}
|
||||||
14
example/go.mod
Normal file
14
example/go.mod
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/example
|
||||||
|
|
||||||
|
go 1.21.5
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8
|
||||||
|
gitoa.ru/go-4devs/log v0.5.3
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/iancoleman/strcase v0.3.0 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
|
)
|
||||||
21
example/go.sum
Normal file
21
example/go.sum
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||||
|
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8 h1:PZ4SE0gq719+lXorGaRPfGSSO2JpfxTWCWOSsna+7Yw=
|
||||||
|
gitoa.ru/go-4devs/config/definition v0.0.0-20240125203435-5586adc4e3d8/go.mod h1:jV6jF0PsK4ffNC8hrBxMx33PVJtnW5O6S/sGNJDqrd4=
|
||||||
|
gitoa.ru/go-4devs/log v0.5.3 h1:o/4DcypxbgQ9GEfUWrZ3FVrVfttuJgLs2ptMVPj47sE=
|
||||||
|
gitoa.ru/go-4devs/log v0.5.3/go.mod h1:tREtjEH2cTHl0p3uCVcH9g5tlqtsVNI/tDQVfq53Ty4=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
48
provider/etcd/etcd_test.go
Normal file
48
provider/etcd/etcd_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package etcd_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
client "go.etcd.io/etcd/client/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ConfigJSON = `{"duration":1260000000000,"enabled":true}`
|
||||||
|
|
||||||
|
func NewEtcd(ctx context.Context) (*client.Client, error) {
|
||||||
|
dsn, ok := os.LookupEnv("FDEVS_CONFIG_ETCD_HOST")
|
||||||
|
if !ok {
|
||||||
|
dsn = "127.0.0.1:2379"
|
||||||
|
}
|
||||||
|
|
||||||
|
et, err := client.New(client.Config{
|
||||||
|
Endpoints: []string{dsn},
|
||||||
|
DialTimeout: time.Second,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
values := map[string]string{
|
||||||
|
"fdevs/config/db_dsn": "pgsql://user@pass:127.0.0.1:5432",
|
||||||
|
"fdevs/config/duration": "12m",
|
||||||
|
"fdevs/config/port": "8080",
|
||||||
|
"fdevs/config/maintain": "true",
|
||||||
|
"fdevs/config/start_at": "2020-01-02T15:04:05Z",
|
||||||
|
"fdevs/config/percent": "0.064",
|
||||||
|
"fdevs/config/count": "2020",
|
||||||
|
"fdevs/config/int64": "2021",
|
||||||
|
"fdevs/config/uint64": "2022",
|
||||||
|
"fdevs/config/config": ConfigJSON,
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, val := range values {
|
||||||
|
_, err = et.KV.Put(ctx, name, val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return et, nil
|
||||||
|
}
|
||||||
32
provider/etcd/go.mod
Normal file
32
provider/etcd/go.mod
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/etcd
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/stretchr/testify v1.8.4
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2
|
||||||
|
go.etcd.io/etcd/api/v3 v3.5.11
|
||||||
|
go.etcd.io/etcd/client/v3 v3.5.11
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/coreos/go-semver v0.3.0 // indirect
|
||||||
|
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/golang/protobuf v1.5.3 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect
|
||||||
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
|
go.uber.org/zap v1.17.0 // indirect
|
||||||
|
golang.org/x/net v0.17.0 // indirect
|
||||||
|
golang.org/x/sys v0.13.0 // indirect
|
||||||
|
golang.org/x/text v0.13.0 // indirect
|
||||||
|
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||||
|
google.golang.org/grpc v1.59.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.31.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
97
provider/etcd/go.sum
Normal file
97
provider/etcd/go.sum
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
|
||||||
|
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||||
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||||
|
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||||
|
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
go.etcd.io/etcd/api/v3 v3.5.11 h1:B54KwXbWDHyD3XYAwprxNzTe7vlhR69LuBgZnMVvS7E=
|
||||||
|
go.etcd.io/etcd/api/v3 v3.5.11/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4=
|
||||||
|
go.etcd.io/etcd/client/pkg/v3 v3.5.11 h1:bT2xVspdiCj2910T0V+/KHcVKjkUrCZVtk8J2JF2z1A=
|
||||||
|
go.etcd.io/etcd/client/pkg/v3 v3.5.11/go.mod h1:seTzl2d9APP8R5Y2hFL3NVlD6qC/dOT+3kvrqPyTas4=
|
||||||
|
go.etcd.io/etcd/client/v3 v3.5.11 h1:ajWtgoNSZJ1gmS8k+icvPtqsqEav+iUorF7b0qozgUU=
|
||||||
|
go.etcd.io/etcd/client/v3 v3.5.11/go.mod h1:a6xQUEqFJ8vztO1agJh/KQKOMfFI8og52ZconzcDJwE=
|
||||||
|
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||||
|
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
|
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||||
|
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||||
|
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
|
||||||
|
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||||
|
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||||
|
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||||
|
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
|
||||||
|
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
||||||
|
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
|
||||||
|
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
|
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||||
|
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
116
provider/etcd/provider.go
Normal file
116
provider/etcd/provider.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"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)
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client interface {
|
||||||
|
client.KV
|
||||||
|
client.Watcher
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(namespace, appName string, client Client) *Provider {
|
||||||
|
prov := Provider{
|
||||||
|
client: client,
|
||||||
|
key: func(s ...string) string {
|
||||||
|
return strings.Join(s, Separator)
|
||||||
|
},
|
||||||
|
name: Name,
|
||||||
|
prefix: namespace + Separator + appName,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &prov
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct {
|
||||||
|
client Client
|
||||||
|
key func(...string) string
|
||||||
|
name string
|
||||||
|
prefix string
|
||||||
|
log func(context.Context, string, ...any)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err := p.resolve(name, resp.Kvs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 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 {
|
||||||
|
go func(ctx context.Context, key string, callback config.WatchCallback) {
|
||||||
|
watch := p.client.Watch(ctx, key, client.WithPrevKV(), client.WithPrefix())
|
||||||
|
for w := range watch {
|
||||||
|
kvs, olds := p.getEventKvs(w.Events)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ctx, p.Key(path), callback)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) getEventKvs(events []*client.Event) ([]*pb.KeyValue, []*pb.KeyValue) {
|
||||||
|
kvs := make([]*pb.KeyValue, 0, len(events))
|
||||||
|
old := make([]*pb.KeyValue, 0, len(events))
|
||||||
|
|
||||||
|
for i := range events {
|
||||||
|
kvs = append(kvs, events[i].Kv)
|
||||||
|
old = append(old, events[i].PrevKv)
|
||||||
|
}
|
||||||
|
|
||||||
|
return kvs, old
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Value, error) {
|
||||||
|
for _, kv := range kvs {
|
||||||
|
switch {
|
||||||
|
case kv == nil:
|
||||||
|
return nil, nil
|
||||||
|
case string(kv.Key) == key:
|
||||||
|
return value.JBytes(kv.Value), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("%w: name %s", config.ErrValueNotFound, key)
|
||||||
|
}
|
||||||
121
provider/etcd/provider_example_test.go
Normal file
121
provider/etcd/provider_example_test.go
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
package etcd_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/etcd"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleClient_Value() {
|
||||||
|
const (
|
||||||
|
namespace = "fdevs"
|
||||||
|
appName = "config"
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// configure etcd client
|
||||||
|
etcdClient, err := NewEtcd(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := config.New(
|
||||||
|
etcd.New(namespace, appName, etcdClient),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
enabled, err := config.Value(ctx, "maintain")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("maintain ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("maintain from etcd: %v\n", enabled.Bool())
|
||||||
|
// Output:
|
||||||
|
// maintain from etcd: true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleClient_Watch() {
|
||||||
|
const (
|
||||||
|
namespace = "fdevs"
|
||||||
|
appName = "config"
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// configure etcd client
|
||||||
|
etcdClient, err := NewEtcd(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "pgsql://user@pass:127.0.0.1:5432")
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
if _, err = etcdClient.KV.Delete(context.Background(), "fdevs/config/example_db_dsn"); err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
watcher, err := config.New(
|
||||||
|
etcd.New(namespace, appName, etcdClient),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
err = watcher.Watch(ctx, func(_ context.Context, oldVar, newVar config.Value) error {
|
||||||
|
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
|
||||||
|
wg.Done()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, "example_db_dsn")
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
time.AfterFunc(time.Second, func() {
|
||||||
|
if _, err := etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "mysql://localhost:5432"); err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// update example_db_dsn old: pgsql://user@pass:127.0.0.1:5432 new: mysql://localhost:5432
|
||||||
|
}
|
||||||
118
provider/etcd/provider_test.go
Normal file
118
provider/etcd/provider_test.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package etcd_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"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) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
et, err := NewEtcd(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
provider := etcd.New("fdevs", "config", 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.NewReadConfig("config"),
|
||||||
|
}
|
||||||
|
test.Run(t, provider, read)
|
||||||
|
}
|
||||||
|
|
||||||
|
func value(cnt int32) string {
|
||||||
|
return fmt.Sprintf("test data: %d", cnt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWatcher(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
key := "test_watch"
|
||||||
|
|
||||||
|
et, err := NewEtcd(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_, err = et.KV.Delete(context.Background(), "fdevs/config/test_watch")
|
||||||
|
require.NoError(t, err)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var cnt, cnt2 int32
|
||||||
|
|
||||||
|
prov := etcd.New("fdevs", "config", et)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(6)
|
||||||
|
|
||||||
|
watch := func(cnt *int32) config.WatchCallback {
|
||||||
|
return func(ctx context.Context, oldVar, newVar config.Value) error {
|
||||||
|
switch *cnt {
|
||||||
|
case 0:
|
||||||
|
assert.Equal(t, value(*cnt), newVar.String())
|
||||||
|
assert.Nil(t, oldVar)
|
||||||
|
case 1:
|
||||||
|
assert.Equal(t, value(*cnt), newVar.String())
|
||||||
|
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||||
|
case 2:
|
||||||
|
_, perr := newVar.ParseString()
|
||||||
|
require.NoError(t, perr)
|
||||||
|
assert.Equal(t, "", newVar.String())
|
||||||
|
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||||
|
default:
|
||||||
|
t.Error("unexpected watch")
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
atomic.AddInt32(cnt, 1)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = prov.Watch(ctx, watch(&cnt), key)
|
||||||
|
err = prov.Watch(ctx, watch(&cnt2), key)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
time.AfterFunc(time.Second, func() {
|
||||||
|
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(0))
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(1))
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = et.KV.Delete(ctx, "fdevs/config/test_watch")
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
time.AfterFunc(time.Second*10, func() {
|
||||||
|
assert.Fail(t, "failed watch after 5 sec")
|
||||||
|
cancel()
|
||||||
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
}
|
||||||
1038
provider/ini/fixture/config.ini
Normal file
1038
provider/ini/fixture/config.ini
Normal file
File diff suppressed because it is too large
Load Diff
15
provider/ini/go.mod
Normal file
15
provider/ini/go.mod
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/ini
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/stretchr/testify v1.8.4
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1
|
||||||
|
gopkg.in/ini.v1 v1.67.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
14
provider/ini/go.sum
Normal file
14
provider/ini/go.sum
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||||
|
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
58
provider/ini/provider.go
Normal file
58
provider/ini/provider.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package ini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/value"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Name = "ini"
|
||||||
|
Separator = "."
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ config.Provider = (*Provider)(nil)
|
||||||
|
|
||||||
|
func New(data *ini.File) *Provider {
|
||||||
|
return &Provider{
|
||||||
|
data: data,
|
||||||
|
resolve: func(path []string) (string, string) {
|
||||||
|
if len(path) == 1 {
|
||||||
|
return "", path[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(path[:len(path)-1], Separator), strings.ToUpper(path[len(path)-1])
|
||||||
|
},
|
||||||
|
name: Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct {
|
||||||
|
data *ini.File
|
||||||
|
resolve func(path []string) (string, string)
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||||
|
section, name := p.resolve(path)
|
||||||
|
|
||||||
|
iniSection, err := p.data.GetSection(section)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
iniKey, err := iniSection.GetKey(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value.JString(iniKey.String()), nil
|
||||||
|
}
|
||||||
38
provider/ini/provider_test.go
Normal file
38
provider/ini/provider_test.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package ini_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/ini"
|
||||||
|
"gitoa.ru/go-4devs/config/test"
|
||||||
|
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)
|
||||||
|
|
||||||
|
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"),
|
||||||
|
}
|
||||||
|
|
||||||
|
prov := ini.New(file)
|
||||||
|
test.Run(t, prov, read)
|
||||||
|
}
|
||||||
17
provider/json/fixture/config.json
Normal file
17
provider/json/fixture/config.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"app": {
|
||||||
|
"name": {
|
||||||
|
"var": [
|
||||||
|
"name"
|
||||||
|
],
|
||||||
|
"title": "config title",
|
||||||
|
"timeout": "1m",
|
||||||
|
"success": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cfg": {
|
||||||
|
"duration": 1260000000000,
|
||||||
|
"enabled": true,
|
||||||
|
"type":"json"
|
||||||
|
}
|
||||||
|
}
|
||||||
13
provider/json/go.mod
Normal file
13
provider/json/go.mod
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/json
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/tidwall/gjson v1.17.0
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
|
)
|
||||||
10
provider/json/go.sum
Normal file
10
provider/json/go.sum
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
|
||||||
|
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a h1:61iwpn1Ec4yIkBSvSz8knNENJuhj6v2rp6bfw1wkG0E=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a/go.mod h1:3g2bwE2OTDyYwm33KN/Cqc8pEdGlWXnB8Ju3PsYNQr0=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
65
provider/json/provider.go
Normal file
65
provider/json/provider.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"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)
|
||||||
|
},
|
||||||
|
data: json,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&provider)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &provider
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFile(path string, opts ...Option) (*Provider, error) {
|
||||||
|
file, err := os.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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return New(file, opts...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Provider)
|
||||||
|
|
||||||
|
type Provider struct {
|
||||||
|
data []byte
|
||||||
|
key func(...string) string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrValueNotFound)
|
||||||
|
}
|
||||||
55
provider/json/provider_example_test.go
Normal file
55
provider/json/provider_example_test.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package json_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/json"
|
||||||
|
"gitoa.ru/go-4devs/config/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleClient_Value() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// read json config
|
||||||
|
jsonConfig, jerr := fixture.ReadFile("fixture/config.json")
|
||||||
|
if jerr != nil {
|
||||||
|
log.Printf("failed load file:%v", jerr)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := config.New(
|
||||||
|
json.New(jsonConfig),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
title, err := config.Value(ctx, "app.name.title")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("app.name.title", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfgValue, err := config.Value(ctx, "cfg")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("cfg ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := test.Config{}
|
||||||
|
_ = cfgValue.Unmarshal(&cfg)
|
||||||
|
|
||||||
|
fmt.Printf("title from json: %v\n", title.String())
|
||||||
|
fmt.Printf("struct from json: %+v\n", cfg)
|
||||||
|
// Output:
|
||||||
|
// title from json: config title
|
||||||
|
// struct from json: {Duration:21m0s Enabled:true}
|
||||||
|
}
|
||||||
33
provider/json/provider_test.go
Normal file
33
provider/json/provider_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package json_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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)
|
||||||
|
|
||||||
|
prov := json.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.NewReadConfig("cfg"),
|
||||||
|
test.NewRead(true, "app", "name", "success"),
|
||||||
|
}
|
||||||
|
|
||||||
|
test.Run(t, prov, read)
|
||||||
|
}
|
||||||
31
provider/toml/fixture/config.toml
Normal file
31
provider/toml/fixture/config.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
title = "TOML Example"
|
||||||
|
|
||||||
|
[owner]
|
||||||
|
name = "Tom Preston-Werner"
|
||||||
|
dob = 1979-05-27T07:32:00-08:00 # First class dates
|
||||||
|
|
||||||
|
[database]
|
||||||
|
server = "192.168.1.1"
|
||||||
|
ports = [ 8001, 8001, 8002 ]
|
||||||
|
connection_max = 5000
|
||||||
|
enabled = true
|
||||||
|
|
||||||
|
[servers]
|
||||||
|
|
||||||
|
# Indentation (tabs and/or spaces) is allowed but not required
|
||||||
|
[servers.alpha]
|
||||||
|
ip = "10.0.0.1"
|
||||||
|
dc = "eqdc10"
|
||||||
|
|
||||||
|
[servers.beta]
|
||||||
|
ip = "10.0.0.2"
|
||||||
|
dc = "eqdc10"
|
||||||
|
|
||||||
|
[clients]
|
||||||
|
data = [ ["gamma", "delta"], [1, 2] ]
|
||||||
|
|
||||||
|
# Line breaks are OK when inside arrays
|
||||||
|
hosts = [
|
||||||
|
"alpha",
|
||||||
|
"omega"
|
||||||
|
]
|
||||||
15
provider/toml/go.mod
Normal file
15
provider/toml/go.mod
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/toml
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/pelletier/go-toml v1.9.5
|
||||||
|
github.com/stretchr/testify v1.8.4
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
14
provider/toml/go.sum
Normal file
14
provider/toml/go.sum
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||||
|
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
71
provider/toml/provider.go
Normal file
71
provider/toml/provider.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package toml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/value"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Name = "toml"
|
||||||
|
Separator = "."
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ config.Provider = (*Provider)(nil)
|
||||||
|
|
||||||
|
func NewFile(file string, opts ...Option) (*Provider, error) {
|
||||||
|
tree, err := toml.LoadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("toml: failed load file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configure(tree, opts...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(prov)
|
||||||
|
}
|
||||||
|
|
||||||
|
return prov
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(data []byte, opts ...Option) (*Provider, error) {
|
||||||
|
tree, err := toml.LoadBytes(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("toml failed load data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configure(tree, opts...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct {
|
||||||
|
tree *toml.Tree
|
||||||
|
key func([]string) string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, config.ErrValueNotFound
|
||||||
|
}
|
||||||
36
provider/toml/provider_test.go
Normal file
36
provider/toml/provider_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package toml_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"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)
|
||||||
|
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.Run(t, prov, read)
|
||||||
|
}
|
||||||
41
provider/toml/value.go
Normal file
41
provider/toml/value.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package toml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/value"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Value struct {
|
||||||
|
value.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Value) Int() int {
|
||||||
|
v, _ := s.ParseInt()
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Value) ParseInt() (int, error) {
|
||||||
|
v, err := s.ParseInt64()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("toml failed parce int: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Value) Unmarshal(target interface{}) error {
|
||||||
|
b, err := json.Marshal(s.Raw())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, target); err != nil {
|
||||||
|
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
29
provider/vault/go.mod
Normal file
29
provider/vault/go.mod
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/vault
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/hashicorp/vault/api v1.11.0
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
|
||||||
|
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
|
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
|
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
|
||||||
|
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||||
|
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
|
||||||
|
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
|
||||||
|
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
|
||||||
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
|
github.com/ryanuber/go-glob v1.0.0 // indirect
|
||||||
|
golang.org/x/crypto v0.17.0 // indirect
|
||||||
|
golang.org/x/net v0.17.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
|
||||||
|
)
|
||||||
92
provider/vault/go.sum
Normal file
92
provider/vault/go.sum
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
|
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||||
|
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
|
||||||
|
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||||
|
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||||
|
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
|
||||||
|
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
|
||||||
|
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
|
||||||
|
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||||
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||||
|
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||||
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||||
|
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
|
||||||
|
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
|
||||||
|
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
|
||||||
|
github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs=
|
||||||
|
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
|
||||||
|
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||||
|
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
|
||||||
|
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
|
||||||
|
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
|
||||||
|
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
|
||||||
|
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 h1:om4Al8Oy7kCm/B86rLCLah4Dt5Aa0Fr5rYBG60OzwHQ=
|
||||||
|
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8=
|
||||||
|
github.com/hashicorp/go-secure-stdlib/strutil v0.1.1/go.mod h1:gKOamz3EwoIoJq7mlMIRBpVTAUn8qPCrEclOKKWhD3U=
|
||||||
|
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts=
|
||||||
|
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
|
||||||
|
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
|
||||||
|
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
|
||||||
|
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
|
github.com/hashicorp/vault/api v1.11.0 h1:AChWByeHf4/P9sX3Y1B7vFsQhZO2BgQiCMQ2SA1P1UY=
|
||||||
|
github.com/hashicorp/vault/api v1.11.0/go.mod h1:si+lJCYO7oGkIoNPAN8j3azBLTn9SjMGS+jFaHd1Cck=
|
||||||
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
|
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
|
||||||
|
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
|
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
|
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
|
||||||
|
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
|
||||||
|
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||||
|
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
|
||||||
|
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
||||||
|
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||||
|
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||||
|
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
|
||||||
|
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
120
provider/vault/provider.go
Normal file
120
provider/vault/provider.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
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) 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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]interface{})
|
||||||
|
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
|
||||||
|
}
|
||||||
47
provider/vault/provider_example_test.go
Normal file
47
provider/vault/provider_example_test.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package vault_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/provider/vault"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleClient_Value() {
|
||||||
|
const (
|
||||||
|
namespace = "fdevs"
|
||||||
|
appName = "config"
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// configure vault client
|
||||||
|
vaultClient, err := NewVault()
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := config.New(
|
||||||
|
vault.New(namespace, appName, vaultClient),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn, err := config.Value(ctx, "example", "dsn")
|
||||||
|
if err != nil {
|
||||||
|
log.Print("example:dsn ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("dsn from vault: %s\n", dsn.String())
|
||||||
|
// Output:
|
||||||
|
// dsn from vault: pgsql://user@pass:127.0.0.1:5432
|
||||||
|
}
|
||||||
26
provider/vault/provider_test.go
Normal file
26
provider/vault/provider_test.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package vault_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
provider := vault.New("fdevs", "config", cl)
|
||||||
|
|
||||||
|
read := []test.Read{
|
||||||
|
test.NewReadConfig("database"),
|
||||||
|
test.NewRead(test.DSN, "db", "dsn"),
|
||||||
|
test.NewRead(time.Minute, "db", "timeout"),
|
||||||
|
}
|
||||||
|
test.Run(t, provider, read)
|
||||||
|
}
|
||||||
90
provider/vault/vault_test.go
Normal file
90
provider/vault/vault_test.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package vault_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/hashicorp/vault/api"
|
||||||
|
"gitoa.ru/go-4devs/config/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
const token = "dev"
|
||||||
|
|
||||||
|
func NewVault() (*api.Client, error) {
|
||||||
|
address, ok := os.LookupEnv("VAULT_DEV_LISTEN_ADDRESS")
|
||||||
|
if !ok {
|
||||||
|
address = "http://127.0.0.1:8200"
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenID, ok := os.LookupEnv("VAULT_DEV_ROOT_TOKEN_ID")
|
||||||
|
if !ok {
|
||||||
|
tokenID = token
|
||||||
|
}
|
||||||
|
|
||||||
|
cl, err := api.NewClient(&api.Config{
|
||||||
|
Address: address,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.SetToken(tokenID)
|
||||||
|
|
||||||
|
values := map[string]map[string]interface{}{
|
||||||
|
"database": {
|
||||||
|
"duration": 1260000000000,
|
||||||
|
"enabled": true,
|
||||||
|
},
|
||||||
|
"db": {
|
||||||
|
"dsn": test.DSN,
|
||||||
|
"timeout": "60s",
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"dsn": test.DSN,
|
||||||
|
"timeout": "60s",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, val := range values {
|
||||||
|
if err := create(address, tokenID, name, val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cl, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func create(host, token, path string, data map[string]interface{}) error {
|
||||||
|
type Req struct {
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(Req{Data: data})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
body := bytes.NewBuffer(b)
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(
|
||||||
|
context.Background(),
|
||||||
|
http.MethodPost,
|
||||||
|
host+"/v1/secret/data/fdevs/config/"+path,
|
||||||
|
body,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("X-Vault-Token", token)
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.Body.Close()
|
||||||
|
}
|
||||||
14
provider/yaml/fixture/config.yaml
Normal file
14
provider/yaml/fixture/config.yaml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
app:
|
||||||
|
title: yaml title
|
||||||
|
name:
|
||||||
|
var:
|
||||||
|
- test
|
||||||
|
bool_var: true
|
||||||
|
duration_var: 21m
|
||||||
|
empty_var:
|
||||||
|
url_var: "http://google.com/"
|
||||||
|
time_var: "2020-01-02T15:04:05Z"
|
||||||
|
cfg:
|
||||||
|
duration: 21m
|
||||||
|
enabled: true
|
||||||
|
type: yaml
|
||||||
8
provider/yaml/go.mod
Normal file
8
provider/yaml/go.mod
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module gitoa.ru/go-4devs/config/provider/yaml
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
6
provider/yaml/go.sum
Normal file
6
provider/yaml/go.sum
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
|
||||||
|
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
100
provider/yaml/provider.go
Normal file
100
provider/yaml/provider.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/value"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Name = "yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ config.Provider = (*Provider)(nil)
|
||||||
|
|
||||||
|
func NewFile(name string, opts ...Option) (*Provider, error) {
|
||||||
|
in, err := os.ReadFile(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("yaml_file: read error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return New(in, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(yml []byte, opts ...Option) (*Provider, error) {
|
||||||
|
var data yaml.Node
|
||||||
|
if err := yaml.Unmarshal(yml, &data); err != nil {
|
||||||
|
return nil, fmt.Errorf("yaml: unmarshal err: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return create(opts...).With(&data), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func create(opts ...Option) *Provider {
|
||||||
|
prov := Provider{
|
||||||
|
name: Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&prov)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &prov
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Provider)
|
||||||
|
|
||||||
|
type Provider struct {
|
||||||
|
data node
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||||
|
return p.data.read(p.Name(), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) With(data *yaml.Node) *Provider {
|
||||||
|
return &Provider{
|
||||||
|
data: node{Node: data},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
*yaml.Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) read(name string, keys []string) (config.Value, error) {
|
||||||
|
val, err := getData(n.Node.Content[0].Content, keys)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, config.ErrValueNotFound) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", config.ErrValueNotFound, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("%w: %s", err, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value.Decode(val), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error) {
|
||||||
|
for idx := len(node) - 1; idx > 0; idx -= 2 {
|
||||||
|
if node[idx-1].Value == keys[0] {
|
||||||
|
if len(keys) > 1 {
|
||||||
|
return getData(node[idx].Content, keys[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
return node[idx].Decode, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, config.ErrValueNotFound
|
||||||
|
}
|
||||||
32
provider/yaml/provider_test.go
Normal file
32
provider/yaml/provider_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package yaml_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/provider/yaml"
|
||||||
|
"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()
|
||||||
|
|
||||||
|
data, err := fixture.ReadFile("fixture/config.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
prov, err := yaml.New(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
read := []test.Read{
|
||||||
|
test.NewRead(21*time.Minute, "duration_var"),
|
||||||
|
test.NewRead(true, "app", "name", "bool_var"),
|
||||||
|
test.NewRead(test.Time("2020-01-02T15:04:05Z"), "time_var"),
|
||||||
|
test.NewReadConfig("cfg"),
|
||||||
|
}
|
||||||
|
|
||||||
|
test.Run(t, prov, read)
|
||||||
|
}
|
||||||
46
provider/yaml/watch.go
Normal file
46
provider/yaml/watch.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const NameWatch = "yaml_watch"
|
||||||
|
|
||||||
|
func NewWatch(name string, opts ...Option) *Watch {
|
||||||
|
wath := Watch{
|
||||||
|
file: name,
|
||||||
|
prov: create(opts...),
|
||||||
|
name: NameWatch,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &wath
|
||||||
|
}
|
||||||
|
|
||||||
|
type Watch struct {
|
||||||
|
file string
|
||||||
|
prov *Provider
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Watch) Name() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Watch) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||||
|
in, err := os.ReadFile(p.file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("yaml_file: read error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var yNode yaml.Node
|
||||||
|
if err = yaml.Unmarshal(in, &yNode); err != nil {
|
||||||
|
return nil, fmt.Errorf("yaml_file: unmarshal error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.prov.With(&yNode).Value(ctx, path...)
|
||||||
|
}
|
||||||
30
test/assert/equal.go
Normal file
30
test/assert/equal.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package assert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if reflect.DeepEqual(expected, actual) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Error(msgAndArgs...)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if reflect.DeepEqual(expected, actual) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf(msg, args...)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
15
test/assert/nil.go
Normal file
15
test/assert/nil.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package assert
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Nil(t *testing.T, data any, msgAndArgs ...interface{}) bool {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if data != nil {
|
||||||
|
t.Error(msgAndArgs...)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -1,25 +1,25 @@
|
|||||||
package require
|
package require
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config/test/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
|
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if reflect.DeepEqual(expected, actual) {
|
if assert.Equal(t, expected, actual, msgAndArgs...) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Error(msgAndArgs...)
|
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
|
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if reflect.DeepEqual(expected, actual) {
|
if assert.Equalf(t, expected, actual, msg, args...) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user