Compare commits
5 Commits
v0.0.1
...
5cb46f5030
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cb46f5030 | |||
| dd37b51974 | |||
| 1088e26bcf | |||
| 089f43dcb9 | |||
| 59f97e1681 |
56
.drone.yml
56
.drone.yml
@@ -1,3 +1,4 @@
|
||||
---
|
||||
kind: pipeline
|
||||
name: default
|
||||
|
||||
@@ -11,10 +12,63 @@ steps:
|
||||
image: golang
|
||||
commands:
|
||||
# - go test -parallel 10 -race ./...
|
||||
# - go test -race ./...
|
||||
- go test ./...
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.55
|
||||
commands:
|
||||
- golangci-lint run
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: json
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
commands:
|
||||
- cd provider/json
|
||||
- go test ./...
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.55
|
||||
commands:
|
||||
- cd provider/json
|
||||
- golangci-lint run
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: yaml
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
commands:
|
||||
- cd provider/yaml
|
||||
- go test ./...
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.55
|
||||
commands:
|
||||
- cd provider/yaml
|
||||
- golangci-lint run
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: ini
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
|
||||
commands:
|
||||
- cd provider/ini
|
||||
- go test ./...
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.55
|
||||
commands:
|
||||
- cd provider/ini
|
||||
- golangci-lint run
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
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...)
|
||||
}
|
||||
Reference in New Issue
Block a user