From 50df7ec00093c6c1233050d2f66b1ebebd95d294 Mon Sep 17 00:00:00 2001 From: andrey1s Date: Sun, 1 Nov 2020 12:52:35 +0300 Subject: [PATCH] add chain input --- app.go | 7 +++---- console.go | 12 ++++++------ input/chain.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ input/value/empty.go | 2 +- input/wrap.go | 36 ---------------------------------- 5 files changed, 56 insertions(+), 47 deletions(-) create mode 100644 input/chain.go delete mode 100644 input/wrap.go diff --git a/app.go b/app.go index ef54e85..dc34b63 100644 --- a/app.go +++ b/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) } diff --git a/console.go b/console.go index aef1831..dd869f1 100644 --- a/console.go +++ b/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) } diff --git a/input/chain.go b/input/chain.go new file mode 100644 index 0000000..19fb504 --- /dev/null +++ b/input/chain.go @@ -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 +} diff --git a/input/value/empty.go b/input/value/empty.go index d4f09ca..b6735c9 100644 --- a/input/value/empty.go +++ b/input/value/empty.go @@ -10,7 +10,7 @@ var ( ) func IsEmpty(v Value) bool { - return v == Empty + return v == nil || v == Empty } type empty struct{} diff --git a/input/wrap.go b/input/wrap.go deleted file mode 100644 index 1b2302d..0000000 --- a/input/wrap.go +++ /dev/null @@ -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) -}