add test arg bind
All checks were successful
Go Action / goaction (pull_request) Successful in 1m18s
All checks were successful
Go Action / goaction (pull_request) Successful in 1m18s
This commit is contained in:
@@ -19,6 +19,7 @@ const (
|
||||
dash = `-`
|
||||
)
|
||||
|
||||
// Deprecated: use WithArgs.
|
||||
func WithSkip(skip int) func(*Argv) {
|
||||
return func(ar *Argv) {
|
||||
res := 2
|
||||
@@ -34,24 +35,27 @@ func WithSkip(skip int) func(*Argv) {
|
||||
res = 1
|
||||
}
|
||||
|
||||
ar.skip = res
|
||||
ar.args = os.Args[res:]
|
||||
}
|
||||
}
|
||||
|
||||
func WithArgs(args []string) func(*Argv) {
|
||||
return func(a *Argv) {
|
||||
a.args = args
|
||||
}
|
||||
}
|
||||
|
||||
func New(opts ...func(*Argv)) *Argv {
|
||||
arg := &Argv{
|
||||
args: nil,
|
||||
args: os.Args[1:],
|
||||
pos: 0,
|
||||
Map: memory.Map{},
|
||||
skip: 1,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(arg)
|
||||
}
|
||||
|
||||
arg.args = os.Args[arg.skip:]
|
||||
|
||||
return arg
|
||||
}
|
||||
|
||||
@@ -60,7 +64,6 @@ type Argv struct {
|
||||
|
||||
args []string
|
||||
pos uint64
|
||||
skip int
|
||||
}
|
||||
|
||||
func (i *Argv) Value(ctx context.Context, key ...string) (config.Value, error) {
|
||||
@@ -164,7 +167,7 @@ func (i *Argv) parseShortOption(arg string, def config.Variables) error {
|
||||
var value string
|
||||
|
||||
if len(name) > 1 {
|
||||
name, value = arg[0:1], arg[1:]
|
||||
name, value = arg[0:1], strings.TrimSpace(arg[1:])
|
||||
}
|
||||
|
||||
opt, err := def.ByParam(option.HasShort(name))
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
package arg_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"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/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
args := os.Args
|
||||
|
||||
defer func() {
|
||||
os.Args = args
|
||||
}()
|
||||
|
||||
os.Args = []string{
|
||||
"main.go",
|
||||
args := []string{
|
||||
"--listen=8080",
|
||||
"--config=config.hcl",
|
||||
"--url=http://4devs.io",
|
||||
@@ -44,11 +43,65 @@ func TestProvider(t *testing.T) {
|
||||
}, &[]time.Time{}, "end-after"),
|
||||
}
|
||||
|
||||
prov := arg.New()
|
||||
prov := arg.New(arg.WithArgs(args))
|
||||
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
|
||||
func TestProviderBind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
args := []string{
|
||||
"-p 8080",
|
||||
"--config=config.hcl",
|
||||
"-u http://4devs.io",
|
||||
"--url=https://4devs.io",
|
||||
"-t 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(8080, "listen"),
|
||||
test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"),
|
||||
test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"),
|
||||
test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"),
|
||||
test.NewReadUnmarshal(&[]time.Time{
|
||||
test.Time("2009-01-02T15:04:05Z"),
|
||||
test.Time("2008-01-02T15:04:05+03:00"),
|
||||
}, &[]time.Time{}, "end", "after"),
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
prov := arg.New(arg.WithArgs(args))
|
||||
require.NoError(t, prov.Bind(ctx, testVariables(t)))
|
||||
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
|
||||
func testVariables(t *testing.T) config.Variables {
|
||||
t.Helper()
|
||||
|
||||
def := definition.New()
|
||||
def.Add(
|
||||
option.Int("listen", "listen", option.Short('p')),
|
||||
option.String("config", "config"),
|
||||
option.String("url", "url", option.Short('u'), option.Slice),
|
||||
option.Duration("timeout", "timeout", option.Short('t'), option.Slice),
|
||||
option.Time("start-at", "start at"),
|
||||
group.New("end", "end at",
|
||||
option.Time("after", "after", option.Slice),
|
||||
proto.New("service", "service after",
|
||||
option.Time("after", "after"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
return config.NewVars(def)
|
||||
}
|
||||
|
||||
type Duration struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
13
variable.go
13
variable.go
@@ -8,9 +8,14 @@ import (
|
||||
)
|
||||
|
||||
func NewVar(opt Option) Variable {
|
||||
names := make([]string, 0, 1)
|
||||
if name := opt.Name(); name != "" {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
return Variable{
|
||||
param: opt,
|
||||
names: []string{opt.Name()},
|
||||
names: names,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +63,12 @@ func newVars(opts ...Option) []Variable {
|
||||
one := NewVar(opt)
|
||||
switch data := opt.(type) {
|
||||
case Group:
|
||||
if data.Name() == "" {
|
||||
vars = append(vars, newVars(data.Options()...)...)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
vars = append(vars, groupVars(one, data.Options()...)...)
|
||||
default:
|
||||
vars = append(vars, one)
|
||||
|
||||
Reference in New Issue
Block a user