4 Commits

Author SHA1 Message Date
ad5cf18535 Merge pull request 'check empty execute' (#6) from execute into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #6
2022-09-24 12:20:33 +03:00
andrey1s
1151e7c3ad check empty execute
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2022-09-24 12:16:23 +03:00
44d8837dbc Merge pull request 'add example hidden option' (#5) from example into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #5
2022-09-18 23:14:22 +03:00
andrey1s
0b6a6ee99b add example hidden option
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2022-09-18 23:11:24 +03:00
6 changed files with 59 additions and 30 deletions

View File

@@ -100,6 +100,10 @@ func (c *Command) With(opts ...Option) *Command {
// Run run command with input and output. // 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 input.Input, out output.Output) error {
if c.Execute == nil {
return fmt.Errorf("%w", ErrExecuteNil)
}
if c.Handle != nil { if c.Handle != nil {
return c.Handle(ctx, in, out, c.Execute) return c.Handle(ctx, in, out, c.Execute)
} }

View File

@@ -2,6 +2,7 @@ package console_test
import ( import (
"context" "context"
"errors"
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
@@ -125,3 +126,21 @@ func TestChainHandle(t *testing.T) {
} }
} }
} }
func TestRunEmptyExecute(t *testing.T) {
t.Parallel()
ctx := context.Background()
empty := console.Command{
Name: "empty",
}
in := &input.Array{
Map: input.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)
}
}

31
error.go Normal file
View 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
}

View File

@@ -16,7 +16,8 @@ const (
AppName = "console" AppName = "console"
) )
// FDEVS_CONSOLE_CAT=env go run cmd/config/main.go fdevs:console:arg -b tmp. // 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() { func main() {
env := config.New(Namespace, AppName, []config.Provider{ env := config.New(Namespace, AppName, []config.Provider{
env.New(), env.New(),

View File

@@ -20,6 +20,7 @@ func Args() *console.Command {
option.String("bar", "required bar option", option.Required, option.Short('b')), option.String("bar", "required bar option", option.Required, option.Short('b')),
option.String("cat", "cat option", option.Short('c')), option.String("cat", "cat option", option.Short('c')),
option.Time("time", "time example"), option.Time("time", "time example"),
option.Time("hidden", "hidden time example", option.Hidden),
) )
return nil return nil
@@ -29,6 +30,7 @@ func Args() *console.Command {
out.Println(ctx, "bar: <info>", in.Option(ctx, "bar").String(), "</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, "cat: <info>", in.Option(ctx, "cat").String(), "</info>")
out.Println(ctx, "time: <info>", in.Option(ctx, "time").Time().Format(time.RFC3339), "</info>") out.Println(ctx, "time: <info>", in.Option(ctx, "time").Time().Format(time.RFC3339), "</info>")
out.Println(ctx, "hidden: <info>", in.Option(ctx, "hidden").Time().Format(time.RFC3339), "</info>")
return nil return nil
}, },

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"sort" "sort"
"strings"
"sync" "sync"
) )
@@ -14,12 +13,6 @@ const (
CommandList = "list" 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 //nolint:gochecknoglobals
var ( var (
commandsMu sync.RWMutex commandsMu sync.RWMutex
@@ -27,27 +20,6 @@ var (
findCommand = regexp.MustCompile("([^:]+|)") 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. // MustRegister register command or panic if err.
func MustRegister(cmd *Command) { func MustRegister(cmd *Command) {
if err := Register(cmd); err != nil { if err := Register(cmd); err != nil {
@@ -134,7 +106,7 @@ func Find(name string) (*Command, error) {
names[i] = findCommands[i].Name names[i] = findCommands[i].Name
} }
return nil, AlternativesError{alt: names, err: ErrNotFound} return nil, AlternativesError{Alt: names, Err: ErrNotFound}
} }
return nil, ErrNotFound return nil, ErrNotFound