add chain input
This commit is contained in:
7
app.go
7
app.go
@@ -131,10 +131,9 @@ func (a *App) list(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
in := &input.Wrap{
|
||||
Input: a.in,
|
||||
}
|
||||
in.SetArgument("command_name", value.New(CommandList))
|
||||
arr := &input.Array{}
|
||||
arr.SetArgument("command_name", value.New(CommandList))
|
||||
in := input.Chain(a.in, arr)
|
||||
|
||||
return Run(ctx, cmd, in, a.out)
|
||||
}
|
||||
|
||||
12
console.go
12
console.go
@@ -3,6 +3,7 @@ package console
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input"
|
||||
@@ -104,12 +105,9 @@ func verbose(ctx context.Context, in input.Input, out output.Output) output.Outp
|
||||
}
|
||||
|
||||
func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Output) error {
|
||||
w := &input.Wrap{
|
||||
Input: in,
|
||||
}
|
||||
|
||||
w.SetArgument(HelpArgumentCommandName, value.New(cmd.Name))
|
||||
w.SetOption("help", value.New(false))
|
||||
a := &input.Array{}
|
||||
a.SetArgument(HelpArgumentCommandName, value.New(cmd.Name))
|
||||
a.SetOption("help", value.New(false))
|
||||
|
||||
if _, err := Find(cmd.Name); errors.Is(err, ErrNotFound) {
|
||||
register(cmd)
|
||||
@@ -119,6 +117,8 @@ func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Outp
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println(a)
|
||||
w := input.Chain(a, in)
|
||||
|
||||
return Run(ctx, help, w, out)
|
||||
}
|
||||
|
||||
46
input/chain.go
Normal file
46
input/chain.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"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 {
|
||||
log.Println(name, len(c))
|
||||
for _, in := range c {
|
||||
log.Printf("%T\n", in)
|
||||
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 err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -10,7 +10,7 @@ var (
|
||||
)
|
||||
|
||||
func IsEmpty(v Value) bool {
|
||||
return v == Empty
|
||||
return v == nil || v == Empty
|
||||
}
|
||||
|
||||
type empty struct{}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Wrap struct {
|
||||
Input
|
||||
Array
|
||||
}
|
||||
|
||||
func (w *Wrap) Option(ctx context.Context, name string) value.Value {
|
||||
if v, ok := w.Array.GetOption(name); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
return w.Input.Option(ctx, name)
|
||||
}
|
||||
|
||||
func (w *Wrap) Argument(ctx context.Context, name string) value.Value {
|
||||
if v, ok := w.Array.GetArgument(name); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
return w.Input.Argument(ctx, name)
|
||||
}
|
||||
|
||||
func (w *Wrap) Bind(ctx context.Context, def *Definition) error {
|
||||
if err := w.Input.Bind(ctx, def); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return w.Array.Bind(ctx, def)
|
||||
}
|
||||
Reference in New Issue
Block a user