first commit
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
andrey1s
2021-04-27 14:58:19 +03:00
commit 913ca9672d
55 changed files with 4355 additions and 0 deletions

136
provider/arg/provider.go Normal file
View File

@@ -0,0 +1,136 @@
package arg
import (
"context"
"fmt"
"os"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
"gopkg.in/yaml.v3"
)
var _ config.Provider = (*Provider)(nil)
type Option func(*Provider)
func WithKeyFactory(factory config.KeyFactory) Option {
return func(p *Provider) { p.key = factory }
}
func New(opts ...Option) *Provider {
p := Provider{
key: key.Name,
}
for _, opt := range opts {
opt(&p)
}
return &p
}
type Provider struct {
args map[string][]string
key config.KeyFactory
}
// nolint: cyclop
func (p *Provider) parseOne(arg string) (name, val string, err error) {
if arg[0] != '-' {
return
}
numMinuses := 1
if arg[1] == '-' {
numMinuses++
}
name = strings.TrimSpace(arg[numMinuses:])
if len(name) == 0 {
return
}
if name[0] == '-' || name[0] == '=' {
return "", "", fmt.Errorf("%w: bad flag syntax: %s", config.ErrInvalidValue, arg)
}
for i := 1; i < len(name); i++ {
if name[i] == '=' || name[i] == ' ' {
val = strings.TrimSpace(name[i+1:])
name = name[0:i]
break
}
}
if val == "" && numMinuses == 1 && len(arg) > 2 {
name, val = name[:1], name[1:]
}
return name, val, nil
}
func (p *Provider) parse() error {
if len(p.args) > 0 {
return nil
}
p.args = make(map[string][]string, len(os.Args[1:]))
for _, arg := range os.Args[1:] {
name, value, err := p.parseOne(arg)
if err != nil {
return err
}
if name != "" {
p.args[name] = append(p.args[name], value)
}
}
return nil
}
func (p *Provider) Name() string {
return "arg"
}
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
return p.key(ctx, key) != ""
}
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
if err := p.parse(); err != nil {
return config.Variable{Provider: p.Name()}, err
}
k := p.key(ctx, key)
if val, ok := p.args[k]; ok {
switch {
case len(val) == 1:
return config.Variable{
Name: k,
Provider: p.Name(),
Value: value.JString(val[0]),
}, nil
default:
var n yaml.Node
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &n); err != nil {
return config.Variable{}, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
}
return config.Variable{
Name: k,
Provider: p.Name(),
Value: value.Decode(n.Decode),
}, nil
}
}
return config.Variable{Name: k, Provider: p.Name()}, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,48 @@
package arg_test
import (
"os"
"testing"
"time"
"gitoa.ru/go-4devs/config/provider/arg"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{
"main.go",
"--listen=8080",
"--config=config.hcl",
"--url=http://4devs.io",
"--url=https://4devs.io",
"--timeout=1m",
"--timeout=1h",
"--start-at=2010-01-02T15:04:05Z",
"--end-after=2009-01-02T15:04:05Z",
"--end-after=2008-01-02T15:04:05+03:00",
}
read := []test.Read{
test.NewRead("listen", 8080),
test.NewRead("config", "config.hcl"),
test.NewRead("start-at", test.Time("2010-01-02T15:04:05Z")),
test.NewReadUnmarshal("url", &[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}),
test.NewReadUnmarshal("timeout", &[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}),
test.NewReadUnmarshal("end-after", &[]time.Time{
test.Time("2009-01-02T15:04:05Z"),
test.Time("2008-01-02T15:04:05+03:00"),
}, &[]time.Time{}),
}
prov := arg.New()
test.Run(t, prov, read)
}