-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
63 lines (53 loc) · 1.74 KB
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package commander
import (
"strings"
"github.com/leep-frog/command/command"
)
// EchoExecuteDataProcessor is a `command.Processor` that outputs the current command.ExecuteData contents.
type EchoExecuteDataProcessor struct {
// Stderr is whether or not the output should be written to Stderr instead.
Stderr bool
// Format
Format string
}
func (e *EchoExecuteDataProcessor) Execute(_ *command.Input, o command.Output, _ *command.Data, ed *command.ExecuteData) error {
if e.Format != "" && len(ed.Executable) > 0 {
if e.Stderr {
o.Stderrf(e.Format, strings.Join(ed.Executable, "\n"))
} else {
o.Stdoutf(e.Format, strings.Join(ed.Executable, "\n"))
}
return nil
}
for _, s := range ed.Executable {
if e.Stderr {
o.Stderrln(s)
} else {
o.Stdoutln(s)
}
}
return nil
}
func (e *EchoExecuteDataProcessor) Complete(*command.Input, *command.Data) (*command.Completion, error) {
return nil, nil
}
func (e *EchoExecuteDataProcessor) Usage(i *command.Input, d *command.Data, u *command.Usage) error {
return nil
}
// EchoExecuteData returns a `command.Processor` that sends the `command.ExecuteData` contents
// to stdout.
func EchoExecuteData() *EchoExecuteDataProcessor {
return &EchoExecuteDataProcessor{}
}
// EchoExecuteDataf returns a `command.Processor` that sends the `command.ExecuteData` contents
// to stdout with the provided format
func EchoExecuteDataf(format string) command.Processor {
return &EchoExecuteDataProcessor{Format: format}
}
// PrintlnProcessor returns a `command.Processor` that runs `output.Stdoutln(v)`.
func PrintlnProcessor(v string) command.Processor {
return SimpleProcessor(func(i *command.Input, o command.Output, d *command.Data, ed *command.ExecuteData) error {
o.Stdoutln(v)
return nil
}, nil)
}