Compare commits
15 Commits
input/cfg/
...
517895e319
| Author | SHA1 | Date | |
|---|---|---|---|
| 517895e319 | |||
| e48a9b29d8 | |||
| 71f774aa5a | |||
| c6a6300edf | |||
| c9b4f4cfd0 | |||
| d2ef3f7d0a | |||
| 3b32bb2759 | |||
| ad5cf18535 | |||
|
|
1151e7c3ad | ||
| 44d8837dbc | |||
|
|
0b6a6ee99b | ||
| 7771ff495d | |||
|
|
662cbdb510 | ||
| 65a754363f | |||
| ef4d5d126a |
24
.drone.yml
24
.drone.yml
@@ -1,24 +0,0 @@
|
||||
kind: pipeline
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.49
|
||||
volumes:
|
||||
- name: deps
|
||||
path: /go/src/mod
|
||||
commands:
|
||||
- golangci-lint run --timeout 5m
|
||||
|
||||
- name: test
|
||||
image: golang
|
||||
volumes:
|
||||
- name: deps
|
||||
path: /go/src/mod
|
||||
commands:
|
||||
- go test ./...
|
||||
|
||||
volumes:
|
||||
- name: deps
|
||||
temp: {}
|
||||
|
||||
34
.gitea/workflows/example.yml
Normal file
34
.gitea/workflows/example.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
name: Go Action
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
paths:
|
||||
- 'example/**'
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
working-directory: example
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
working-directory: example
|
||||
|
||||
32
.gitea/workflows/goaction.yml
Normal file
32
.gitea/workflows/goaction.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
name: Go Action
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'example/**'
|
||||
|
||||
jobs:
|
||||
goaction:
|
||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4 # Action to clone the repo
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||
with:
|
||||
go-version: '1.25.5' # Specify your required Go version
|
||||
|
||||
- name: Run golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
||||
with:
|
||||
version: v2.7.2 # Specify the linter version
|
||||
# Optional: additional arguments
|
||||
args: --verbose
|
||||
|
||||
- name: Run go test
|
||||
run: go test ./...
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
linters-settings:
|
||||
version: "2"
|
||||
linters:
|
||||
default: all
|
||||
disable:
|
||||
- exhaustruct
|
||||
- gomoddirectives
|
||||
- ireturn
|
||||
- wsl
|
||||
- noinlineerr
|
||||
- depguard
|
||||
settings:
|
||||
funcorder:
|
||||
constructor: false
|
||||
dupl:
|
||||
threshold: 100
|
||||
funlen:
|
||||
@@ -9,53 +21,44 @@ linters-settings:
|
||||
min-occurrences: 2
|
||||
gocyclo:
|
||||
min-complexity: 15
|
||||
golint:
|
||||
min-confidence: 0
|
||||
gomnd:
|
||||
settings:
|
||||
mnd:
|
||||
# don't include the "operation" and "assign"
|
||||
checks: argument,case,condition,return
|
||||
ignored-functions:
|
||||
- "strconv.*"
|
||||
- "strings.*"
|
||||
govet:
|
||||
check-shadowing: true
|
||||
lll:
|
||||
line-length: 140
|
||||
maligned:
|
||||
suggest-new: true
|
||||
misspell:
|
||||
locale: US
|
||||
locale: US
|
||||
staticcheck:
|
||||
checks:
|
||||
- all
|
||||
- -SA1030
|
||||
varnamelen:
|
||||
min-name-length: 2
|
||||
staticcheck:
|
||||
checks: ["all","-SA1030"]
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- varcheck
|
||||
- maligned
|
||||
- scopelint
|
||||
- nosnakecase
|
||||
- ifshort
|
||||
- golint
|
||||
- interfacer
|
||||
- structcheck
|
||||
- deadcode
|
||||
- exhaustivestruct
|
||||
|
||||
- ireturn
|
||||
- exhaustruct
|
||||
|
||||
issues:
|
||||
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||
exclude-rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gomnd
|
||||
- varnamelen
|
||||
- path: input/variable
|
||||
linters:
|
||||
- dupl
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- legacy
|
||||
- std-error-handling
|
||||
rules:
|
||||
- linters:
|
||||
- mnd
|
||||
- varnamelen
|
||||
path: _test\.go
|
||||
- linters:
|
||||
- dupl
|
||||
path: input/variable
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
formatters:
|
||||
enable:
|
||||
- gci
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goimports
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
|
||||
39
README.md
39
README.md
@@ -1,7 +1,6 @@
|
||||
# Console
|
||||
|
||||
|
||||
[](https://drone.gitoa.ru/go-4devs/console)
|
||||

|
||||
[](https://goreportcard.com/report/gitoa.ru/go-4devs/console)
|
||||
[](http://godoc.org/gitoa.ru/go-4devs/console)
|
||||
|
||||
@@ -17,14 +16,14 @@ import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
)
|
||||
|
||||
func CreateUser() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "app:create-user",
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -49,12 +48,14 @@ func CreateUser() *console.Command {
|
||||
func CreateUser(required bool) *console.Command {
|
||||
return &console.Command{
|
||||
//....
|
||||
Configure: func(ctx context.Context, cfg *input.Definition) error {
|
||||
var opts []func(*input.Argument)
|
||||
Configure: func(ctx context.Context, cfg config.Definition) error {
|
||||
var opts []func(*arg.Option)
|
||||
if required {
|
||||
opts = append(opts, argument.Required)
|
||||
opts = append(opts, arg.Required)
|
||||
}
|
||||
cfg.SetArgument("password", "User password", opts...)
|
||||
cfg.Add(
|
||||
arg.String("password", "User password", opts...)
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
@@ -98,7 +99,7 @@ The Execute field has access to the output stream to write messages to the conso
|
||||
func CreateUser(required bool) *console.Command {
|
||||
return &console.Command{
|
||||
// ....
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
// outputs a message followed by a "\n"
|
||||
out.Println(ctx, "User Creator")
|
||||
out.Println(ctx, "Whoa!")
|
||||
@@ -128,21 +129,23 @@ Use input options or arguments to pass information to the command:
|
||||
```go
|
||||
func CreateUser(required bool) *console.Command {
|
||||
return &console.Command{
|
||||
Configure: func(ctx context.Context, cfg *input.Definition) error {
|
||||
Configure: func(ctx context.Context, cfg config.Definition) error {
|
||||
var opts []func(*input.Argument)
|
||||
if required {
|
||||
opts = append(opts, argument.Required)
|
||||
}
|
||||
cfg.
|
||||
SetArgument("username", "The username of the user.", argument.Required).
|
||||
SetArgument("password", "User password", opts...)
|
||||
cfg.Add(
|
||||
arg.String("username", "The username of the user.", arg.Required),
|
||||
arg.String("password", "User password", opts...),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
// outputs a message followed by a "\n"
|
||||
username, _ := in.Value(ctx, "username")
|
||||
out.Println(ctx, "User Creator")
|
||||
out.Println(ctx, "Username: ", in.Argument(ctx, "username").String())
|
||||
out.Println(ctx, "Username: ", username.String())
|
||||
|
||||
return nil
|
||||
},
|
||||
@@ -170,14 +173,14 @@ import (
|
||||
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/example/pkg/command"
|
||||
"gitoa.ru/go-4devs/console/input/array"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
in := input.Array{}
|
||||
in.SetArgument("username","andrey")
|
||||
in := memory.Map{}
|
||||
in.Set("andrey","username")
|
||||
buf := bytes.Buffer{}
|
||||
out := output.Buffer(&buf)
|
||||
|
||||
|
||||
44
app.go
44
app.go
@@ -4,8 +4,11 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/provider/chain"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -17,7 +20,7 @@ func WithOutput(out output.Output) func(*App) {
|
||||
}
|
||||
|
||||
// WithInput sets input, by default creates inpur by os.Args.
|
||||
func WithInput(in input.Input) func(*App) {
|
||||
func WithInput(in config.BindProvider) func(*App) {
|
||||
return func(a *App) {
|
||||
a.in = in
|
||||
}
|
||||
@@ -25,7 +28,7 @@ func WithInput(in input.Input) func(*App) {
|
||||
|
||||
// WithSkipArgs sets how many arguments are passed. For example, you don't need to pass the name of a single command.
|
||||
func WithSkipArgs(l int) func(*App) {
|
||||
return WithInput(input.NewArgs(l))
|
||||
return WithInput(chain.New(arg.New(arg.WithArgs(os.Args[resolveSkip(l):])), &memory.Default{}))
|
||||
}
|
||||
|
||||
// WithExit sets exit callback by default os.Exit.
|
||||
@@ -40,7 +43,7 @@ func New(opts ...func(*App)) *App {
|
||||
app := &App{
|
||||
out: output.Stdout(),
|
||||
exit: os.Exit,
|
||||
in: input.NewArgs(0),
|
||||
in: chain.New(arg.New(arg.WithArgs(os.Args[resolveSkip(0):])), &memory.Default{}),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -54,7 +57,7 @@ func New(opts ...func(*App)) *App {
|
||||
type App struct {
|
||||
cmds []*Command
|
||||
out output.Output
|
||||
in input.Input
|
||||
in config.BindProvider
|
||||
exit func(int)
|
||||
}
|
||||
|
||||
@@ -75,7 +78,8 @@ func (a *App) Execute(ctx context.Context) {
|
||||
if err != nil {
|
||||
a.printError(ctx, err)
|
||||
|
||||
if err := a.list(ctx); err != nil {
|
||||
err := a.list(ctx)
|
||||
if err != nil {
|
||||
a.printError(ctx, err)
|
||||
}
|
||||
|
||||
@@ -86,7 +90,8 @@ func (a *App) Execute(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (a *App) exec(ctx context.Context, cmd *Command) {
|
||||
if err := Run(ctx, cmd, a.in, a.out); err != nil {
|
||||
err := Run(ctx, cmd, a.in, a.out)
|
||||
if err != nil {
|
||||
a.printError(ctx, err)
|
||||
a.exit(1)
|
||||
}
|
||||
@@ -110,9 +115,9 @@ func (a *App) list(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
arr := &input.Array{}
|
||||
arr.SetArgument("command_name", value.New(CommandList))
|
||||
in := input.Chain(arr, a.in)
|
||||
arr := &memory.Map{}
|
||||
arr.SetOption(value.New(CommandList), ArgumentCommandName)
|
||||
in := chain.New(arr, a.in)
|
||||
|
||||
return Run(ctx, cmd, in, a.out)
|
||||
}
|
||||
@@ -120,3 +125,20 @@ func (a *App) list(ctx context.Context) error {
|
||||
func (a *App) printError(ctx context.Context, err error) {
|
||||
ansi(ctx, a.in, a.out).Println(ctx, "<error>\n\n ", err, "\n</error>")
|
||||
}
|
||||
|
||||
func resolveSkip(in int) int {
|
||||
res := 2
|
||||
|
||||
switch {
|
||||
case in > 0 && len(os.Args) > in:
|
||||
res = in
|
||||
case in > 0:
|
||||
res = len(os.Args)
|
||||
case len(os.Args) == 1:
|
||||
res = 1
|
||||
case len(os.Args) > 1 && os.Args[1][0] == '-':
|
||||
res = 1
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
33
app_test.go
33
app_test.go
@@ -30,18 +30,21 @@ func ExampleNew_help() {
|
||||
// test:command [options] [--] [<test_argument>]
|
||||
//
|
||||
// Arguments:
|
||||
// test_argument test argument
|
||||
// test_argument test argument
|
||||
//
|
||||
// Options:
|
||||
// --duration[=DURATION] test duration with default [default: 1s]
|
||||
// --bool test bool option
|
||||
// --string[=STRING] array string (multiple values allowed)
|
||||
// -q, --quiet Do not output any message
|
||||
// -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed)
|
||||
// -h, --help Display this help message
|
||||
// -V, --version Display this application version
|
||||
// --ansi Do not ask any interactive question
|
||||
// --no-ansi Disable ANSI output
|
||||
// --string[=STRING] array string (multiple values allowed)
|
||||
// --group-bool bool
|
||||
// --group-test-string[=GROUP-TEST-STRING] test group string [default:group string default value]
|
||||
// --log-{service}-level[=LOG-{SERVICE}-LEVEL] service level [default:debug]
|
||||
// --bool test bool option
|
||||
// --duration[=DURATION] test duration with default
|
||||
// --ansi Do not ask any interactive question
|
||||
// -V, --version Display this application version
|
||||
// -h, --help Display this help message
|
||||
// -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed)
|
||||
// -q, --quiet Do not output any message
|
||||
// --no-ansi Disable ANSI output
|
||||
}
|
||||
|
||||
func ExampleNew_list() {
|
||||
@@ -74,12 +77,12 @@ func ExampleNew_list() {
|
||||
// command [options] [arguments]
|
||||
//
|
||||
// Options:
|
||||
// -q, --quiet Do not output any message
|
||||
// -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed)
|
||||
// -h, --help Display this help message
|
||||
// -V, --version Display this application version
|
||||
// --ansi Do not ask any interactive question
|
||||
// --no-ansi Disable ANSI output
|
||||
// --ansi Do not ask any interactive question
|
||||
// -V, --version Display this application version
|
||||
// -h, --help Display this help message
|
||||
// -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed)
|
||||
// -q, --quiet Do not output any message
|
||||
//
|
||||
// Available commands:
|
||||
// help Displays help for a command
|
||||
|
||||
38
command.go
38
command.go
@@ -4,15 +4,15 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
type (
|
||||
Action func(ctx context.Context, input input.Input, output output.Output) error
|
||||
Handle func(ctx context.Context, in input.Input, out output.Output, n Action) error
|
||||
Configure func(ctx context.Context, cfg *input.Definition) error
|
||||
Prepare func(ctx context.Context, cfg *input.Definition, n Configure) error
|
||||
Action func(ctx context.Context, input config.Provider, output output.Output) error
|
||||
Handle func(ctx context.Context, in config.Provider, out output.Output, n Action) error
|
||||
Configure func(ctx context.Context, cfg config.Definition) error
|
||||
Prepare func(ctx context.Context, cfg config.Definition, n Configure) error
|
||||
Option func(*Command)
|
||||
)
|
||||
|
||||
@@ -99,7 +99,11 @@ func (c *Command) With(opts ...Option) *Command {
|
||||
}
|
||||
|
||||
// Run run command with input and output.
|
||||
func (c *Command) Run(ctx context.Context, in input.Input, out output.Output) error {
|
||||
func (c *Command) Run(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
if c.Execute == nil {
|
||||
return fmt.Errorf("%w", ErrExecuteNil)
|
||||
}
|
||||
|
||||
if c.Handle != nil {
|
||||
return c.Handle(ctx, in, out, c.Execute)
|
||||
}
|
||||
@@ -108,12 +112,12 @@ func (c *Command) Run(ctx context.Context, in input.Input, out output.Output) er
|
||||
}
|
||||
|
||||
// Init configures command.
|
||||
func (c *Command) Init(ctx context.Context, cfg *input.Definition) error {
|
||||
func (c *Command) Init(ctx context.Context, cfg config.Definition) error {
|
||||
switch {
|
||||
case c.Prepare != nil && c.Configure != nil:
|
||||
return c.Prepare(ctx, cfg, c.Configure)
|
||||
case c.Prepare != nil:
|
||||
return c.Prepare(ctx, cfg, func(_ context.Context, _ *input.Definition) error {
|
||||
return c.Prepare(ctx, cfg, func(_ context.Context, _ config.Definition) error {
|
||||
return nil
|
||||
})
|
||||
case c.Configure != nil:
|
||||
@@ -133,16 +137,17 @@ func ChainPrepare(prepare ...Prepare) Prepare {
|
||||
if num > 1 {
|
||||
lastI := num - 1
|
||||
|
||||
return func(ctx context.Context, def *input.Definition, next Configure) error {
|
||||
return func(ctx context.Context, def config.Definition, next Configure) error {
|
||||
var (
|
||||
chainHandler func(context.Context, *input.Definition) error
|
||||
chainHandler func(context.Context, config.Definition) error
|
||||
curI int
|
||||
)
|
||||
|
||||
chainHandler = func(currentCtx context.Context, currentDef *input.Definition) error {
|
||||
chainHandler = func(currentCtx context.Context, currentDef config.Definition) error {
|
||||
if curI == lastI {
|
||||
return next(currentCtx, currentDef)
|
||||
}
|
||||
|
||||
curI++
|
||||
err := prepare[curI](currentCtx, currentDef, chainHandler)
|
||||
curI--
|
||||
@@ -154,7 +159,7 @@ func ChainPrepare(prepare ...Prepare) Prepare {
|
||||
}
|
||||
}
|
||||
|
||||
return func(ctx context.Context, cfg *input.Definition, next Configure) error {
|
||||
return func(ctx context.Context, cfg config.Definition, next Configure) error {
|
||||
return next(ctx, cfg)
|
||||
}
|
||||
}
|
||||
@@ -169,16 +174,17 @@ func ChainHandle(handlers ...Handle) Handle {
|
||||
if num > 1 {
|
||||
lastI := num - 1
|
||||
|
||||
return func(ctx context.Context, in input.Input, out output.Output, next Action) error {
|
||||
return func(ctx context.Context, in config.Provider, out output.Output, next Action) error {
|
||||
var (
|
||||
chainHandler func(context.Context, input.Input, output.Output) error
|
||||
chainHandler func(context.Context, config.Provider, output.Output) error
|
||||
curI int
|
||||
)
|
||||
|
||||
chainHandler = func(currentCtx context.Context, currentIn input.Input, currentOut output.Output) error {
|
||||
chainHandler = func(currentCtx context.Context, currentIn config.Provider, currentOut output.Output) error {
|
||||
if curI == lastI {
|
||||
return next(currentCtx, currentIn, currentOut)
|
||||
}
|
||||
|
||||
curI++
|
||||
err := handlers[curI](currentCtx, currentIn, currentOut, chainHandler)
|
||||
curI--
|
||||
@@ -190,7 +196,7 @@ func ChainHandle(handlers ...Handle) Handle {
|
||||
}
|
||||
}
|
||||
|
||||
return func(ctx context.Context, in input.Input, out output.Output, next Action) error {
|
||||
return func(ctx context.Context, in config.Provider, out output.Output, next Action) error {
|
||||
return next(ctx, in, out)
|
||||
}
|
||||
}
|
||||
|
||||
104
command_test.go
104
command_test.go
@@ -2,15 +2,22 @@ package console_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"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/provider/memory"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -24,25 +31,40 @@ func Command() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "test:command",
|
||||
Description: "test command",
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
var astr []string
|
||||
if aerr := console.ReadValue(ctx, in, "string").Unmarshal(&astr); aerr != nil && !errors.Is(aerr, config.ErrNotFound) {
|
||||
return fmt.Errorf("unmarshal string:%w", aerr)
|
||||
}
|
||||
|
||||
out.Print(ctx,
|
||||
"test argument:", in.Argument(ctx, "test_argument").String(), "\n",
|
||||
"bool option:", in.Option(ctx, "bool").Bool(), "\n",
|
||||
"duration option with default:", in.Option(ctx, "duration").Duration(), "\n",
|
||||
"array string:[", strings.Join(in.Option(ctx, "string").Strings(), ","), "]\n",
|
||||
"test argument:", console.ReadValue(ctx, in, "test_argument").String(), "\n",
|
||||
"bool option:", console.ReadValue(ctx, in, "bool").Bool(), "\n",
|
||||
"duration option with default:", console.ReadValue(ctx, in, "duration").Duration(), "\n",
|
||||
"array string:[", strings.Join(astr, ","), "]\n",
|
||||
"group string:", console.ReadValue(ctx, in, "group", "test", "string").String(), "\n",
|
||||
"log http service:", console.ReadValue(ctx, in, "log", "http", "level").String(), "\n",
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
Configure: func(ctx context.Context, def *input.Definition) error {
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.
|
||||
SetArguments(
|
||||
argument.String("test_argument", "test argument"),
|
||||
).
|
||||
SetOptions(
|
||||
option.String("string", "array string", option.Array),
|
||||
Add(
|
||||
group.New("group", "group example",
|
||||
option.Bool("bool", "bool"),
|
||||
group.New("test", "test", option.String("string", "test group string", option.Default("group string default value"))),
|
||||
),
|
||||
group.New("log", "log",
|
||||
proto.New("service", "service level",
|
||||
option.String("level", "service level", option.Default("debug")),
|
||||
),
|
||||
),
|
||||
arg.String("test_argument", "test argument"),
|
||||
option.String("string", "array string", option.Slice),
|
||||
option.Bool("bool", "test bool option"),
|
||||
option.Duration("duration", "test duration with default", option.Default(time.Second)),
|
||||
option.Duration("duration", "test duration with default", option.Default(value.New(time.Second))),
|
||||
option.Time("hidden", "hidden time", option.Default(value.New(time.Second)), option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
@@ -53,34 +75,35 @@ func Command() *console.Command {
|
||||
func TestChainPrepare(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var cnt int32
|
||||
var cnt int64
|
||||
|
||||
ctx := context.Background()
|
||||
def := input.NewDefinition()
|
||||
def := definition.New()
|
||||
|
||||
prepare := func(ctx context.Context, def *input.Definition, n console.Configure) error {
|
||||
atomic.AddInt32(&cnt, 1)
|
||||
prepare := func(ctx context.Context, def config.Definition, n console.Configure) error {
|
||||
atomic.AddInt64(&cnt, 1)
|
||||
|
||||
return n(ctx, def)
|
||||
}
|
||||
configure := func(context.Context, *input.Definition) error {
|
||||
configure := func(context.Context, config.Definition) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} {
|
||||
prepares := make([]console.Prepare, i)
|
||||
for p := 0; p < i; p++ {
|
||||
for p := range i {
|
||||
prepares[p] = prepare
|
||||
}
|
||||
|
||||
cnt = 0
|
||||
chain := console.ChainPrepare(prepares...)
|
||||
|
||||
if err := chain(ctx, def, configure); err != nil {
|
||||
err := chain(ctx, def, configure)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got: %s", err)
|
||||
}
|
||||
|
||||
if cnt != int32(i) {
|
||||
if cnt != int64(i) {
|
||||
t.Fatalf("expected: call prepare 1, got: %d ", cnt)
|
||||
}
|
||||
}
|
||||
@@ -89,38 +112,53 @@ func TestChainPrepare(t *testing.T) {
|
||||
func TestChainHandle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var cnt int32
|
||||
var cnt int64
|
||||
|
||||
ctx := context.Background()
|
||||
in := &input.Array{
|
||||
Map: input.Map{},
|
||||
}
|
||||
in := &memory.Map{}
|
||||
out := output.Stdout()
|
||||
|
||||
handle := func(ctx context.Context, in input.Input, out output.Output, next console.Action) error {
|
||||
atomic.AddInt32(&cnt, 1)
|
||||
handle := func(ctx context.Context, in config.Provider, out output.Output, next console.Action) error {
|
||||
atomic.AddInt64(&cnt, 1)
|
||||
|
||||
return next(ctx, in, out)
|
||||
}
|
||||
action := func(context.Context, input.Input, output.Output) error {
|
||||
action := func(context.Context, config.Provider, output.Output) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range []int{0, 1, 2, 30, 40, 50} {
|
||||
handles := make([]console.Handle, i)
|
||||
for p := 0; p < i; p++ {
|
||||
for p := range i {
|
||||
handles[p] = handle
|
||||
}
|
||||
|
||||
cnt = 0
|
||||
chain := console.ChainHandle(handles...)
|
||||
|
||||
if err := chain(ctx, in, out, action); err != nil {
|
||||
err := chain(ctx, in, out, action)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got: %s", err)
|
||||
}
|
||||
|
||||
if cnt != int32(i) {
|
||||
if cnt != int64(i) {
|
||||
t.Fatalf("expected: call prepare 1, got: %d ", cnt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunEmptyExecute(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
empty := console.Command{
|
||||
Name: "empty",
|
||||
}
|
||||
in := &memory.Map{}
|
||||
out := output.Stdout()
|
||||
|
||||
err := empty.Run(ctx, in, out)
|
||||
if !errors.Is(err, console.ErrExecuteNil) {
|
||||
t.Fatalf("expected: %v, got: %v ", console.ErrExecuteNil, err)
|
||||
}
|
||||
}
|
||||
|
||||
95
console.go
95
console.go
@@ -3,11 +3,16 @@ package console
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/provider/chain"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
@@ -18,6 +23,19 @@ const (
|
||||
verboseInfo = 1
|
||||
)
|
||||
|
||||
const (
|
||||
OptionHelp = "help"
|
||||
OptionVersion = "version"
|
||||
OptionAnsi = "ansi"
|
||||
OptionNoAnsi = "no-ansi"
|
||||
OptionQuiet = "quiet"
|
||||
OptionVerbose = "verbose"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultOptionsPosition = math.MaxUint64 / 2
|
||||
)
|
||||
|
||||
// Execute the current command with option.
|
||||
func Execute(ctx context.Context, cmd *Command, opts ...func(*App)) {
|
||||
opts = append([]func(*App){WithSkipArgs(1)}, opts...)
|
||||
@@ -25,15 +43,19 @@ func Execute(ctx context.Context, cmd *Command, opts ...func(*App)) {
|
||||
}
|
||||
|
||||
// Run current command by input and output.
|
||||
func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) error {
|
||||
def := input.NewDefinition()
|
||||
func Run(ctx context.Context, cmd *Command, in config.BindProvider, out output.Output) error {
|
||||
def := definition.New()
|
||||
|
||||
if err := cmd.Init(ctx, def); err != nil {
|
||||
err := cmd.Init(ctx, def)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := in.Bind(ctx, Default(def)); err != nil {
|
||||
ansi(ctx, in, out).Print(ctx, "<error>\n\n ", err, "\n</error>\n")
|
||||
def.Add(Default()...)
|
||||
|
||||
berr := in.Bind(ctx, config.NewVars(def.Options()...))
|
||||
if berr != nil {
|
||||
log.Print(berr)
|
||||
|
||||
return showHelp(ctx, cmd, in, output.Ansi(out))
|
||||
}
|
||||
@@ -42,7 +64,7 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e
|
||||
|
||||
out = verbose(ctx, in, out)
|
||||
|
||||
if in.Option(ctx, "version").Bool() {
|
||||
if ReadValue(ctx, in, OptionVersion).Bool() {
|
||||
version := cmd.Version
|
||||
if version == "" {
|
||||
version = "unknown"
|
||||
@@ -53,18 +75,18 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e
|
||||
return nil
|
||||
}
|
||||
|
||||
if in.Option(ctx, "help").Bool() {
|
||||
if ReadValue(ctx, in, OptionHelp).Bool() {
|
||||
return showHelp(ctx, cmd, in, out)
|
||||
}
|
||||
|
||||
return cmd.Run(ctx, in, out)
|
||||
}
|
||||
|
||||
func ansi(ctx context.Context, in input.Input, out output.Output) output.Output {
|
||||
func ansi(ctx context.Context, in config.Provider, out output.Output) output.Output {
|
||||
switch {
|
||||
case in.Option(ctx, "ansi").Bool():
|
||||
case ReadValue(ctx, in, OptionAnsi).Bool():
|
||||
out = output.Ansi(out)
|
||||
case in.Option(ctx, "no-ansi").Bool():
|
||||
case ReadValue(ctx, in, OptionNoAnsi).Bool():
|
||||
out = output.None(out)
|
||||
case lookupEnv("NO_COLOR"):
|
||||
out = output.None(out)
|
||||
@@ -81,12 +103,14 @@ func lookupEnv(name string) bool {
|
||||
return has && v == "true"
|
||||
}
|
||||
|
||||
func verbose(ctx context.Context, in input.Input, out output.Output) output.Output {
|
||||
func verbose(ctx context.Context, in config.Provider, out output.Output) output.Output {
|
||||
switch {
|
||||
case in.Option(ctx, "quiet").Bool():
|
||||
case ReadValue(ctx, in, OptionQuiet).Bool():
|
||||
out = output.Quiet()
|
||||
default:
|
||||
verb := in.Option(ctx, "verbose").Bools()
|
||||
var verb []bool
|
||||
|
||||
_ = ReadValue(ctx, in, OptionVerbose).Unmarshal(&verb)
|
||||
|
||||
switch {
|
||||
case len(verb) == verboseInfo:
|
||||
@@ -103,10 +127,10 @@ func verbose(ctx context.Context, in input.Input, out output.Output) output.Outp
|
||||
return out
|
||||
}
|
||||
|
||||
func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Output) error {
|
||||
arr := &input.Array{}
|
||||
arr.SetArgument(HelpArgumentCommandName, value.New(cmd.Name))
|
||||
arr.SetOption("help", value.New(false))
|
||||
func showHelp(ctx context.Context, cmd *Command, in config.Provider, out output.Output) error {
|
||||
arr := &memory.Map{}
|
||||
arr.SetOption(value.New(cmd.Name), ArgumentCommandName)
|
||||
arr.SetOption(value.New(false), OptionHelp)
|
||||
|
||||
if _, err := Find(cmd.Name); errors.Is(err, ErrNotFound) {
|
||||
register(cmd)
|
||||
@@ -117,21 +141,30 @@ func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Outp
|
||||
return err
|
||||
}
|
||||
|
||||
w := input.Chain(arr, in)
|
||||
w := chain.New(arr, in)
|
||||
|
||||
return Run(ctx, help, w, out)
|
||||
}
|
||||
|
||||
// Default options and argument command.
|
||||
func Default(d *input.Definition) *input.Definition {
|
||||
return d.SetOptions(
|
||||
option.Bool("no-ansi", "Disable ANSI output"),
|
||||
option.Bool("ansi", "Do not ask any interactive question"),
|
||||
option.Bool("version", "Display this application version", option.Short('V')),
|
||||
option.Bool("help", "Display this help message", option.Short('h')),
|
||||
option.Bool("verbose",
|
||||
func Default() []config.Option {
|
||||
return []config.Option{
|
||||
option.Bool(OptionNoAnsi, "Disable ANSI output", option.Position(defaultOptionsPosition)),
|
||||
option.Bool(OptionAnsi, "Do not ask any interactive question", option.Position(defaultOptionsPosition)),
|
||||
option.Bool(OptionVersion, "Display this application version", option.Short('V'), option.Position(defaultOptionsPosition)),
|
||||
option.Bool(OptionHelp, "Display this help message", option.Short('h'), option.Position(defaultOptionsPosition)),
|
||||
option.Bool(OptionVerbose,
|
||||
"Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace",
|
||||
option.Short('v'), option.Array),
|
||||
option.Bool("quiet", "Do not output any message", option.Short('q')),
|
||||
)
|
||||
option.Short('v'), option.Slice, option.Position(defaultOptionsPosition)),
|
||||
option.Bool(OptionQuiet, "Do not output any message", option.Short('q'), option.Position(defaultOptionsPosition)),
|
||||
}
|
||||
}
|
||||
|
||||
func ReadValue(ctx context.Context, in config.Provider, path ...string) config.Value {
|
||||
val, err := in.Value(ctx, path...)
|
||||
if err != nil {
|
||||
return value.EmptyValue()
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@ package console_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/chain"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -14,7 +17,7 @@ func ExampleRun() {
|
||||
cmd := Command()
|
||||
ctx := context.Background()
|
||||
out := output.Stdout()
|
||||
in := &input.Array{}
|
||||
in := chain.New(&memory.Map{}, &memory.Default{})
|
||||
|
||||
err := console.Run(ctx, cmd, in, out)
|
||||
fmt.Println("err:", err)
|
||||
@@ -23,23 +26,33 @@ func ExampleRun() {
|
||||
// bool option:false
|
||||
// duration option with default:1s
|
||||
// array string:[]
|
||||
// group string:group string default value
|
||||
// log http service:debug
|
||||
// err: <nil>
|
||||
}
|
||||
|
||||
func ExampleExecute() {
|
||||
cmd := Command()
|
||||
ctx := context.Background()
|
||||
in := &input.Array{}
|
||||
in := &memory.Map{}
|
||||
|
||||
jb, err := json.Marshal([]string{"same value", "other value"})
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
// Run command: ./bin "argument value" -b --string="same value" --string="other value"
|
||||
in.SetOption("bool", value.New(true))
|
||||
in.SetOption("string", value.New([]string{"same value", "other value"}))
|
||||
in.SetArgument("test_argument", value.New("argument value"))
|
||||
in.SetOption(value.New(true), "bool")
|
||||
in.SetOption(value.JBytes(jb), "string")
|
||||
in.SetOption(value.New("argument value"), "test_argument")
|
||||
in.SetOption(value.New("error"), "log", "http", "level")
|
||||
|
||||
console.Execute(ctx, cmd, console.WithInput(in), console.WithExit(func(int) {}))
|
||||
console.Execute(ctx, cmd, console.WithInput(chain.New(in, &memory.Default{})), console.WithExit(func(int) {}))
|
||||
// Output:
|
||||
// test argument:argument value
|
||||
// bool option:true
|
||||
// duration option with default:1s
|
||||
// array string:[same value,other value]
|
||||
// group string:group string default value
|
||||
// log http service:error
|
||||
}
|
||||
|
||||
31
error.go
Normal file
31
error.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("command not found")
|
||||
ErrCommandNil = errors.New("console: Register command is nil")
|
||||
ErrExecuteNil = errors.New("console: execute is nil")
|
||||
ErrCommandDuplicate = errors.New("console: duplicate command")
|
||||
)
|
||||
|
||||
type AlternativesError struct {
|
||||
Alt []string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e AlternativesError) Error() string {
|
||||
return fmt.Sprintf("%s, alternatives: [%s]", e.Err, strings.Join(e.Alt, ","))
|
||||
}
|
||||
|
||||
func (e AlternativesError) Is(err error) bool {
|
||||
return errors.Is(e.Err, err)
|
||||
}
|
||||
|
||||
func (e AlternativesError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
Binary file not shown.
@@ -21,5 +21,6 @@ func main() {
|
||||
<-ch
|
||||
cancel()
|
||||
}()
|
||||
|
||||
console.Execute(ctx, command.Long())
|
||||
}
|
||||
|
||||
35
example/cmd/config/main.go
Normal file
35
example/cmd/config/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/provider/chain"
|
||||
"gitoa.ru/go-4devs/config/provider/env"
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/example/pkg/command"
|
||||
)
|
||||
|
||||
const (
|
||||
Namespace = "fdevs"
|
||||
AppName = "console"
|
||||
)
|
||||
|
||||
// FDEVS_CONSOLE_CAT=env FDEVS_CONSOLE_HIDDEN=2022-09-18T23:07:49+03:00 go run cmd/config/main.go fdevs:console:arg -b tmp.
|
||||
// FDEVS_CONSOLE_CAT=env go run cmd/config/main.go fdevs:console:arg --hidden=2022-09-18T23:07:49+03:00 -b tmp.
|
||||
func main() {
|
||||
console.
|
||||
New(console.WithInput(
|
||||
chain.New(
|
||||
arg.New(arg.WithSkip(0)),
|
||||
env.New(Namespace, AppName),
|
||||
&memory.Default{},
|
||||
),
|
||||
)).
|
||||
Add(
|
||||
command.Long(),
|
||||
command.Args(),
|
||||
).
|
||||
Execute(context.Background())
|
||||
}
|
||||
10
example/go.mod
Normal file
10
example/go.mod
Normal file
@@ -0,0 +1,10 @@
|
||||
module gitoa.ru/go-4devs/console/example
|
||||
|
||||
go 1.23
|
||||
|
||||
toolchain go1.24.1
|
||||
|
||||
require (
|
||||
gitoa.ru/go-4devs/config v0.0.7
|
||||
gitoa.ru/go-4devs/console v0.2.0
|
||||
)
|
||||
4
example/go.sum
Normal file
4
example/go.sum
Normal file
@@ -0,0 +1,4 @@
|
||||
gitoa.ru/go-4devs/config v0.0.7 h1:8q6axRNLgXE5dYQd8Jbh9j+STqevbibVyvwrtsuHpZk=
|
||||
gitoa.ru/go-4devs/config v0.0.7/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
||||
gitoa.ru/go-4devs/console v0.2.0 h1:6lsbArs99GA8vGdnwNDThZNKjFNctNtTlSCUjhgwIpU=
|
||||
gitoa.ru/go-4devs/console v0.2.0/go.mod h1:xi4Svw7T+lylckAQiJQS/2qwDwF4YbIanlhcbQrBAiI=
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -14,21 +14,23 @@ func Args() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:console:arg",
|
||||
Description: "Understanding how Console Arguments and Options Are Handled",
|
||||
Configure: func(ctx context.Context, def *input.Definition) error {
|
||||
def.SetOptions(
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.Add(
|
||||
option.Bool("foo", "foo option", option.Short('f')),
|
||||
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
||||
option.String("cat", "cat option", option.Short('c')),
|
||||
option.Time("time", "time example"),
|
||||
option.Time("hidden", "hidden time example", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
out.Println(ctx, "foo: <info>", in.Option(ctx, "foo").Bool(), "</info>")
|
||||
out.Println(ctx, "bar: <info>", in.Option(ctx, "bar").String(), "</info>")
|
||||
out.Println(ctx, "cat: <info>", in.Option(ctx, "cat").String(), "</info>")
|
||||
out.Println(ctx, "time: <info>", in.Option(ctx, "time").Time().Format(time.RFC3339), "</info>")
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
out.Println(ctx, "foo: <info>", console.ReadValue(ctx, in, "foo").Bool(), "</info>")
|
||||
out.Println(ctx, "bar: <info>", console.ReadValue(ctx, in, "bar").String(), "</info>")
|
||||
out.Println(ctx, "cat: <info>", console.ReadValue(ctx, in, "cat").String(), "</info>")
|
||||
out.Println(ctx, "time: <info>", console.ReadValue(ctx, in, "time").Time().Format(time.RFC3339), "</info>")
|
||||
out.Println(ctx, "hidden: <info>", console.ReadValue(ctx, in, "hidden").Time().Format(time.RFC3339), "</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -3,10 +3,11 @@ package command
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
argument "gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -15,21 +16,24 @@ func CreateUser(required bool) *console.Command {
|
||||
Name: "app:create-user",
|
||||
Description: "Creates a new user.",
|
||||
Help: "This command allows you to create a user...",
|
||||
Configure: func(ctx context.Context, cfg *input.Definition) error {
|
||||
var opts []variable.Option
|
||||
Configure: func(_ context.Context, cfg config.Definition) error {
|
||||
var opts []param.Option
|
||||
if required {
|
||||
opts = append(opts, argument.Required)
|
||||
opts = append(opts, option.Required)
|
||||
}
|
||||
|
||||
cfg.
|
||||
SetArgument("username", "The username of the user.", argument.Required).
|
||||
SetArgument("password", "User password", opts...)
|
||||
Add(
|
||||
argument.String("username", "The username of the user.", option.Required),
|
||||
argument.String("password", "User password", opts...),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
// outputs a message followed by a "\n"
|
||||
out.Println(ctx, "User Creator")
|
||||
out.Println(ctx, "Username: ", in.Argument(ctx, "username").String())
|
||||
out.Println(ctx, "Username: ", console.ReadValue(ctx, in, "username").String())
|
||||
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/memory"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/example/pkg/command"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -17,8 +17,8 @@ func TestCreateUser(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
buf := bytes.Buffer{}
|
||||
out := output.Buffer(&buf)
|
||||
in := &input.Array{}
|
||||
in.SetArgument("username", "andrey")
|
||||
in := &memory.Map{}
|
||||
in.SetOption("andrey", "username")
|
||||
|
||||
err := console.Run(ctx, command.CreateUser(false), in, out)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,9 +3,10 @@ package command
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -13,19 +14,23 @@ func Hello() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:console:hello",
|
||||
Description: "example hello command",
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
name := in.Argument(ctx, "name").String()
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
name := console.ReadValue(ctx, in, "name").String()
|
||||
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
||||
|
||||
out.Info(ctx, "same trace info\n")
|
||||
out.Debug(ctx, "have some question?\n")
|
||||
out.Trace(ctx, "this message shows with -vvv\n")
|
||||
|
||||
pass := console.ReadValue(ctx, in, "pass").String()
|
||||
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
Configure: func(_ context.Context, def *input.Definition) error {
|
||||
def.SetArguments(
|
||||
argument.String("name", "Same name", argument.Default("World")),
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.Add(
|
||||
arg.String("name", "Same name", arg.Default("World")),
|
||||
option.String("pass", "password", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -3,8 +3,8 @@ package command
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -13,7 +13,7 @@ func Hidden() *console.Command {
|
||||
Name: "fdevs:console:hidden",
|
||||
Description: "hidden command exmale",
|
||||
Hidden: true,
|
||||
Execute: func(ctx context.Context, _ input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
out.Println(ctx, "<info> call hidden command</info>")
|
||||
|
||||
return nil
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/validator"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -18,11 +18,13 @@ const defaultTimeout = time.Second * 30
|
||||
func Long() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:command:long",
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
timeout := in.Option(ctx, "timeout").Duration()
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||
timer := time.NewTimer(timeout)
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
@@ -38,11 +40,11 @@ func Long() *console.Command {
|
||||
}
|
||||
}
|
||||
},
|
||||
Configure: func(ctx context.Context, def *input.Definition) error {
|
||||
def.SetOptions(option.Duration("timeout", "set duration run command",
|
||||
option.Default(defaultTimeout),
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.Add(option.Duration("timeout", "set duration run command",
|
||||
option.Default(value.New(defaultTimeout)),
|
||||
option.Short('t'),
|
||||
option.Valid(validator.NotBlank(flag.Duration)),
|
||||
validator.Valid(validator.NotBlank),
|
||||
))
|
||||
|
||||
return nil
|
||||
|
||||
@@ -3,8 +3,8 @@ package command
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ func Namespace() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "app:start",
|
||||
Description: "example command in other namespace",
|
||||
Execute: func(ctx context.Context, _ input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
out.Println(ctx, "example command in other namespace")
|
||||
|
||||
return nil
|
||||
|
||||
12
go.mod
12
go.mod
@@ -1,9 +1,13 @@
|
||||
module gitoa.ru/go-4devs/console
|
||||
|
||||
go 1.15
|
||||
go 1.24.0
|
||||
|
||||
require gitoa.ru/go-4devs/config v0.0.8
|
||||
|
||||
require (
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/stretchr/testify v1.8.0
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
golang.org/x/mod v0.31.0 // indirect
|
||||
golang.org/x/sync v0.19.0 // indirect
|
||||
golang.org/x/tools v0.40.0 // indirect
|
||||
)
|
||||
|
||||
tool golang.org/x/tools/cmd/stringer
|
||||
|
||||
29
go.sum
29
go.sum
@@ -1,21 +1,8 @@
|
||||
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/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/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=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
|
||||
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
||||
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
||||
|
||||
50
help.go
50
help.go
@@ -6,12 +6,13 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/validator"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/console/output/descriptor"
|
||||
)
|
||||
@@ -22,8 +23,8 @@ func init() {
|
||||
}
|
||||
|
||||
const (
|
||||
HelpArgumentCommandName = "command_name"
|
||||
helpOptFormat = "format"
|
||||
ArgumentCommandName = "command_name"
|
||||
OptionFormat = "format"
|
||||
)
|
||||
|
||||
func help() *Command {
|
||||
@@ -37,14 +38,15 @@ You can also output the help in other formats by using the <comment>--format</co
|
||||
<info>{{ .Bin }} {{ .Name }} --format=xml list</info>
|
||||
To display the list of available commands, please use the <info>list</info> command.
|
||||
`,
|
||||
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
var err error
|
||||
name := in.Argument(ctx, HelpArgumentCommandName).String()
|
||||
format := in.Option(ctx, helpOptFormat).String()
|
||||
|
||||
name := ReadValue(ctx, in, ArgumentCommandName).String()
|
||||
format := ReadValue(ctx, in, OptionFormat).String()
|
||||
|
||||
des, err := descriptor.Find(format)
|
||||
if err != nil {
|
||||
return fmt.Errorf("find descriptor: %w", err)
|
||||
return fmt.Errorf("find descriptor[%v]: %w", format, err)
|
||||
}
|
||||
|
||||
cmd, err := Find(name)
|
||||
@@ -52,9 +54,10 @@ To display the list of available commands, please use the <info>list</info> comm
|
||||
return fmt.Errorf("find cmd: %w", err)
|
||||
}
|
||||
|
||||
def := input.NewDefinition()
|
||||
def := definition.New()
|
||||
def.Add(Default()...)
|
||||
|
||||
if err := cmd.Init(ctx, Default(def)); err != nil {
|
||||
if err := cmd.Init(ctx, def); err != nil {
|
||||
return fmt.Errorf("init cmd: %w", err)
|
||||
}
|
||||
|
||||
@@ -68,27 +71,24 @@ To display the list of available commands, please use the <info>list</info> comm
|
||||
Name: cmd.Name,
|
||||
Description: cmd.Description,
|
||||
Help: cmd.Help,
|
||||
Definition: def,
|
||||
Options: def.With(param.New(descriptor.TxtStyle())),
|
||||
})
|
||||
|
||||
if derr != nil {
|
||||
return fmt.Errorf("descriptor help:%w", derr)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Configure: func(ctx context.Context, config *input.Definition) error {
|
||||
Configure: func(_ context.Context, config config.Definition) error {
|
||||
formats := descriptor.Descriptors()
|
||||
config.
|
||||
SetArguments(
|
||||
argument.String(HelpArgumentCommandName, "The command name", argument.Default(value.New("help"))),
|
||||
).
|
||||
SetOptions(
|
||||
option.String(helpOptFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")),
|
||||
Add(
|
||||
arg.String(ArgumentCommandName, "The command name", arg.Default(value.New("help"))),
|
||||
option.String(OptionFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")),
|
||||
option.Required,
|
||||
option.Default(formats[0]),
|
||||
option.Valid(
|
||||
validator.NotBlank(flag.String),
|
||||
option.Default(value.New(formats[0])),
|
||||
validator.Valid(
|
||||
validator.NotBlank,
|
||||
validator.Enum(formats...),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package argument
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
)
|
||||
|
||||
func Default(in interface{}) variable.Option {
|
||||
return variable.Default(value.New(in))
|
||||
}
|
||||
|
||||
func Required(v *variable.Variable) {
|
||||
variable.Required(v)
|
||||
}
|
||||
|
||||
func Array(v *variable.Variable) {
|
||||
variable.Array(v)
|
||||
}
|
||||
|
||||
func String(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.String(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Bool(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Bool(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Duration(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Duration(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Float64(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Float64(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Int(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Int(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Int64(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Int64(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Time(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Time(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Uint(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Uint(name, description, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Uint64(name, descriontion string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Uint64(name, descriontion, append(opts, variable.ArgArgument)...)
|
||||
}
|
||||
|
||||
func Err(name string, err error) variable.Error {
|
||||
return variable.Err(name, variable.TypeArgument, err)
|
||||
}
|
||||
160
input/argv.go
160
input/argv.go
@@ -1,160 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
)
|
||||
|
||||
const doubleDash = `--`
|
||||
|
||||
func NewArgs(skip int) *Argv {
|
||||
res := 2
|
||||
|
||||
switch {
|
||||
case skip > 0 && len(os.Args) > skip:
|
||||
res = skip
|
||||
case skip > 0:
|
||||
res = len(os.Args)
|
||||
case len(os.Args) == 1:
|
||||
res = 1
|
||||
case len(os.Args) > 1 && os.Args[1][0] == '-':
|
||||
res = 1
|
||||
}
|
||||
|
||||
return &Argv{Args: os.Args[res:]}
|
||||
}
|
||||
|
||||
type Argv struct {
|
||||
Array
|
||||
Args []string
|
||||
ErrHandle func(error) error
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func (i *Argv) Bind(ctx context.Context, def *Definition) error {
|
||||
options := true
|
||||
|
||||
for len(i.Args) > 0 {
|
||||
var err error
|
||||
|
||||
arg := i.Args[0]
|
||||
i.Args = i.Args[1:]
|
||||
|
||||
switch {
|
||||
case options && arg == doubleDash:
|
||||
options = false
|
||||
case options && len(arg) > 2 && arg[0:2] == doubleDash:
|
||||
err = i.parseLongOption(arg[2:], def)
|
||||
case options && arg[0:1] == "-":
|
||||
if len(arg) == 1 {
|
||||
return fmt.Errorf("%w: option name required given '-'", errs.ErrInvalidName)
|
||||
}
|
||||
|
||||
err = i.parseShortOption(arg[1:], def)
|
||||
default:
|
||||
err = i.parseArgument(arg, def)
|
||||
}
|
||||
|
||||
if err != nil && i.ErrHandle != nil {
|
||||
if herr := i.ErrHandle(err); herr != nil {
|
||||
return herr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return i.Array.Bind(ctx, def)
|
||||
}
|
||||
|
||||
func (i *Argv) parseLongOption(arg string, def *Definition) error {
|
||||
var value *string
|
||||
|
||||
name := arg
|
||||
|
||||
if strings.Contains(arg, "=") {
|
||||
vals := strings.SplitN(arg, "=", 2)
|
||||
name = vals[0]
|
||||
value = &vals[1]
|
||||
}
|
||||
|
||||
opt, err := def.Option(name)
|
||||
if err != nil {
|
||||
return option.Err(name, err)
|
||||
}
|
||||
|
||||
return i.appendOption(name, value, opt)
|
||||
}
|
||||
|
||||
func (i *Argv) appendOption(name string, data *string, opt variable.Variable) error {
|
||||
if i.HasOption(name) && !opt.IsArray() {
|
||||
return fmt.Errorf("%w: got: array, expect: %s", errs.ErrUnexpectedType, opt.Flag.Type())
|
||||
}
|
||||
|
||||
var val string
|
||||
|
||||
switch {
|
||||
case data != nil:
|
||||
val = *data
|
||||
case opt.IsBool():
|
||||
val = "true"
|
||||
case len(i.Args) > 0 && len(i.Args[0]) > 0 && i.Args[0][0:1] != "-":
|
||||
val = i.Args[0]
|
||||
i.Args = i.Args[1:]
|
||||
default:
|
||||
return option.Err(name, errs.ErrRequired)
|
||||
}
|
||||
|
||||
if err := i.AppendOption(opt, val); err != nil {
|
||||
return option.Err(name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Argv) parseShortOption(arg string, def *Definition) error {
|
||||
name := arg
|
||||
|
||||
var value string
|
||||
|
||||
if len(name) > 1 {
|
||||
name, value = arg[0:1], arg[1:]
|
||||
}
|
||||
|
||||
opt, err := def.ShortOption(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if opt.IsBool() && value != "" {
|
||||
if err := i.parseShortOption(value, def); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value = ""
|
||||
}
|
||||
|
||||
if value == "" {
|
||||
return i.appendOption(opt.Name, nil, opt)
|
||||
}
|
||||
|
||||
return i.appendOption(opt.Name, &value, opt)
|
||||
}
|
||||
|
||||
func (i *Argv) parseArgument(arg string, def *Definition) error {
|
||||
opt, err := def.Argument(i.LenArguments())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := i.AppendArgument(opt, arg); err != nil {
|
||||
return argument.Err(opt.Name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
110
input/array.go
110
input/array.go
@@ -1,110 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Array struct {
|
||||
Map
|
||||
defaults Map
|
||||
}
|
||||
|
||||
func (a *Array) Option(ctx context.Context, name string) value.Value {
|
||||
if v := a.Map.Option(ctx, name); !value.IsEmpty(v) {
|
||||
return v
|
||||
}
|
||||
|
||||
if v := a.defaults.Option(ctx, name); !value.IsEmpty(v) {
|
||||
return v
|
||||
}
|
||||
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
func (a *Array) Argument(ctx context.Context, name string) value.Value {
|
||||
if v := a.Map.Argument(ctx, name); !value.IsEmpty(v) {
|
||||
return v
|
||||
}
|
||||
|
||||
if v := a.defaults.Argument(ctx, name); !value.IsEmpty(v) {
|
||||
return v
|
||||
}
|
||||
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
func (a *Array) Bind(ctx context.Context, d *Definition) error {
|
||||
if err := a.bindArguments(ctx, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return a.bindOption(ctx, d)
|
||||
}
|
||||
|
||||
func (a *Array) bindOption(ctx context.Context, def *Definition) error {
|
||||
for _, name := range def.Options() {
|
||||
opt, err := def.Option(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !a.HasOption(name) {
|
||||
switch {
|
||||
case opt.HasDefault():
|
||||
a.defaults.SetOption(name, opt.Default)
|
||||
|
||||
continue
|
||||
case opt.IsRequired():
|
||||
return option.Err(name, errs.ErrRequired)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
v := a.Map.Option(ctx, name)
|
||||
if value.IsEmpty(v) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := opt.Validate(v); err != nil {
|
||||
return option.Err(name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Array) bindArguments(ctx context.Context, def *Definition) error {
|
||||
for pos, name := range def.Arguments() {
|
||||
arg, err := def.Argument(pos)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !a.HasArgument(name) {
|
||||
switch {
|
||||
case arg.HasDefault():
|
||||
a.defaults.SetArgument(name, arg.Default)
|
||||
|
||||
continue
|
||||
case arg.IsRequired():
|
||||
return argument.Err(name, errs.ErrRequired)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if v := a.Map.Argument(ctx, name); !value.IsEmpty(v) {
|
||||
if err := arg.Validate(v); err != nil {
|
||||
return argument.Err(name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
module gitoa.ru/go-4devs/console/input/cfg
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
gitoa.ru/go-4devs/config v0.0.0-20210427173104-3ba6b4c71578
|
||||
gitoa.ru/go-4devs/console v0.1.0
|
||||
)
|
||||
|
||||
replace gitoa.ru/go-4devs/console => ../../
|
||||
302
input/cfg/go.sum
302
input/cfg/go.sum
@@ -1,302 +0,0 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||
github.com/armon/go-metrics v0.3.0/go.mod h1:zXjbSimjXTd7vOpY8B0/2LpvNvDoXBuplAD+gJD3GYs=
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/aws/aws-sdk-go v1.25.37/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
|
||||
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
|
||||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/coreos/go-systemd/v22 v22.1.0 h1:kq/SbG2BCKLkDKkjQf5OWwKWUKj1lgs3lFI4PxnR5lg=
|
||||
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
|
||||
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-ldap/ldap/v3 v3.1.3/go.mod h1:3rbOH3jRS2u6jg2rJnKAMLE/xQyCKIveG2Sa/Cohzb8=
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
|
||||
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
|
||||
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
|
||||
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
|
||||
github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
|
||||
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-kms-wrapping/entropy v0.1.0/go.mod h1:d1g9WGtAunDNpek8jUIEJnBlbgKS1N2Q61QkHiZyR1g=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
|
||||
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
|
||||
github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY=
|
||||
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
|
||||
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
|
||||
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
|
||||
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
|
||||
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/hashicorp/vault/api v1.1.0 h1:QcxC7FuqEl0sZaIjcXB/kNEeBa0DH5z57qbWBvZwLC4=
|
||||
github.com/hashicorp/vault/api v1.1.0/go.mod h1:R3Umvhlxi2TN7Ex2hzOowyeNb+SfbVWI973N+ctaFMk=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267 h1:e1ok06zGrWJW91rzRroyl5nRNqraaBe4d5hiKcVZuHM=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
|
||||
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
|
||||
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
|
||||
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
|
||||
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
||||
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
|
||||
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
|
||||
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
||||
github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
|
||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
||||
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
|
||||
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
||||
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
|
||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
|
||||
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc=
|
||||
github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
|
||||
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
|
||||
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
|
||||
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
|
||||
gitoa.ru/go-4devs/config v0.0.0-20210427173104-3ba6b4c71578 h1:VecgkaFAv+eoGOfSbRrnH2pRHzZa9gF98x71jYuvqFo=
|
||||
gitoa.ru/go-4devs/config v0.0.0-20210427173104-3ba6b4c71578/go.mod h1:gpSbYuLQRK2PXgA1AQc1Pwf56pAPpPlXbQ8fLg8vwPk=
|
||||
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0 h1:+e5nrluATIy3GP53znpkHMFzPTHGYyzvJGFCbuI6ZLc=
|
||||
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0/go.mod h1:mPcW6aZJukV6Aa81LSKpBjQXTWlXB5r74ymPoSWa3Sw=
|
||||
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0 h1:dr1EOILak2pu4Nf5XbRIOCNIBjcz6UmkQd7hHRXwxaM=
|
||||
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0/go.mod h1:wKt7jgDgf/OfKiYmCq5WFGxOFAkVMLxiiXgLDFhECr8=
|
||||
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0 h1:3yLUEC0nFCxw/RArImOyRUI4OAFbg4PFpBbAhSNzKNY=
|
||||
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0/go.mod h1:tV31atvwzcybuqejDoY3oaNRTtlD2l/Ot78Pc9w7DMY=
|
||||
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
|
||||
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
|
||||
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
|
||||
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
|
||||
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
|
||||
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
|
||||
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
|
||||
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
|
||||
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig=
|
||||
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
|
||||
google.golang.org/grpc v1.32.0 h1:zWTV+LMdc3kaiJMSTOFz2UgSBgx8RNQoTGiZu3fR9S0=
|
||||
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/square/go-jose.v2 v2.5.1 h1:7odma5RETjNHWJnR32wx8t+Io4djHE1PqxCFx3iiZ2w=
|
||||
gopkg.in/square/go-jose.v2 v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/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=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
|
||||
@@ -1,50 +0,0 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Resolver func(ctx context.Context, name string) (config.Value, error)
|
||||
|
||||
func New(resolver func(ctx context.Context, name string) (config.Value, error)) *Input {
|
||||
return &Input{
|
||||
resolver: resolver,
|
||||
}
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
resolver Resolver
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
config.Value
|
||||
}
|
||||
|
||||
func (v Value) Any() interface{} {
|
||||
var out interface{}
|
||||
_ = v.Value.Unmarshal(&out)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (i *Input) Option(ctx context.Context, name string) value.Value {
|
||||
val, err := i.resolver(ctx, name)
|
||||
if errors.Is(err, config.ErrVariableNotFound) {
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
return value.Read{ParseValue: Value{Value: val}}
|
||||
}
|
||||
|
||||
func (i *Input) Argument(ctx context.Context, name string) value.Value {
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
func (i *Input) Bind(ctx context.Context, def *input.Definition) error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Chain(c ...Input) Input {
|
||||
return chain(c)
|
||||
}
|
||||
|
||||
type chain []Input
|
||||
|
||||
func (c chain) Option(ctx context.Context, name string) value.Value {
|
||||
for _, in := range c {
|
||||
if val := in.Option(ctx, name); !value.IsEmpty(val) {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
func (c chain) Argument(ctx context.Context, name string) value.Value {
|
||||
for _, in := range c {
|
||||
if val := in.Argument(ctx, name); !value.IsEmpty(val) {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
return value.Empty()
|
||||
}
|
||||
|
||||
func (c chain) Bind(ctx context.Context, def *Definition) error {
|
||||
for _, input := range c {
|
||||
if err := input.Bind(ctx, def); err != nil {
|
||||
return fmt.Errorf("%T:%w", input, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
)
|
||||
|
||||
func NewDefinition() *Definition {
|
||||
return &Definition{
|
||||
options: make(map[string]variable.Variable),
|
||||
args: make(map[string]variable.Variable),
|
||||
short: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
type Definition struct {
|
||||
options map[string]variable.Variable
|
||||
posOpt []string
|
||||
args map[string]variable.Variable
|
||||
posArgs []string
|
||||
short map[string]string
|
||||
}
|
||||
|
||||
func (d *Definition) Options() []string {
|
||||
return d.posOpt
|
||||
}
|
||||
|
||||
func (d *Definition) Arguments() []string {
|
||||
return d.posArgs
|
||||
}
|
||||
|
||||
func (d *Definition) SetOption(name, description string, opts ...variable.Option) *Definition {
|
||||
return d.SetOptions(option.String(name, description, opts...))
|
||||
}
|
||||
|
||||
func (d *Definition) SetOptions(opts ...variable.Variable) *Definition {
|
||||
for _, opt := range opts {
|
||||
if _, has := d.options[opt.Name]; !has {
|
||||
d.posOpt = append([]string{opt.Name}, d.posOpt...)
|
||||
}
|
||||
|
||||
d.options[opt.Name] = opt
|
||||
if opt.HasShort() {
|
||||
d.short[opt.Alias] = opt.Name
|
||||
}
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Definition) SetArgument(name, description string, opts ...variable.Option) *Definition {
|
||||
return d.SetArguments(argument.String(name, description, opts...))
|
||||
}
|
||||
|
||||
func (d *Definition) SetArguments(args ...variable.Variable) *Definition {
|
||||
for _, arg := range args {
|
||||
if _, ok := d.args[arg.Name]; !ok {
|
||||
d.posArgs = append(d.posArgs, arg.Name)
|
||||
}
|
||||
|
||||
d.args[arg.Name] = arg
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Definition) Argument(pos int) (variable.Variable, error) {
|
||||
if len(d.posArgs) == 0 {
|
||||
return variable.Variable{}, errs.ErrNoArgs
|
||||
}
|
||||
|
||||
lastPos := len(d.posArgs) - 1
|
||||
if lastPos < pos {
|
||||
arg := d.args[d.posArgs[lastPos]]
|
||||
if arg.IsArray() {
|
||||
return arg, nil
|
||||
}
|
||||
|
||||
return variable.Variable{}, errs.ErrToManyArgs
|
||||
}
|
||||
|
||||
return d.args[d.posArgs[pos]], nil
|
||||
}
|
||||
|
||||
func (d *Definition) ShortOption(short string) (variable.Variable, error) {
|
||||
name, ok := d.short[short]
|
||||
if !ok {
|
||||
return variable.Variable{}, errs.ErrNotFound
|
||||
}
|
||||
|
||||
return d.Option(name)
|
||||
}
|
||||
|
||||
func (d *Definition) Option(name string) (variable.Variable, error) {
|
||||
if opt, ok := d.options[name]; ok {
|
||||
return opt, nil
|
||||
}
|
||||
|
||||
return variable.Variable{}, errs.ErrNotFound
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrNoArgs = errors.New("no arguments expected")
|
||||
ErrToManyArgs = errors.New("too many arguments")
|
||||
ErrUnexpectedType = errors.New("unexpected type")
|
||||
ErrRequired = errors.New("is required")
|
||||
ErrAppend = errors.New("failed append")
|
||||
ErrInvalidName = errors.New("invalid name")
|
||||
ErrWrongType = errors.New("wrong type")
|
||||
)
|
||||
@@ -1,98 +0,0 @@
|
||||
package flag
|
||||
|
||||
//go:generate stringer -type=Flag -linecomment
|
||||
|
||||
type Flag int
|
||||
|
||||
const (
|
||||
String Flag = 0 // string
|
||||
Required Flag = 1 << iota // required
|
||||
Array // array
|
||||
Int // int
|
||||
Int64 // int64
|
||||
Uint // uint
|
||||
Uint64 // uint64
|
||||
Float64 // float64
|
||||
Bool // bool
|
||||
Duration // duration
|
||||
Time // time
|
||||
Any // any
|
||||
)
|
||||
|
||||
func (i Flag) With(v Flag) Flag {
|
||||
return i | v
|
||||
}
|
||||
|
||||
func (i Flag) IsString() bool {
|
||||
return i|Required|Array^Required^Array == 0
|
||||
}
|
||||
|
||||
func (i Flag) IsRequired() bool {
|
||||
return i&Required > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsArray() bool {
|
||||
return i&Array > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsInt() bool {
|
||||
return i&Int > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsInt64() bool {
|
||||
return i&Int64 > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsUint() bool {
|
||||
return i&Uint > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsUint64() bool {
|
||||
return i&Uint64 > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsFloat64() bool {
|
||||
return i&Float64 > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsBool() bool {
|
||||
return i&Bool > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsDuration() bool {
|
||||
return i&Duration > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsTime() bool {
|
||||
return i&Time > 0
|
||||
}
|
||||
|
||||
func (i Flag) IsAny() bool {
|
||||
return i&Any > 0
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func (i Flag) Type() Flag {
|
||||
switch {
|
||||
case i.IsInt():
|
||||
return Int
|
||||
case i.IsInt64():
|
||||
return Int64
|
||||
case i.IsUint():
|
||||
return Uint
|
||||
case i.IsUint64():
|
||||
return Uint64
|
||||
case i.IsFloat64():
|
||||
return Float64
|
||||
case i.IsBool():
|
||||
return Bool
|
||||
case i.IsDuration():
|
||||
return Duration
|
||||
case i.IsTime():
|
||||
return Time
|
||||
case i.IsAny():
|
||||
return Any
|
||||
default:
|
||||
return String
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
// Code generated by "stringer -type=Flag -linecomment"; DO NOT EDIT.
|
||||
|
||||
package flag
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[String-0]
|
||||
_ = x[Required-2]
|
||||
_ = x[Array-4]
|
||||
_ = x[Int-8]
|
||||
_ = x[Int64-16]
|
||||
_ = x[Uint-32]
|
||||
_ = x[Uint64-64]
|
||||
_ = x[Float64-128]
|
||||
_ = x[Bool-256]
|
||||
_ = x[Duration-512]
|
||||
_ = x[Time-1024]
|
||||
_ = x[Any-2048]
|
||||
}
|
||||
|
||||
const _Flag_name = "stringrequiredarrayintint64uintuint64float64booldurationtimeany"
|
||||
|
||||
var _Flag_map = map[Flag]string{
|
||||
0: _Flag_name[0:6],
|
||||
2: _Flag_name[6:14],
|
||||
4: _Flag_name[14:19],
|
||||
8: _Flag_name[19:22],
|
||||
16: _Flag_name[22:27],
|
||||
32: _Flag_name[27:31],
|
||||
64: _Flag_name[31:37],
|
||||
128: _Flag_name[37:44],
|
||||
256: _Flag_name[44:48],
|
||||
512: _Flag_name[48:56],
|
||||
1024: _Flag_name[56:60],
|
||||
2048: _Flag_name[60:63],
|
||||
}
|
||||
|
||||
func (i Flag) String() string {
|
||||
if str, ok := _Flag_map[i]; ok {
|
||||
return str
|
||||
}
|
||||
return "Flag(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Input interface {
|
||||
Option(ctx context.Context, name string) value.Value
|
||||
Argument(ctx context.Context, name string) value.Value
|
||||
Bind(ctx context.Context, def *Definition) error
|
||||
}
|
||||
118
input/map.go
118
input/map.go
@@ -1,118 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
)
|
||||
|
||||
type Map struct {
|
||||
opts map[string]value.Value
|
||||
args map[string]value.Value
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (m *Map) Option(_ context.Context, name string) value.Value {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
return m.opts[name]
|
||||
}
|
||||
|
||||
func (m *Map) Argument(_ context.Context, name string) value.Value {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
return m.args[name]
|
||||
}
|
||||
|
||||
func (m *Map) Bind(_ context.Context, _ *Definition) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Map) LenArguments() int {
|
||||
return len(m.args)
|
||||
}
|
||||
|
||||
func (m *Map) HasOption(name string) bool {
|
||||
_, ok := m.opts[name]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *Map) SetOption(name string, val interface{}) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
if m.opts == nil {
|
||||
m.opts = make(map[string]value.Value)
|
||||
}
|
||||
|
||||
m.opts[name] = value.New(val)
|
||||
}
|
||||
|
||||
func (m *Map) HasArgument(name string) bool {
|
||||
_, ok := m.args[name]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *Map) SetArgument(name string, val interface{}) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
if m.args == nil {
|
||||
m.args = make(map[string]value.Value)
|
||||
}
|
||||
|
||||
m.args[name] = value.New(val)
|
||||
}
|
||||
|
||||
func (m *Map) AppendOption(opt variable.Variable, val string) error {
|
||||
old, ok := m.opts[opt.Name]
|
||||
if !ok {
|
||||
value, err := opt.Create(val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("append option:%w", err)
|
||||
}
|
||||
|
||||
m.SetOption(opt.Name, value)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := opt.Append(old, val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("append option:%w", err)
|
||||
}
|
||||
|
||||
m.SetOption(opt.Name, value)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Map) AppendArgument(arg variable.Variable, val string) error {
|
||||
old, ok := m.args[arg.Name]
|
||||
if !ok {
|
||||
value, err := arg.Create(val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("append option:%w", err)
|
||||
}
|
||||
|
||||
m.SetArgument(arg.Name, value)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := arg.Append(old, val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("append option:%w", err)
|
||||
}
|
||||
|
||||
m.SetArgument(arg.Name, value)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/console/input/variable"
|
||||
)
|
||||
|
||||
func Short(in rune) variable.Option {
|
||||
return func(v *variable.Variable) {
|
||||
v.Alias = string(in)
|
||||
}
|
||||
}
|
||||
|
||||
func Default(in interface{}) variable.Option {
|
||||
return variable.Default(value.New(in))
|
||||
}
|
||||
|
||||
func Required(v *variable.Variable) {
|
||||
variable.Required(v)
|
||||
}
|
||||
|
||||
func Valid(f ...func(value.Value) error) variable.Option {
|
||||
return variable.Valid(f...)
|
||||
}
|
||||
|
||||
func Array(v *variable.Variable) {
|
||||
variable.Array(v)
|
||||
}
|
||||
|
||||
func String(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.String(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Bool(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Bool(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Duration(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Duration(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Float64(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Float64(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Int(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Int(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Int64(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Int64(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Time(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Time(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Uint(name, description string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Uint(name, description, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Uint64(name, descriontion string, opts ...variable.Option) variable.Variable {
|
||||
return variable.Uint64(name, descriontion, append(opts, variable.ArgOption)...)
|
||||
}
|
||||
|
||||
func Err(name string, err error) variable.Error {
|
||||
return variable.Err(name, variable.TypeOption, err)
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package validator
|
||||
|
||||
import "gitoa.ru/go-4devs/console/input/value"
|
||||
|
||||
func Enum(enum ...string) func(value.Value) error {
|
||||
return func(in value.Value) error {
|
||||
val := in.String()
|
||||
for _, e := range enum {
|
||||
if e == val {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return NewError(ErrInvalid, val, enum)
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package validator_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func TestEnum(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
validValue := value.New("valid")
|
||||
invalidValue := value.New("invalid")
|
||||
|
||||
enum := validator.Enum("valid", "other", "three")
|
||||
|
||||
if err := enum(validValue); err != nil {
|
||||
t.Errorf("expected valid value got err:%s", err)
|
||||
}
|
||||
|
||||
if err := enum(invalidValue); !errors.Is(err, validator.ErrInvalid) {
|
||||
t.Errorf("expected err:%s, got: %s", validator.ErrInvalid, err)
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalid = errors.New("invalid value")
|
||||
ErrNotBlank = errors.New("not blank")
|
||||
)
|
||||
|
||||
func NewError(err error, value, expect interface{}) Error {
|
||||
return Error{
|
||||
err: err,
|
||||
value: value,
|
||||
expect: expect,
|
||||
}
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
err error
|
||||
value interface{}
|
||||
expect interface{}
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return fmt.Sprintf("%s: expext: %s, given: %s", e.err, e.expect, e.value)
|
||||
}
|
||||
|
||||
func (e Error) Is(err error) bool {
|
||||
return errors.Is(e.err, err)
|
||||
}
|
||||
|
||||
func (e Error) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
//nolint:gocyclo,cyclop
|
||||
func NotBlank(fl flag.Flag) func(value.Value) error {
|
||||
return func(in value.Value) error {
|
||||
switch {
|
||||
case fl.IsAny() && in.Any() != nil:
|
||||
return nil
|
||||
case fl.IsArray():
|
||||
return arrayNotBlank(fl, in)
|
||||
case fl.IsInt() && in.Int() != 0:
|
||||
return nil
|
||||
case fl.IsInt64() && in.Int64() != 0:
|
||||
return nil
|
||||
case fl.IsUint() && in.Uint() != 0:
|
||||
return nil
|
||||
case fl.IsUint64() && in.Uint64() != 0:
|
||||
return nil
|
||||
case fl.IsFloat64() && in.Float64() != 0:
|
||||
return nil
|
||||
case fl.IsDuration() && in.Duration() != 0:
|
||||
return nil
|
||||
case fl.IsTime() && !in.Time().IsZero():
|
||||
return nil
|
||||
case fl.IsString() && len(in.String()) > 0:
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gocyclo,gocognit,cyclop
|
||||
func arrayNotBlank(fl flag.Flag, in value.Value) error {
|
||||
switch {
|
||||
case fl.IsInt() && len(in.Ints()) > 0:
|
||||
for _, i := range in.Ints() {
|
||||
if i == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsInt64() && len(in.Int64s()) > 0:
|
||||
for _, i := range in.Int64s() {
|
||||
if i == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsUint() && len(in.Uints()) > 0:
|
||||
for _, u := range in.Uints() {
|
||||
if u == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsUint64() && len(in.Uint64s()) > 0:
|
||||
for _, u := range in.Uint64s() {
|
||||
if u == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsFloat64() && len(in.Float64s()) > 0:
|
||||
for _, f := range in.Float64s() {
|
||||
if f == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsBool() && len(in.Bools()) > 0:
|
||||
return nil
|
||||
case fl.IsDuration() && len(in.Durations()) > 0:
|
||||
for _, d := range in.Durations() {
|
||||
if d == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsTime() && len(in.Times()) > 0:
|
||||
for _, t := range in.Times() {
|
||||
if t.IsZero() {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
case fl.IsString() && len(in.Strings()) > 0:
|
||||
for _, st := range in.Strings() {
|
||||
if len(st) == 0 {
|
||||
return ErrNotBlank
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrNotBlank
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package validator_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func TestNotBlank(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := map[string]struct {
|
||||
flag flag.Flag
|
||||
value value.Value
|
||||
empty value.Value
|
||||
}{
|
||||
"any": {flag: flag.Any, value: value.New(float32(1))},
|
||||
"array int": {
|
||||
flag: flag.Int | flag.Array,
|
||||
value: value.New([]int{1}),
|
||||
empty: value.New([]int{10, 20, 0}),
|
||||
},
|
||||
"array int64": {
|
||||
flag: flag.Int64 | flag.Array,
|
||||
value: value.New([]int64{1}),
|
||||
empty: value.New([]int64{0}),
|
||||
},
|
||||
"array uint": {
|
||||
flag: flag.Uint | flag.Array,
|
||||
value: value.New([]uint{1}),
|
||||
empty: value.New([]uint{1, 0}),
|
||||
},
|
||||
"array uint64": {
|
||||
flag: flag.Uint64 | flag.Array,
|
||||
value: value.New([]uint64{1}),
|
||||
empty: value.New([]uint64{0}),
|
||||
},
|
||||
"array float64": {
|
||||
flag: flag.Float64 | flag.Array,
|
||||
value: value.New([]float64{0.2}),
|
||||
empty: value.New([]float64{0}),
|
||||
},
|
||||
"array bool": {
|
||||
flag: flag.Bool | flag.Array,
|
||||
value: value.New([]bool{true, false}),
|
||||
empty: value.New([]bool{}),
|
||||
},
|
||||
"array duration": {
|
||||
flag: flag.Duration | flag.Array,
|
||||
value: value.New([]time.Duration{time.Second}),
|
||||
empty: value.New([]time.Duration{time.Second, 0}),
|
||||
},
|
||||
"array time": {
|
||||
flag: flag.Time | flag.Array,
|
||||
value: value.New([]time.Time{time.Now()}),
|
||||
empty: value.New([]time.Time{{}, time.Now()}),
|
||||
},
|
||||
"array string": {
|
||||
flag: flag.Array,
|
||||
value: value.New([]string{"value"}),
|
||||
empty: value.New([]string{""}),
|
||||
},
|
||||
"int": {
|
||||
flag: flag.Int,
|
||||
value: value.New(int(1)),
|
||||
},
|
||||
"int64": {
|
||||
flag: flag.Int64,
|
||||
value: value.New(int64(2)),
|
||||
},
|
||||
"uint": {
|
||||
flag: flag.Uint,
|
||||
value: value.New(uint(1)),
|
||||
empty: value.New([]uint{1}),
|
||||
},
|
||||
"uint64": {
|
||||
flag: flag.Uint64,
|
||||
value: value.New(uint64(10)),
|
||||
},
|
||||
"float64": {
|
||||
flag: flag.Float64,
|
||||
value: value.New(float64(.00001)),
|
||||
},
|
||||
"duration": {
|
||||
flag: flag.Duration,
|
||||
value: value.New(time.Minute),
|
||||
empty: value.New("same string"),
|
||||
},
|
||||
"time": {flag: flag.Time, value: value.New(time.Now())},
|
||||
"string": {value: value.New("string"), empty: value.New("")},
|
||||
}
|
||||
|
||||
for name, ca := range cases {
|
||||
valid := validator.NotBlank(ca.flag)
|
||||
if err := valid(ca.value); err != nil {
|
||||
t.Errorf("case: %s, expected error <nil>, got: %s", name, err)
|
||||
}
|
||||
|
||||
if ca.empty == nil {
|
||||
ca.empty = value.Empty()
|
||||
}
|
||||
|
||||
if err := valid(ca.empty); err == nil || !errors.Is(err, validator.ErrNotBlank) {
|
||||
t.Errorf("case empty: %s, expect: %s, got:%s", name, validator.ErrNotBlank, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package validator
|
||||
|
||||
import "gitoa.ru/go-4devs/console/input/value"
|
||||
|
||||
func Valid(v ...func(value.Value) error) func(value.Value) error {
|
||||
return func(in value.Value) error {
|
||||
for _, valid := range v {
|
||||
if err := valid(in); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package validator_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func TestValid(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
validValue := value.New("one")
|
||||
invalidValue := value.New([]string{"one"})
|
||||
|
||||
valid := validator.Valid(
|
||||
validator.NotBlank(flag.String),
|
||||
validator.Enum("one", "two"),
|
||||
)
|
||||
|
||||
if err := valid(validValue); err != nil {
|
||||
t.Errorf("expected valid value, got: %s", err)
|
||||
}
|
||||
|
||||
if err := valid(invalidValue); !errors.Is(err, validator.ErrNotBlank) {
|
||||
t.Errorf("expected not blank, got:%s", err)
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ Value = NewAny(nil)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
emptyValue = NewAny(nil)
|
||||
)
|
||||
|
||||
func Empty() Value {
|
||||
return emptyValue
|
||||
}
|
||||
|
||||
func IsEmpty(v Value) bool {
|
||||
return v == nil || v == emptyValue
|
||||
}
|
||||
|
||||
func NewAny(in interface{}) Value {
|
||||
return Read{Any{v: in}}
|
||||
}
|
||||
|
||||
type Any struct {
|
||||
v interface{}
|
||||
}
|
||||
|
||||
func (a Any) Any() interface{} {
|
||||
return a.v
|
||||
}
|
||||
|
||||
func (a Any) Unmarshal(val interface{}) error {
|
||||
out, err := a.ParseString()
|
||||
if err != nil {
|
||||
return fmt.Errorf("any parse string:%w", err)
|
||||
}
|
||||
|
||||
uerr := json.Unmarshal([]byte(out), val)
|
||||
if uerr != nil {
|
||||
return fmt.Errorf("any unmarshal: %w", uerr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Any) ParseString() (string, error) {
|
||||
if a.v == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
bout, err := json.Marshal(a.v)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("any string:%w", err)
|
||||
}
|
||||
|
||||
return string(bout), err
|
||||
}
|
||||
|
||||
func (a Any) ParseInt() (int, error) {
|
||||
out, ok := a.v.(int)
|
||||
if !ok {
|
||||
return 0, a.wrongType("int")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseInt64() (int64, error) {
|
||||
out, ok := a.v.(int64)
|
||||
if !ok {
|
||||
return 0, a.wrongType("int64")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseUint() (uint, error) {
|
||||
out, ok := a.v.(uint)
|
||||
if !ok {
|
||||
return 0, a.wrongType("uint")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseUint64() (uint64, error) {
|
||||
out, ok := a.v.(uint64)
|
||||
if !ok {
|
||||
return 0, a.wrongType("uint64")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseFloat64() (float64, error) {
|
||||
out, ok := a.v.(float64)
|
||||
if !ok {
|
||||
return 0, a.wrongType("float64")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseBool() (bool, error) {
|
||||
out, ok := a.v.(bool)
|
||||
if !ok {
|
||||
return false, a.wrongType("bool")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseDuration() (time.Duration, error) {
|
||||
out, ok := a.v.(time.Duration)
|
||||
if !ok {
|
||||
return 0, a.wrongType("time.Duration")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) ParseTime() (time.Time, error) {
|
||||
out, ok := a.v.(time.Time)
|
||||
if !ok {
|
||||
return time.Time{}, a.wrongType("time.Time")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a Any) wrongType(ex String) error {
|
||||
return fmt.Errorf("%w any: got: %T expect: %s", ErrWrongType, a.v, ex)
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Bool(false)
|
||||
_ SliceValue = Bools{}
|
||||
)
|
||||
|
||||
func NewBools(in []bool) Slice {
|
||||
return Slice{SliceValue: Bools(in)}
|
||||
}
|
||||
|
||||
type Bools []bool
|
||||
|
||||
func (b Bools) Any() interface{} {
|
||||
return b.Bools()
|
||||
}
|
||||
|
||||
func (b Bools) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*[]bool)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect: *[]bool got: %T", ErrWrongType, val)
|
||||
}
|
||||
|
||||
*v = b
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Bools() []bool {
|
||||
out := make([]bool, len(b))
|
||||
copy(out, b)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (b Bools) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bools) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewBool(in bool) Read {
|
||||
return Read{ParseValue: Bool(in)}
|
||||
}
|
||||
|
||||
type Bool bool
|
||||
|
||||
func (b Bool) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*bool)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect: *bool got: %T", ErrWrongType, val)
|
||||
}
|
||||
|
||||
*v = bool(b)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseString() (string, error) {
|
||||
return fmt.Sprintf("%v", b), nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseInt() (int, error) {
|
||||
if b {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseInt64() (int64, error) {
|
||||
if b {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseUint() (uint, error) {
|
||||
if b {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseUint64() (uint64, error) {
|
||||
if b {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseFloat64() (float64, error) {
|
||||
if b {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseBool() (bool, error) {
|
||||
return bool(b), nil
|
||||
}
|
||||
|
||||
func (b Bool) ParseDuration() (time.Duration, error) {
|
||||
return 0, fmt.Errorf("bool to duration:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (b Bool) ParseTime() (time.Time, error) {
|
||||
return time.Time{}, fmt.Errorf("bool to time:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (b Bool) Any() interface{} {
|
||||
return bool(b)
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Duration(0)
|
||||
_ SliceValue = Durations{}
|
||||
)
|
||||
|
||||
func NewDurations(in []time.Duration) Slice {
|
||||
return Slice{SliceValue: Durations(in)}
|
||||
}
|
||||
|
||||
type Durations []time.Duration
|
||||
|
||||
func (d Durations) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*[]time.Duration)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect: *[]time.Duration got: %T", ErrWrongType, val)
|
||||
}
|
||||
|
||||
*v = d
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Any() interface{} {
|
||||
return d.Durations()
|
||||
}
|
||||
|
||||
func (d Durations) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Durations) Durations() []time.Duration {
|
||||
out := make([]time.Duration, len(d))
|
||||
copy(out, d)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (d Durations) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDuration(in time.Duration) Read {
|
||||
return Read{ParseValue: Duration(in)}
|
||||
}
|
||||
|
||||
type Duration time.Duration
|
||||
|
||||
func (d Duration) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseString() (string, error) {
|
||||
return time.Duration(d).String(), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseInt() (int, error) {
|
||||
return int(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseInt64() (int64, error) {
|
||||
return int64(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseUint() (uint, error) {
|
||||
return uint(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseUint64() (uint64, error) {
|
||||
return uint64(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseFloat64() (float64, error) {
|
||||
return float64(d), nil
|
||||
}
|
||||
|
||||
func (d Duration) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("duration:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (d Duration) ParseTime() (time.Time, error) {
|
||||
return time.Time{}, fmt.Errorf("duration:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (d Duration) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*time.Duration)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect: *[]time.Duration got: %T", ErrWrongType, val)
|
||||
}
|
||||
|
||||
*v = time.Duration(d)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Duration) Any() interface{} {
|
||||
return time.Duration(d)
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Float64(0)
|
||||
_ SliceValue = Float64s{}
|
||||
)
|
||||
|
||||
func NewFloat64s(in []float64) Slice {
|
||||
return Slice{SliceValue: Float64s(in)}
|
||||
}
|
||||
|
||||
type Float64s []float64
|
||||
|
||||
func (f Float64s) Any() interface{} {
|
||||
return f.Float64s()
|
||||
}
|
||||
|
||||
func (f Float64s) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*[]float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]float64", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = f
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Float64s() []float64 {
|
||||
out := make([]float64, len(f))
|
||||
copy(out, f)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (f Float64s) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float64s) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewFloat64(in float64) Read {
|
||||
return Read{ParseValue: Float64(in)}
|
||||
}
|
||||
|
||||
type Float64 float64
|
||||
|
||||
func (f Float64) Any() interface{} {
|
||||
return float64(f)
|
||||
}
|
||||
|
||||
func (f Float64) ParseString() (string, error) {
|
||||
return fmt.Sprint(float64(f)), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseInt() (int, error) {
|
||||
return int(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseInt64() (int64, error) {
|
||||
return int64(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseUint() (uint, error) {
|
||||
return uint(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseUint64() (uint64, error) {
|
||||
return uint64(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseFloat64() (float64, error) {
|
||||
return float64(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("float64:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (f Float64) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(f), nil
|
||||
}
|
||||
|
||||
func (f Float64) ParseTime() (time.Time, error) {
|
||||
return time.Unix(0, int64(f*Float64(time.Second))), nil
|
||||
}
|
||||
|
||||
func (f Float64) Unmarshal(in interface{}) error {
|
||||
v, ok := in.(*float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *float64", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = float64(f)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package value_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func TestFloat64_Unmarshal(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
f := value.Float64(math.Pi)
|
||||
|
||||
var out float64
|
||||
|
||||
require.NoError(t, f.Unmarshal(&out))
|
||||
require.Equal(t, math.Pi, out)
|
||||
}
|
||||
|
||||
func TestFloat64_Any(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
f := value.Float64(math.Pi)
|
||||
|
||||
require.Equal(t, math.Pi, f.Any())
|
||||
}
|
||||
|
||||
func TestFloat64s_Unmarshal(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
f := value.Float64s{math.Pi, math.Sqrt2}
|
||||
|
||||
var out []float64
|
||||
|
||||
require.NoError(t, f.Unmarshal(&out))
|
||||
require.Equal(t, []float64{math.Pi, math.Sqrt2}, out)
|
||||
}
|
||||
|
||||
func TestFloat64s_Any(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
f := value.Float64s{math.Pi, math.Sqrt2}
|
||||
|
||||
require.Equal(t, []float64{math.Pi, math.Sqrt2}, f.Any())
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Int(0)
|
||||
_ SliceValue = Ints{}
|
||||
)
|
||||
|
||||
func NewInts(in []int) Slice {
|
||||
return Slice{SliceValue: Ints(in)}
|
||||
}
|
||||
|
||||
type Ints []int
|
||||
|
||||
func (i Ints) Unmarshal(in interface{}) error {
|
||||
val, ok := in.(*[]int)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]int", ErrWrongType)
|
||||
}
|
||||
|
||||
*val = i
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Any() interface{} {
|
||||
return i.Ints()
|
||||
}
|
||||
|
||||
func (i Ints) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Ints() []int {
|
||||
out := make([]int, len(i))
|
||||
copy(out, i)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (i Ints) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Ints) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewInt(in int) Read {
|
||||
return Read{ParseValue: Int(in)}
|
||||
}
|
||||
|
||||
type Int int
|
||||
|
||||
func (i Int) Unmarshal(in interface{}) error {
|
||||
v, ok := in.(*int)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *int", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = int(i)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int) ParseString() (string, error) {
|
||||
return strconv.Itoa(int(i)), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseInt() (int, error) {
|
||||
return int(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseInt64() (int64, error) {
|
||||
return int64(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseUint() (uint, error) {
|
||||
return uint(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseUint64() (uint64, error) {
|
||||
return uint64(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseFloat64() (float64, error) {
|
||||
return float64(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("int:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (i Int) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(i), nil
|
||||
}
|
||||
|
||||
func (i Int) ParseTime() (time.Time, error) {
|
||||
return time.Unix(0, int64(i)), nil
|
||||
}
|
||||
|
||||
func (i Int) Any() interface{} {
|
||||
return int(i)
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Int64(0)
|
||||
_ SliceValue = Int64s{}
|
||||
)
|
||||
|
||||
func NewInt64s(in []int64) Slice {
|
||||
return Slice{SliceValue: Int64s(in)}
|
||||
}
|
||||
|
||||
type Int64s []int64
|
||||
|
||||
func (i Int64s) Any() interface{} {
|
||||
return i.Int64s()
|
||||
}
|
||||
|
||||
func (i Int64s) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*[]int64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]int64", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = i
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Int64s() []int64 {
|
||||
out := make([]int64, len(i))
|
||||
copy(out, i)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (i Int64s) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int64s) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewInt64(in int64) Read {
|
||||
return Read{ParseValue: Int64(in)}
|
||||
}
|
||||
|
||||
type Int64 int64
|
||||
|
||||
func (i Int64) Any() interface{} {
|
||||
return int64(i)
|
||||
}
|
||||
|
||||
func (i Int64) ParseString() (string, error) {
|
||||
return strconv.FormatInt(int64(i), 10), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseInt() (int, error) {
|
||||
return int(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseInt64() (int64, error) {
|
||||
return int64(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseUint() (uint, error) {
|
||||
return uint(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseUint64() (uint64, error) {
|
||||
return uint64(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseFloat64() (float64, error) {
|
||||
return float64(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("int64:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (i Int64) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(i), nil
|
||||
}
|
||||
|
||||
func (i Int64) ParseTime() (time.Time, error) {
|
||||
return time.Unix(0, int64(i)), nil
|
||||
}
|
||||
|
||||
func (i Int64) Unmarshal(val interface{}) error {
|
||||
v, ok := val.(*int64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *int64", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = int64(i)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
)
|
||||
|
||||
var (
|
||||
_ Value = Read{}
|
||||
_ Value = Slice{}
|
||||
)
|
||||
|
||||
var ErrWrongType = errs.ErrWrongType
|
||||
|
||||
type Read struct {
|
||||
ParseValue
|
||||
}
|
||||
|
||||
func (r Read) String() string {
|
||||
sout, _ := r.ParseValue.ParseString()
|
||||
|
||||
return sout
|
||||
}
|
||||
|
||||
func (r Read) Int() int {
|
||||
iout, _ := r.ParseValue.ParseInt()
|
||||
|
||||
return iout
|
||||
}
|
||||
|
||||
func (r Read) Int64() int64 {
|
||||
iout, _ := r.ParseValue.ParseInt64()
|
||||
|
||||
return iout
|
||||
}
|
||||
|
||||
func (r Read) Uint() uint {
|
||||
uout, _ := r.ParseValue.ParseUint()
|
||||
|
||||
return uout
|
||||
}
|
||||
|
||||
func (r Read) Uint64() uint64 {
|
||||
uout, _ := r.ParseValue.ParseUint64()
|
||||
|
||||
return uout
|
||||
}
|
||||
|
||||
func (r Read) Float64() float64 {
|
||||
fout, _ := r.ParseValue.ParseFloat64()
|
||||
|
||||
return fout
|
||||
}
|
||||
|
||||
func (r Read) Bool() bool {
|
||||
bout, _ := r.ParseValue.ParseBool()
|
||||
|
||||
return bout
|
||||
}
|
||||
|
||||
func (r Read) Duration() time.Duration {
|
||||
dout, _ := r.ParseValue.ParseDuration()
|
||||
|
||||
return dout
|
||||
}
|
||||
|
||||
func (r Read) Time() time.Time {
|
||||
tout, _ := r.ParseValue.ParseTime()
|
||||
|
||||
return tout
|
||||
}
|
||||
|
||||
func (r Read) Strings() []string {
|
||||
return []string{r.String()}
|
||||
}
|
||||
|
||||
func (r Read) Ints() []int {
|
||||
return []int{r.Int()}
|
||||
}
|
||||
|
||||
func (r Read) Int64s() []int64 {
|
||||
return []int64{r.Int64()}
|
||||
}
|
||||
|
||||
func (r Read) Uints() []uint {
|
||||
return []uint{r.Uint()}
|
||||
}
|
||||
|
||||
func (r Read) Uint64s() []uint64 {
|
||||
return []uint64{r.Uint64()}
|
||||
}
|
||||
|
||||
func (r Read) Float64s() []float64 {
|
||||
return []float64{r.Float64()}
|
||||
}
|
||||
|
||||
func (r Read) Bools() []bool {
|
||||
return []bool{r.Bool()}
|
||||
}
|
||||
|
||||
func (r Read) Durations() []time.Duration {
|
||||
return []time.Duration{r.Duration()}
|
||||
}
|
||||
|
||||
func (r Read) Times() []time.Time {
|
||||
return []time.Time{r.Time()}
|
||||
}
|
||||
|
||||
type Slice struct {
|
||||
SliceValue
|
||||
}
|
||||
|
||||
func (s Slice) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s Slice) Int() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Int64() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Uint() uint {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Uint64() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Float64() float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Bool() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Slice) Duration() time.Duration {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s Slice) Time() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (s Slice) wrongType() error {
|
||||
return fmt.Errorf("%w: for %T", ErrWrongType, s.SliceValue)
|
||||
}
|
||||
|
||||
func (s Slice) ParseString() (string, error) {
|
||||
return "", s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseInt() (int, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseInt64() (int64, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseUint() (uint, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseUint64() (uint64, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseFloat64() (float64, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseBool() (bool, error) {
|
||||
return false, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseDuration() (time.Duration, error) {
|
||||
return 0, s.wrongType()
|
||||
}
|
||||
|
||||
func (s Slice) ParseTime() (time.Time, error) {
|
||||
return time.Time{}, s.wrongType()
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = (String)("")
|
||||
_ SliceValue = (Strings)(nil)
|
||||
)
|
||||
|
||||
func NewStrings(in []string) Slice {
|
||||
return Slice{SliceValue: Strings(in)}
|
||||
}
|
||||
|
||||
type Strings []string
|
||||
|
||||
func (s Strings) Unmarshal(in interface{}) error {
|
||||
val, ok := in.(*[]string)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]string", ErrWrongType)
|
||||
}
|
||||
|
||||
*val = s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Any() interface{} {
|
||||
return s.Strings()
|
||||
}
|
||||
|
||||
func (s Strings) Strings() []string {
|
||||
out := make([]string, len(s))
|
||||
copy(out, s)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (s Strings) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewString(in string) Value {
|
||||
return Read{ParseValue: String(in)}
|
||||
}
|
||||
|
||||
type String string
|
||||
|
||||
func (s String) ParseString() (string, error) {
|
||||
return string(s), nil
|
||||
}
|
||||
|
||||
func (s String) Unmarshal(in interface{}) error {
|
||||
v, ok := in.(*string)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *string", ErrWrongType)
|
||||
}
|
||||
|
||||
*v = string(s)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s String) Any() interface{} {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (s String) ParseInt() (int, error) {
|
||||
v, err := strconv.Atoi(string(s))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("string int:%w", err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s String) Int64() int64 {
|
||||
out, _ := s.ParseInt64()
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (s String) ParseInt64() (int64, error) {
|
||||
v, err := strconv.ParseInt(string(s), 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("string int64:%w", err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s String) ParseUint() (uint, error) {
|
||||
uout, err := s.ParseUint64()
|
||||
|
||||
return uint(uout), err
|
||||
}
|
||||
|
||||
func (s String) ParseUint64() (uint64, error) {
|
||||
uout, err := strconv.ParseUint(string(s), 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("string uint:%w", err)
|
||||
}
|
||||
|
||||
return uout, nil
|
||||
}
|
||||
|
||||
func (s String) ParseFloat64() (float64, error) {
|
||||
fout, err := strconv.ParseFloat(string(s), 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("string float64:%w", err)
|
||||
}
|
||||
|
||||
return fout, nil
|
||||
}
|
||||
|
||||
func (s String) ParseBool() (bool, error) {
|
||||
v, err := strconv.ParseBool(string(s))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("string bool:%w", err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s String) ParseDuration() (time.Duration, error) {
|
||||
v, err := time.ParseDuration(string(s))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("string duration:%w", err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s String) ParseTime() (time.Time, error) {
|
||||
v, err := time.Parse(time.RFC3339, string(s))
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("string time:%w", err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package value_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func TestStringUnmarshal(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
st := value.New("test")
|
||||
sta := value.New([]string{"test1", "test2"})
|
||||
|
||||
ac := ""
|
||||
require.NoError(t, st.Unmarshal(&ac))
|
||||
require.Equal(t, "test", ac)
|
||||
|
||||
aca := []string{}
|
||||
require.NoError(t, sta.Unmarshal(&aca))
|
||||
require.Equal(t, []string{"test1", "test2"}, aca)
|
||||
|
||||
require.ErrorIs(t, sta.Unmarshal(ac), value.ErrWrongType)
|
||||
require.ErrorIs(t, sta.Unmarshal(&ac), value.ErrWrongType)
|
||||
require.ErrorIs(t, st.Unmarshal(aca), value.ErrWrongType)
|
||||
require.ErrorIs(t, st.Unmarshal(&aca), value.ErrWrongType)
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Time{time.Now()}
|
||||
_ SliceValue = (Times)(nil)
|
||||
)
|
||||
|
||||
func NewTimes(in []time.Time) Slice {
|
||||
return Slice{SliceValue: Times(in)}
|
||||
}
|
||||
|
||||
type Times []time.Time
|
||||
|
||||
func (t Times) Any() interface{} {
|
||||
return t.Times()
|
||||
}
|
||||
|
||||
func (t Times) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*[]time.Time)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]time.Time", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = t
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Times) Times() []time.Time {
|
||||
out := make([]time.Time, len(t))
|
||||
copy(out, t)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func NewTime(in time.Time) Read {
|
||||
return Read{ParseValue: Time{Time: in}}
|
||||
}
|
||||
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t Time) ParseString() (string, error) {
|
||||
return t.Format(time.RFC3339), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseInt() (int, error) {
|
||||
return int(t.Unix()), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseInt64() (int64, error) {
|
||||
return t.Unix(), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseUint() (uint, error) {
|
||||
return uint(t.Unix()), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseUint64() (uint64, error) {
|
||||
return uint64(t.Unix()), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseFloat64() (float64, error) {
|
||||
return float64(t.UnixNano()), nil
|
||||
}
|
||||
|
||||
func (t Time) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("time bool:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (t Time) ParseDuration() (time.Duration, error) {
|
||||
return 0, fmt.Errorf("time duration:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (t Time) ParseTime() (time.Time, error) {
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t Time) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*time.Time)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *time.Time", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = t.Time
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Time) Any() interface{} {
|
||||
return t.Time
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Uint(0)
|
||||
_ SliceValue = (Uints)(nil)
|
||||
)
|
||||
|
||||
func NewUints(in []uint) Slice {
|
||||
return Slice{SliceValue: Uints(in)}
|
||||
}
|
||||
|
||||
type Uints []uint
|
||||
|
||||
func (u Uints) Any() interface{} {
|
||||
return u.Uints()
|
||||
}
|
||||
|
||||
func (u Uints) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*[]uint)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]uint", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = u
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Uints() []uint {
|
||||
out := make([]uint, len(u))
|
||||
copy(out, u)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (u Uints) Uint64s() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uints) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewUint(in uint) Read {
|
||||
return Read{ParseValue: Uint(in)}
|
||||
}
|
||||
|
||||
type Uint uint
|
||||
|
||||
func (u Uint) ParseString() (string, error) {
|
||||
return strconv.FormatUint(uint64(u), 10), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseInt() (int, error) {
|
||||
return int(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseInt64() (int64, error) {
|
||||
return int64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseUint() (uint, error) {
|
||||
return uint(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseUint64() (uint64, error) {
|
||||
return uint64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseFloat64() (float64, error) {
|
||||
return float64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("uint:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (u Uint) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(u), nil
|
||||
}
|
||||
|
||||
func (u Uint) ParseTime() (time.Time, error) {
|
||||
return time.Unix(0, int64(u)), nil
|
||||
}
|
||||
|
||||
func (u Uint) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*uint)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *uint", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = uint(u)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint) Any() interface{} {
|
||||
return uint(u)
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ParseValue = Uint64(0)
|
||||
_ SliceValue = (Uint64s)(nil)
|
||||
)
|
||||
|
||||
func NewUint64s(in []uint64) Slice {
|
||||
return Slice{SliceValue: Uint64s(in)}
|
||||
}
|
||||
|
||||
type Uint64s []uint64
|
||||
|
||||
func (u Uint64s) Any() interface{} {
|
||||
return u.Uint64s()
|
||||
}
|
||||
|
||||
func (u Uint64s) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*[]uint64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *[]uint64", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = u
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Strings() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Ints() []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Int64s() []int64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Uints() []uint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Uint64s() []uint64 {
|
||||
out := make([]uint64, len(u))
|
||||
copy(out, u)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (u Uint64s) Float64s() []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Bools() []bool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Durations() []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64s) Times() []time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewUint64(in uint64) Read {
|
||||
return Read{ParseValue: Uint64(in)}
|
||||
}
|
||||
|
||||
type Uint64 uint64
|
||||
|
||||
func (u Uint64) ParseString() (string, error) {
|
||||
return strconv.FormatUint(uint64(u), 10), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseInt() (int, error) {
|
||||
return int(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseInt64() (int64, error) {
|
||||
return int64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseUint() (uint, error) {
|
||||
return uint(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseUint64() (uint64, error) {
|
||||
return uint64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseFloat64() (float64, error) {
|
||||
return float64(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseBool() (bool, error) {
|
||||
return false, fmt.Errorf("uint64 bool:%w", ErrWrongType)
|
||||
}
|
||||
|
||||
func (u Uint64) ParseDuration() (time.Duration, error) {
|
||||
return time.Duration(u), nil
|
||||
}
|
||||
|
||||
func (u Uint64) ParseTime() (time.Time, error) {
|
||||
return time.Unix(0, int64(0)), nil
|
||||
}
|
||||
|
||||
func (u Uint64) Unmarshal(val interface{}) error {
|
||||
res, ok := val.(*uint64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *uint64", ErrWrongType)
|
||||
}
|
||||
|
||||
*res = uint64(u)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Uint64) Any() interface{} {
|
||||
return uint64(u)
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package value
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Value interface {
|
||||
ReadValue
|
||||
ParseValue
|
||||
ArrValue
|
||||
}
|
||||
|
||||
type UnmarshalValue interface {
|
||||
Unmarshal(val interface{}) error
|
||||
}
|
||||
|
||||
type ReadValue interface {
|
||||
String() string
|
||||
Int() int
|
||||
Int64() int64
|
||||
Uint() uint
|
||||
Uint64() uint64
|
||||
Float64() float64
|
||||
Bool() bool
|
||||
Duration() time.Duration
|
||||
Time() time.Time
|
||||
}
|
||||
|
||||
type AnyValue interface {
|
||||
Any() interface{}
|
||||
}
|
||||
|
||||
type SliceValue interface {
|
||||
AnyValue
|
||||
UnmarshalValue
|
||||
ArrValue
|
||||
}
|
||||
|
||||
type ArrValue interface {
|
||||
Strings() []string
|
||||
Ints() []int
|
||||
Int64s() []int64
|
||||
Uints() []uint
|
||||
Uint64s() []uint64
|
||||
Float64s() []float64
|
||||
Bools() []bool
|
||||
Durations() []time.Duration
|
||||
Times() []time.Time
|
||||
}
|
||||
|
||||
//nolint:interfacebloat
|
||||
type ParseValue interface {
|
||||
ParseString() (string, error)
|
||||
ParseInt() (int, error)
|
||||
ParseInt64() (int64, error)
|
||||
ParseUint() (uint, error)
|
||||
ParseUint64() (uint64, error)
|
||||
ParseFloat64() (float64, error)
|
||||
ParseBool() (bool, error)
|
||||
ParseDuration() (time.Duration, error)
|
||||
ParseTime() (time.Time, error)
|
||||
UnmarshalValue
|
||||
AnyValue
|
||||
}
|
||||
|
||||
type Append interface {
|
||||
Value
|
||||
Append(string) (Value, error)
|
||||
}
|
||||
|
||||
//nolint:gocyclo,cyclop
|
||||
func New(in interface{}) Value {
|
||||
switch val := in.(type) {
|
||||
case bool:
|
||||
return Read{Bool(val)}
|
||||
case []bool:
|
||||
return NewBools(val)
|
||||
case string:
|
||||
return Read{String(val)}
|
||||
case int:
|
||||
return Read{Int(val)}
|
||||
case int64:
|
||||
return Read{Int64(val)}
|
||||
case uint:
|
||||
return Read{Uint(val)}
|
||||
case uint64:
|
||||
return Read{Uint64(val)}
|
||||
case float64:
|
||||
return Read{Float64(val)}
|
||||
case time.Duration:
|
||||
return Read{Duration(val)}
|
||||
case time.Time:
|
||||
return Read{Time{val}}
|
||||
case []int64:
|
||||
return Slice{Int64s(val)}
|
||||
case []uint:
|
||||
return Slice{Uints(val)}
|
||||
case []uint64:
|
||||
return Slice{Uint64s(val)}
|
||||
case []float64:
|
||||
return Slice{Float64s(val)}
|
||||
case []time.Duration:
|
||||
return Slice{Durations(val)}
|
||||
case []time.Time:
|
||||
return Slice{Times(val)}
|
||||
case []string:
|
||||
return Slice{Strings(val)}
|
||||
case []int:
|
||||
return Slice{Ints(val)}
|
||||
case []interface{}:
|
||||
return Read{Any{v: val}}
|
||||
case Value:
|
||||
return val
|
||||
default:
|
||||
if in != nil {
|
||||
return Read{Any{v: in}}
|
||||
}
|
||||
|
||||
return Empty()
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package variable
|
||||
|
||||
//go:generate stringer -type=ArgType -linecomment
|
||||
|
||||
type ArgType int
|
||||
|
||||
const (
|
||||
TypeOption ArgType = iota + 1 // option
|
||||
TypeArgument // argument
|
||||
)
|
||||
@@ -1,25 +0,0 @@
|
||||
// Code generated by "stringer -type=ArgType -linecomment"; DO NOT EDIT.
|
||||
|
||||
package variable
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[TypeOption-1]
|
||||
_ = x[TypeArgument-2]
|
||||
}
|
||||
|
||||
const _ArgType_name = "optionargument"
|
||||
|
||||
var _ArgType_index = [...]uint8{0, 6, 14}
|
||||
|
||||
func (i ArgType) String() string {
|
||||
i -= 1
|
||||
if i < 0 || i >= ArgType(len(_ArgType_index)-1) {
|
||||
return "ArgType(" + strconv.FormatInt(int64(i+1), 10) + ")"
|
||||
}
|
||||
return _ArgType_name[_ArgType_index[i]:_ArgType_index[i+1]]
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Bool(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts, WithParse(CreateBool, AppendBool), Value(flag.Bool))...)
|
||||
}
|
||||
|
||||
func CreateBool(in string) (value.Value, error) {
|
||||
out, err := strconv.ParseBool(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create bool:%w", err)
|
||||
}
|
||||
|
||||
return value.NewBool(out), nil
|
||||
}
|
||||
|
||||
func AppendBool(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.ParseBool(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create bool:%w", err)
|
||||
}
|
||||
|
||||
return value.NewBools(append(old.Bools(), out)), nil
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Duration(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts, WithParse(CreateDuration, AppendDuration), Value(flag.Duration))...)
|
||||
}
|
||||
|
||||
func CreateDuration(in string) (value.Value, error) {
|
||||
out, err := time.ParseDuration(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create duration:%w", err)
|
||||
}
|
||||
|
||||
return value.NewDuration(out), nil
|
||||
}
|
||||
|
||||
func AppendDuration(old value.Value, in string) (value.Value, error) {
|
||||
out, err := time.ParseDuration(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append duration:%w", err)
|
||||
}
|
||||
|
||||
return value.NewDurations(append(old.Durations(), out)), nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Name string
|
||||
Err error
|
||||
Type ArgType
|
||||
}
|
||||
|
||||
func (o Error) Error() string {
|
||||
return fmt.Sprintf("%s: '%s' %s", o.Type, o.Name, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Is(err error) bool {
|
||||
return errors.Is(err, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Unwrap() error {
|
||||
return o.Err
|
||||
}
|
||||
|
||||
func Err(name string, t ArgType, err error) Error {
|
||||
return Error{
|
||||
Name: name,
|
||||
Type: t,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Float64(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts, WithParse(CreateFloat64, AppendFloat64), Value(flag.Float64))...)
|
||||
}
|
||||
|
||||
func CreateFloat64(in string) (value.Value, error) {
|
||||
out, err := strconv.ParseFloat(in, 10)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create float64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewFloat64(out), nil
|
||||
}
|
||||
|
||||
func AppendFloat64(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.ParseFloat(in, 10)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append float64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewFloat64s(append(old.Float64s(), out)), nil
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Int(name, description string, opts ...Option) Variable {
|
||||
return New(name, description, append(opts, WithParse(CreateInt, AppendInt), Value(flag.Int))...)
|
||||
}
|
||||
|
||||
func AppendInt(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.Atoi(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append int:%w", err)
|
||||
}
|
||||
|
||||
return value.NewInts(append(old.Ints(), out)), nil
|
||||
}
|
||||
|
||||
func CreateInt(in string) (value.Value, error) {
|
||||
out, err := strconv.Atoi(in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create int:%w", err)
|
||||
}
|
||||
|
||||
return value.NewInt(out), nil
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Int64(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts, WithParse(CreateInt64, AppendInt64), Value(flag.Int64))...)
|
||||
}
|
||||
|
||||
func CreateInt64(in string) (value.Value, error) {
|
||||
out, err := strconv.ParseInt(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create int64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewInt64(out), nil
|
||||
}
|
||||
|
||||
func AppendInt64(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.ParseInt(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append int64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewInt64s(append(old.Int64s(), out)), nil
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func String(name, description string, opts ...Option) Variable {
|
||||
return New(name, description, opts...)
|
||||
}
|
||||
|
||||
func CreateString(in string) (value.Value, error) {
|
||||
return value.NewString(in), nil
|
||||
}
|
||||
|
||||
func AppendString(old value.Value, in string) (value.Value, error) {
|
||||
return value.NewStrings(append(old.Strings(), in)), nil
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
const (
|
||||
ParamFormat = "format"
|
||||
)
|
||||
|
||||
func Time(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts,
|
||||
WithParamParse(CreateTime, AppendTime),
|
||||
WithParam(ParamFormat, RFC3339),
|
||||
Value(flag.Time),
|
||||
)...)
|
||||
}
|
||||
|
||||
func RFC3339(in interface{}) error {
|
||||
v, ok := in.(*string)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: expect *string got %T", errs.ErrWrongType, in)
|
||||
}
|
||||
|
||||
*v = time.RFC3339
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateTime(param Param) Create {
|
||||
var (
|
||||
formatErr error
|
||||
format string
|
||||
)
|
||||
|
||||
formatErr = param.Value(ParamFormat, &format)
|
||||
|
||||
return func(in string) (value.Value, error) {
|
||||
if formatErr != nil {
|
||||
return nil, fmt.Errorf("create format:%w", formatErr)
|
||||
}
|
||||
|
||||
out, err := time.Parse(format, in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create time:%w", err)
|
||||
}
|
||||
|
||||
return value.NewTime(out), nil
|
||||
}
|
||||
}
|
||||
|
||||
func AppendTime(param Param) Append {
|
||||
var (
|
||||
formatErr error
|
||||
format string
|
||||
)
|
||||
|
||||
formatErr = param.Value(ParamFormat, &format)
|
||||
|
||||
return func(old value.Value, in string) (value.Value, error) {
|
||||
if formatErr != nil {
|
||||
return nil, fmt.Errorf("append format:%w", formatErr)
|
||||
}
|
||||
|
||||
out, err := time.Parse(format, in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append time:%w", err)
|
||||
}
|
||||
|
||||
return value.NewTimes(append(old.Times(), out)), nil
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Uint(name, description string, opts ...Option) Variable {
|
||||
return String(name, description, append(opts, WithParse(CreateUint, AppendUint), Value(flag.Uint))...)
|
||||
}
|
||||
|
||||
func CreateUint(in string) (value.Value, error) {
|
||||
out, err := strconv.ParseUint(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create uint:%w", err)
|
||||
}
|
||||
|
||||
return value.NewUint(uint(out)), nil
|
||||
}
|
||||
|
||||
func AppendUint(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.ParseUint(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append uint:%w", err)
|
||||
}
|
||||
|
||||
return value.NewUints(append(old.Uints(), uint(out))), nil
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
func Uint64(name, descriontion string, opts ...Option) Variable {
|
||||
return String(name, descriontion, append(opts, WithParse(CreateUint64, AppendUint64), Value(flag.Uint64))...)
|
||||
}
|
||||
|
||||
func CreateUint64(in string) (value.Value, error) {
|
||||
out, err := strconv.ParseUint(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create uint64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewUint64(out), nil
|
||||
}
|
||||
|
||||
func AppendUint64(old value.Value, in string) (value.Value, error) {
|
||||
out, err := strconv.ParseUint(in, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("append uint64:%w", err)
|
||||
}
|
||||
|
||||
return value.NewUint64s(append(old.Uint64s(), out)), nil
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/errs"
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Option func(*Variable)
|
||||
|
||||
func WithType(t ArgType) Option {
|
||||
return func(v *Variable) {
|
||||
v.Type = t
|
||||
}
|
||||
}
|
||||
|
||||
func ArgOption(v *Variable) {
|
||||
v.Type = TypeOption
|
||||
}
|
||||
|
||||
func ArgArgument(v *Variable) {
|
||||
v.Type = TypeArgument
|
||||
}
|
||||
|
||||
func Value(in flag.Flag) Option {
|
||||
return func(v *Variable) {
|
||||
v.Flag |= in
|
||||
}
|
||||
}
|
||||
|
||||
func Default(in value.Value) Option {
|
||||
return func(v *Variable) {
|
||||
v.Default = in
|
||||
}
|
||||
}
|
||||
|
||||
func Required(v *Variable) {
|
||||
v.Flag |= flag.Required
|
||||
}
|
||||
|
||||
func WithParse(create Create, update Append) Option {
|
||||
return func(v *Variable) {
|
||||
v.append = func(Param) Append { return update }
|
||||
v.create = func(Param) Create { return create }
|
||||
}
|
||||
}
|
||||
|
||||
func WithParamParse(create func(Param) Create, update func(Param) Append) Option {
|
||||
return func(v *Variable) {
|
||||
v.append = update
|
||||
v.create = create
|
||||
}
|
||||
}
|
||||
|
||||
func Valid(f ...func(value.Value) error) Option {
|
||||
return func(v *Variable) {
|
||||
v.Valid = f
|
||||
}
|
||||
}
|
||||
|
||||
func Array(o *Variable) {
|
||||
o.Flag |= flag.Array
|
||||
}
|
||||
|
||||
func WithParam(name string, fn func(interface{}) error) Option {
|
||||
return func(v *Variable) {
|
||||
v.params[name] = fn
|
||||
}
|
||||
}
|
||||
|
||||
type (
|
||||
Create func(s string) (value.Value, error)
|
||||
Append func(old value.Value, s string) (value.Value, error)
|
||||
)
|
||||
|
||||
func New(name, description string, opts ...Option) Variable {
|
||||
res := Variable{
|
||||
Name: name,
|
||||
Description: description,
|
||||
Type: TypeOption,
|
||||
create: func(Param) Create { return CreateString },
|
||||
append: func(Param) Append { return AppendString },
|
||||
params: make(Params),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&res)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
type Variable struct {
|
||||
Name string
|
||||
Description string
|
||||
Alias string
|
||||
Flag flag.Flag
|
||||
Type ArgType
|
||||
Default value.Value
|
||||
Valid []func(value.Value) error
|
||||
params Params
|
||||
create func(Param) Create
|
||||
append func(Param) Append
|
||||
}
|
||||
|
||||
func (v Variable) Validate(in value.Value) error {
|
||||
for _, valid := range v.Valid {
|
||||
if err := valid(in); err != nil {
|
||||
return Err(v.Name, v.Type, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Variable) IsArray() bool {
|
||||
return v.Flag.IsArray()
|
||||
}
|
||||
|
||||
func (v Variable) IsRequired() bool {
|
||||
return v.Flag.IsRequired()
|
||||
}
|
||||
|
||||
func (v Variable) HasDefault() bool {
|
||||
return v.Default != nil
|
||||
}
|
||||
|
||||
func (v Variable) IsBool() bool {
|
||||
return v.Flag.IsBool()
|
||||
}
|
||||
|
||||
func (v Variable) HasShort() bool {
|
||||
return v.Type == TypeOption && len(v.Alias) == 1
|
||||
}
|
||||
|
||||
func (v Variable) Create(s string) (value.Value, error) {
|
||||
return v.create(v.params)(s)
|
||||
}
|
||||
|
||||
func (v Variable) Append(old value.Value, s string) (value.Value, error) {
|
||||
return v.append(v.params)(old, s)
|
||||
}
|
||||
|
||||
type Param interface {
|
||||
Value(name string, v interface{}) error
|
||||
}
|
||||
|
||||
type Params map[string]func(interface{}) error
|
||||
|
||||
func (p Params) Value(name string, v interface{}) error {
|
||||
if p, ok := p[name]; ok {
|
||||
return p(v)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: param %v", errs.ErrNotFound, name)
|
||||
}
|
||||
47
list.go
47
list.go
@@ -5,10 +5,13 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/argument"
|
||||
"gitoa.ru/go-4devs/console/input/option"
|
||||
"gitoa.ru/go-4devs/console/input/validator"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/validator"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/console/output/descriptor"
|
||||
)
|
||||
@@ -20,6 +23,10 @@ func init() {
|
||||
MustRegister(list())
|
||||
}
|
||||
|
||||
const (
|
||||
ArgumentNamespace = "namespace"
|
||||
)
|
||||
|
||||
func list() *Command {
|
||||
return &Command{
|
||||
Name: CommandList,
|
||||
@@ -33,18 +40,16 @@ You can also output the information in other formats by using the <comment>--for
|
||||
<info>{{ .Bin }} {{ .Name }} --format=xml</info>
|
||||
`,
|
||||
Execute: executeList,
|
||||
Configure: func(ctx context.Context, config *input.Definition) error {
|
||||
Configure: func(_ context.Context, cfg config.Definition) error {
|
||||
formats := descriptor.Descriptors()
|
||||
config.
|
||||
SetArguments(
|
||||
argument.String("namespace", "The namespace name"),
|
||||
).
|
||||
SetOptions(
|
||||
option.String(helpOptFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")),
|
||||
cfg.
|
||||
Add(
|
||||
arg.String(ArgumentNamespace, "The namespace name"),
|
||||
option.String(OptionFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")),
|
||||
option.Required,
|
||||
option.Default(formats[0]),
|
||||
option.Valid(
|
||||
validator.NotBlank(0),
|
||||
option.Default(value.New(formats[0])),
|
||||
validator.Valid(
|
||||
validator.NotBlank,
|
||||
validator.Enum(formats...),
|
||||
),
|
||||
),
|
||||
@@ -56,19 +61,19 @@ You can also output the information in other formats by using the <comment>--for
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func executeList(ctx context.Context, in input.Input, out output.Output) error {
|
||||
ns := in.Argument(ctx, "namespace").String()
|
||||
format := in.Option(ctx, helpOptFormat).String()
|
||||
func executeList(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
ns := ReadValue(ctx, in, ArgumentNamespace).String()
|
||||
format := ReadValue(ctx, in, OptionFormat).String()
|
||||
|
||||
des, err := descriptor.Find(format)
|
||||
if err != nil {
|
||||
return fmt.Errorf("find descriptor: %w", err)
|
||||
return fmt.Errorf("find descriptor[%v]: %w", format, err)
|
||||
}
|
||||
|
||||
cmds := Commands()
|
||||
commands := descriptor.Commands{
|
||||
Namespace: ns,
|
||||
Definition: Default(input.NewDefinition()),
|
||||
Namespace: ns,
|
||||
Options: definition.New(Default()...).With(param.New(descriptor.TxtStyle())),
|
||||
}
|
||||
groups := make(map[string]*descriptor.NSCommand)
|
||||
namespaces := make([]string, 0, len(cmds))
|
||||
@@ -84,7 +89,7 @@ func executeList(ctx context.Context, in input.Input, out output.Output) error {
|
||||
continue
|
||||
}
|
||||
|
||||
gn := strings.SplitN(name, ":", 2)
|
||||
gn := strings.SplitN(name, ":", defaultLenNamespace)
|
||||
if len(gn) != defaultLenNamespace {
|
||||
empty.Append(cmd.Name, cmd.Description)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
@@ -20,17 +20,19 @@ var (
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
config.Options
|
||||
|
||||
Bin string
|
||||
Name string
|
||||
Description string
|
||||
Help string
|
||||
Definition *input.Definition
|
||||
}
|
||||
|
||||
type Commands struct {
|
||||
Namespace string
|
||||
Definition *input.Definition
|
||||
Commands []NSCommand
|
||||
config.Options
|
||||
|
||||
Namespace string
|
||||
Commands []NSCommand
|
||||
}
|
||||
|
||||
type NSCommand struct {
|
||||
|
||||
@@ -4,20 +4,19 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
"gitoa.ru/go-4devs/console/input/flag"
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultSpace = 2
|
||||
infoLen = 13
|
||||
defaultSpace = 2
|
||||
dashDelimiter = "-"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
@@ -38,8 +37,8 @@ var (
|
||||
|
||||
{{ end -}}
|
||||
<comment>Usage:</comment>
|
||||
{{ .Name }} {{ synopsis .Definition }}
|
||||
{{- definition .Definition }}
|
||||
{{ .Name }} {{ synopsis .Options }}
|
||||
{{ definition .Options }}
|
||||
{{- help . }}
|
||||
`))
|
||||
|
||||
@@ -47,17 +46,31 @@ var (
|
||||
Funcs(txtFunc).
|
||||
Parse(`<comment>Usage:</comment>
|
||||
command [options] [arguments]
|
||||
{{- definition .Definition }}
|
||||
{{ definition .Options }}
|
||||
{{- commands .Commands -}}
|
||||
`))
|
||||
)
|
||||
|
||||
func TxtStyle() param.Option {
|
||||
return arg.WithStyle(
|
||||
arg.Style{
|
||||
Start: "<comment>",
|
||||
End: "</comment>",
|
||||
},
|
||||
arg.Style{
|
||||
Start: "<info>",
|
||||
End: "</info>",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
type txt struct{}
|
||||
|
||||
func (t *txt) Command(ctx context.Context, out output.Output, cmd Command) error {
|
||||
var tpl bytes.Buffer
|
||||
|
||||
if err := txtHelpTemplate.Execute(&tpl, cmd); err != nil {
|
||||
err := txtHelpTemplate.Execute(&tpl, cmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute txt help tpl:%w", err)
|
||||
}
|
||||
|
||||
@@ -69,7 +82,8 @@ func (t *txt) Command(ctx context.Context, out output.Output, cmd Command) error
|
||||
func (t *txt) Commands(ctx context.Context, out output.Output, cmds Commands) error {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := txtListTemplate.Execute(&buf, cmds); err != nil {
|
||||
err := txtListTemplate.Execute(&buf, cmds)
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute txt list tpl:%w", err)
|
||||
}
|
||||
|
||||
@@ -78,80 +92,8 @@ func (t *txt) Commands(ctx context.Context, out output.Output, cmds Commands) er
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func txtDefaultArray(val value.Value, fl flag.Flag) string {
|
||||
st := val.Strings()
|
||||
|
||||
switch {
|
||||
case fl.IsInt():
|
||||
for _, i := range val.Ints() {
|
||||
st = append(st, strconv.Itoa(i))
|
||||
}
|
||||
case fl.IsInt64():
|
||||
for _, i := range val.Int64s() {
|
||||
st = append(st, strconv.FormatInt(i, 10))
|
||||
}
|
||||
case fl.IsUint():
|
||||
for _, u := range val.Uints() {
|
||||
st = append(st, strconv.FormatUint(uint64(u), 10))
|
||||
}
|
||||
case fl.IsUint64():
|
||||
for _, u := range val.Uint64s() {
|
||||
st = append(st, strconv.FormatUint(u, 10))
|
||||
}
|
||||
case fl.IsFloat64():
|
||||
for _, f := range val.Float64s() {
|
||||
st = append(st, strconv.FormatFloat(f, 'g', -1, 64))
|
||||
}
|
||||
case fl.IsDuration():
|
||||
for _, d := range val.Durations() {
|
||||
st = append(st, d.String())
|
||||
}
|
||||
case fl.IsTime():
|
||||
for _, d := range val.Times() {
|
||||
st = append(st, d.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(st, ",")
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func txtDefault(val value.Value, fl flag.Flag) []byte {
|
||||
var buf bytes.Buffer
|
||||
|
||||
buf.WriteString("<comment> [default: ")
|
||||
|
||||
switch {
|
||||
case fl.IsArray():
|
||||
buf.WriteString(txtDefaultArray(val, fl))
|
||||
case fl.IsInt():
|
||||
buf.WriteString(strconv.Itoa(val.Int()))
|
||||
case fl.IsInt64():
|
||||
buf.WriteString(strconv.FormatInt(val.Int64(), 10))
|
||||
case fl.IsUint():
|
||||
buf.WriteString(strconv.FormatUint(uint64(val.Uint()), 10))
|
||||
case fl.IsUint64():
|
||||
buf.WriteString(strconv.FormatUint(val.Uint64(), 10))
|
||||
case fl.IsFloat64():
|
||||
buf.WriteString(strconv.FormatFloat(val.Float64(), 'g', -1, 64))
|
||||
case fl.IsDuration():
|
||||
buf.WriteString(val.Duration().String())
|
||||
case fl.IsTime():
|
||||
buf.WriteString(val.Time().Format(time.RFC3339))
|
||||
case fl.IsAny():
|
||||
buf.WriteString(fmt.Sprint(val.Any()))
|
||||
default:
|
||||
buf.WriteString(val.String())
|
||||
}
|
||||
|
||||
buf.WriteString("]</comment>")
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func txtCommands(cmds []NSCommand) string {
|
||||
max := commandsTotalWidth(cmds)
|
||||
width := commandsTotalWidth(cmds)
|
||||
showNS := len(cmds) > 1
|
||||
|
||||
var buf bytes.Buffer
|
||||
@@ -177,7 +119,7 @@ func txtCommands(cmds []NSCommand) string {
|
||||
buf.WriteString(" <info>")
|
||||
buf.WriteString(cmd.Name)
|
||||
buf.WriteString("</info>")
|
||||
buf.WriteString(strings.Repeat(" ", max-len(cmd.Name)+defaultSpace))
|
||||
buf.WriteString(strings.Repeat(" ", width-len(cmd.Name)+defaultSpace))
|
||||
buf.WriteString(cmd.Description)
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
@@ -201,124 +143,48 @@ func txtHelp(cmd Command) string {
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func txtDefinitionOption(maxLen int, def *input.Definition) string {
|
||||
buf := bytes.Buffer{}
|
||||
opts := def.Options()
|
||||
|
||||
buf.WriteString("\n\n<comment>Options:</comment>\n")
|
||||
|
||||
for _, name := range opts {
|
||||
opt, _ := def.Option(name)
|
||||
|
||||
var op bytes.Buffer
|
||||
|
||||
op.WriteString(" <info>")
|
||||
|
||||
if opt.HasShort() {
|
||||
op.WriteString("-")
|
||||
op.WriteString(opt.Alias)
|
||||
op.WriteString(", ")
|
||||
} else {
|
||||
op.WriteString(" ")
|
||||
}
|
||||
|
||||
op.WriteString("--")
|
||||
op.WriteString(opt.Name)
|
||||
|
||||
if !opt.IsBool() {
|
||||
if !opt.IsRequired() {
|
||||
op.WriteString("[")
|
||||
}
|
||||
|
||||
op.WriteString("=")
|
||||
op.WriteString(strings.ToUpper(opt.Name))
|
||||
|
||||
if !opt.IsRequired() {
|
||||
op.WriteString("]")
|
||||
}
|
||||
}
|
||||
|
||||
op.WriteString("</info>")
|
||||
buf.Write(op.Bytes())
|
||||
buf.WriteString(strings.Repeat(" ", maxLen+17-op.Len()))
|
||||
buf.WriteString(opt.Description)
|
||||
|
||||
if opt.HasDefault() {
|
||||
buf.Write(txtDefault(opt.Default, opt.Flag))
|
||||
}
|
||||
|
||||
if opt.IsArray() {
|
||||
buf.WriteString("<comment> (multiple values allowed)</comment>")
|
||||
}
|
||||
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func txtDefinition(def *input.Definition) string {
|
||||
max := totalWidth(def)
|
||||
|
||||
func txtDefinition(options config.Options) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if args := def.Arguments(); len(args) > 0 {
|
||||
buf.WriteString("\n\n<comment>Arguments:</comment>\n")
|
||||
|
||||
for pos := range args {
|
||||
var ab bytes.Buffer
|
||||
|
||||
arg, _ := def.Argument(pos)
|
||||
|
||||
ab.WriteString(" <info>")
|
||||
ab.WriteString(arg.Name)
|
||||
ab.WriteString("</info>")
|
||||
ab.WriteString(strings.Repeat(" ", max+infoLen+defaultSpace-ab.Len()))
|
||||
|
||||
buf.Write(ab.Bytes())
|
||||
buf.WriteString(arg.Description)
|
||||
|
||||
if arg.HasDefault() {
|
||||
buf.Write(txtDefault(arg.Default, arg.Flag))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if opts := def.Options(); len(opts) > 0 {
|
||||
buf.WriteString(txtDefinitionOption(max, def))
|
||||
err := arg.NewDump().Reference(&buf, options)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func txtSynopsis(def *input.Definition) string {
|
||||
func txtSynopsis(options config.Options) string {
|
||||
def := arg.NewViews(options, nil)
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
if len(def.Options()) > 0 {
|
||||
buf.WriteString("[options] ")
|
||||
}
|
||||
|
||||
if buf.Len() > 0 && len(def.Arguments()) > 0 {
|
||||
args := def.Arguments()
|
||||
|
||||
if buf.Len() > 0 && len(args) > 0 {
|
||||
buf.WriteString("[--]")
|
||||
}
|
||||
|
||||
var opt int
|
||||
|
||||
for pos := range def.Arguments() {
|
||||
for _, arg := range args {
|
||||
buf.WriteString(" ")
|
||||
|
||||
arg, _ := def.Argument(pos)
|
||||
|
||||
if !arg.IsRequired() {
|
||||
if !option.IsRequired(arg) {
|
||||
buf.WriteString("[")
|
||||
|
||||
opt++
|
||||
}
|
||||
|
||||
buf.WriteString("<")
|
||||
buf.WriteString(arg.Name)
|
||||
buf.WriteString(arg.Name(dashDelimiter))
|
||||
buf.WriteString(">")
|
||||
|
||||
if arg.IsArray() {
|
||||
if option.IsSlice(arg) {
|
||||
buf.WriteString("...")
|
||||
}
|
||||
}
|
||||
@@ -329,47 +195,15 @@ func txtSynopsis(def *input.Definition) string {
|
||||
}
|
||||
|
||||
func commandsTotalWidth(cmds []NSCommand) int {
|
||||
var max int
|
||||
var width int
|
||||
|
||||
for _, ns := range cmds {
|
||||
for _, cmd := range ns.Commands {
|
||||
if len(cmd.Name) > max {
|
||||
max = len(cmd.Name)
|
||||
if len(cmd.Name) > width {
|
||||
width = len(cmd.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return max
|
||||
}
|
||||
|
||||
func totalWidth(def *input.Definition) int {
|
||||
var max int
|
||||
|
||||
for pos := range def.Arguments() {
|
||||
arg, _ := def.Argument(pos)
|
||||
l := len(arg.Name)
|
||||
|
||||
if l > max {
|
||||
max = l
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range def.Options() {
|
||||
opt, _ := def.Option(name)
|
||||
current := len(opt.Name) + 6
|
||||
|
||||
if !opt.IsBool() {
|
||||
current = current*2 + 1
|
||||
}
|
||||
|
||||
if opt.HasDefault() {
|
||||
current += 2
|
||||
}
|
||||
|
||||
if current > max {
|
||||
max = current
|
||||
}
|
||||
}
|
||||
|
||||
return max
|
||||
return width
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ type Formatter struct {
|
||||
styles func(string) (style.Style, error)
|
||||
}
|
||||
|
||||
func (a *Formatter) Format(ctx context.Context, msg string) string {
|
||||
func (a *Formatter) Format(_ context.Context, msg string) string {
|
||||
var (
|
||||
out bytes.Buffer
|
||||
cur int
|
||||
@@ -50,8 +50,8 @@ func (a *Formatter) Format(ctx context.Context, msg string) string {
|
||||
err error
|
||||
)
|
||||
|
||||
switch {
|
||||
case tag[0:1] == "/":
|
||||
switch tag[0:1] {
|
||||
case "/":
|
||||
st, err = a.styles(tag[1:])
|
||||
if err == nil {
|
||||
out.WriteString(st.Set(style.ActionUnset))
|
||||
|
||||
@@ -2,7 +2,7 @@ package label
|
||||
|
||||
type Key string
|
||||
|
||||
func (k Key) Any(v interface{}) KeyValue {
|
||||
func (k Key) Any(v any) KeyValue {
|
||||
return KeyValue{
|
||||
Key: k,
|
||||
Value: AnyValue(v),
|
||||
|
||||
@@ -30,7 +30,7 @@ func (k KeyValue) String() string {
|
||||
return string(k.Key) + "=\"" + k.Value.String() + "\""
|
||||
}
|
||||
|
||||
func Any(k string, v interface{}) KeyValue {
|
||||
func Any(k string, v any) KeyValue {
|
||||
return Key(k).Any(v)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ const (
|
||||
|
||||
type Value struct {
|
||||
vtype Type
|
||||
value interface{}
|
||||
value any
|
||||
}
|
||||
|
||||
func (v Value) String() string {
|
||||
return fmt.Sprint(v.value)
|
||||
}
|
||||
|
||||
func AnyValue(v interface{}) Value {
|
||||
func AnyValue(v any) Value {
|
||||
return Value{vtype: TypeAny, value: v}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ func writeError(_ int, err error) {
|
||||
|
||||
type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...label.KeyValue) (int, error)
|
||||
|
||||
func (o Output) Print(ctx context.Context, args ...interface{}) {
|
||||
func (o Output) Print(ctx context.Context, args ...any) {
|
||||
writeError(o(ctx, verbosity.Norm, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
@@ -26,15 +26,15 @@ func (o Output) PrintKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Norm, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Printf(ctx context.Context, format string, args ...interface{}) {
|
||||
func (o Output) Printf(ctx context.Context, format string, args ...any) {
|
||||
writeError(o(ctx, verbosity.Norm, fmt.Sprintf(format, args...)))
|
||||
}
|
||||
|
||||
func (o Output) Println(ctx context.Context, args ...interface{}) {
|
||||
func (o Output) Println(ctx context.Context, args ...any) {
|
||||
writeError(o(ctx, verbosity.Norm, fmt.Sprintln(args...)))
|
||||
}
|
||||
|
||||
func (o Output) Info(ctx context.Context, args ...interface{}) {
|
||||
func (o Output) Info(ctx context.Context, args ...any) {
|
||||
writeError(o(ctx, verbosity.Info, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (o Output) InfoKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Info, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Debug(ctx context.Context, args ...interface{}) {
|
||||
func (o Output) Debug(ctx context.Context, args ...any) {
|
||||
writeError(o(ctx, verbosity.Debug, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func (o Output) DebugKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Debug, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Trace(ctx context.Context, args ...interface{}) {
|
||||
func (o Output) Trace(ctx context.Context, args ...any) {
|
||||
writeError(o(ctx, verbosity.Trace, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,8 @@ func Register(name string, style Style) error {
|
||||
}
|
||||
|
||||
func MustRegister(name string, style Style) {
|
||||
if err := Register(name, style); err != nil {
|
||||
err := Register(name, style)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package verbosity
|
||||
|
||||
//go:generate stringer -type=Verbosity -linecomment
|
||||
//go:generate go tool stringer -type=Verbosity -linecomment
|
||||
|
||||
type Verbosity int
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ const _Verbosity_name = "quietnorminfodebugtrace"
|
||||
var _Verbosity_index = [...]uint8{0, 5, 9, 13, 18, 23}
|
||||
|
||||
func (i Verbosity) String() string {
|
||||
i -= -1
|
||||
if i < 0 || i >= Verbosity(len(_Verbosity_index)-1) {
|
||||
return "Verbosity(" + strconv.FormatInt(int64(i+-1), 10) + ")"
|
||||
idx := int(i) - -1
|
||||
if i < -1 || idx >= len(_Verbosity_index)-1 {
|
||||
return "Verbosity(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _Verbosity_name[_Verbosity_index[i]:_Verbosity_index[i+1]]
|
||||
return _Verbosity_name[_Verbosity_index[idx]:_Verbosity_index[idx+1]]
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func FormatString(_ verbosity.Verbosity, msg string, kv ...label.KeyValue) strin
|
||||
}
|
||||
|
||||
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...label.KeyValue) string) Output {
|
||||
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
|
||||
return func(_ context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
|
||||
out, err := fmt.Fprint(w, format(verb, msg, kv...))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("writer fprint:%w", err)
|
||||
|
||||
33
register.go
33
register.go
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -14,12 +13,6 @@ const (
|
||||
CommandList = "list"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("command not found")
|
||||
ErrCommandNil = errors.New("console: Register command is nil")
|
||||
ErrCommandDuplicate = errors.New("console: duplicate command")
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
commandsMu sync.RWMutex
|
||||
@@ -27,30 +20,10 @@ var (
|
||||
findCommand = regexp.MustCompile("([^:]+|)")
|
||||
)
|
||||
|
||||
type AlternativesError struct {
|
||||
alt []string
|
||||
err error
|
||||
}
|
||||
|
||||
func (e AlternativesError) Error() string {
|
||||
return fmt.Sprintf("%s, alternatives: [%s]", e.err, strings.Join(e.alt, ","))
|
||||
}
|
||||
|
||||
func (e AlternativesError) Is(err error) bool {
|
||||
return errors.Is(e.err, err)
|
||||
}
|
||||
|
||||
func (e AlternativesError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e AlternativesError) Alternatives() []string {
|
||||
return e.alt
|
||||
}
|
||||
|
||||
// MustRegister register command or panic if err.
|
||||
func MustRegister(cmd *Command) {
|
||||
if err := Register(cmd); err != nil {
|
||||
err := Register(cmd)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -134,7 +107,7 @@ func Find(name string) (*Command, error) {
|
||||
names[i] = findCommands[i].Name
|
||||
}
|
||||
|
||||
return nil, AlternativesError{alt: names, err: ErrNotFound}
|
||||
return nil, AlternativesError{Alt: names, Err: ErrNotFound}
|
||||
}
|
||||
|
||||
return nil, ErrNotFound
|
||||
|
||||
Reference in New Issue
Block a user