-
Notifications
You must be signed in to change notification settings - Fork 32
/
runner.go
123 lines (105 loc) · 2.8 KB
/
runner.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/jhunt/go-ansi"
)
const (
DestructiveCommand string = "@R"
NonDestructiveCommand = "@G"
AdministrativeCommand = "@W"
MiscellaneousCommand = "@W"
HiddenCommand = "HIDEME"
)
type Help struct {
Summary string
Usage string
Description string
Type string
}
type Handler func(command string, args ...string) error
type Runner struct {
Handlers map[string]Handler
Topics map[string]*Help
}
func NewRunner() *Runner {
return &Runner{
Handlers: make(map[string]Handler),
Topics: make(map[string]*Help),
}
}
func (r *Runner) Dispatch(command string, help *Help, fn Handler) {
if help != nil {
help.Description = strings.Trim(help.Description, "\n")
}
r.Handlers[command] = fn
if help != nil && help.Type != HiddenCommand {
r.Topics[command] = help
}
}
func (r *Runner) HelpTopic(topic string, help string) {
r.Topics[topic] = &Help{Description: strings.Trim(help, "\n")}
}
func (r *Runner) Help(out io.Writer, topic string) {
if topic == "commands" {
fmt.Fprintf(out, "Valid commands are:\n\n")
ll := make([]string, 0)
for cmd := range r.Handlers {
ll = append(ll, cmd)
}
sort.Strings(ll)
for _, cmd := range ll {
if h := r.Topics[cmd]; h != nil {
f := h.Type
if f == "" {
f = "@W"
}
ansi.Fprintf(out, " "+f+"{%-10s} %s\n", cmd, h.Summary)
}
}
fmt.Fprintf(out, "\nTry `safe envvars' for information on available environment variables\n")
fmt.Fprintf(out, "Try 'safe help <command>' for detailed information on specific commands\n")
return
}
if help, ok := r.Topics[topic]; ok && help != nil {
if help.Summary != "" {
/* this is a command, print it like one */
ansi.Fprintf(out, "safe @G{%s} - @C{%s}\n", topic, help.Summary)
if help.Usage != "" {
ansi.Fprintf(out, "USAGE: "+help.Usage+"\n")
}
if help.Description != "" {
ansi.Fprintf(out, "\n")
}
}
if help.Description != "" {
ansi.Fprintf(out, help.Description+"\n")
}
return
}
ansi.Fprintf(out, "@R{Unrecognized command or help topic '%s'}\n", topic)
fmt.Fprintf(out, "Try 'safe help' to get started with safe,\n")
fmt.Fprintf(out, " or 'safe commands' for a list of valid commands\n")
os.Exit(1)
}
func (r *Runner) ExitWithUsage(topic string) {
if help, ok := r.Topics[topic]; ok && help != nil {
if help.Summary != "" {
/* this is a command, print it like one */
ansi.Fprintf(os.Stderr, "safe @G{%s} - @C{%s}\n", topic, help.Summary)
if help.Usage != "" {
ansi.Fprintf(os.Stderr, "USAGE: "+help.Usage+"\n")
}
}
}
os.Exit(1)
}
func (r *Runner) Execute(command string, args ...string) error {
if fn, ok := r.Handlers[command]; ok {
return fn(command, args...)
}
return fmt.Errorf("unknown command '%s'", command)
}