-
Notifications
You must be signed in to change notification settings - Fork 66
/
path.go
41 lines (35 loc) · 893 Bytes
/
path.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
package charm
import (
"fmt"
"strings"
)
type path []*instance
func (p path) run(args []string) error {
err := p.last().command.Run(args)
if err == ErrNoRun {
if len(args) == 0 {
return fmt.Errorf("%q: requires a sub-command: %s", p.pathname(), p.subCommands())
} else {
return fmt.Errorf("%q: no such sub-command %q: options are: %s", p.pathname(), args[0], p.subCommands())
}
}
return err
}
func (p path) last() *instance {
return p[len(p)-1]
}
func (p path) pathname(args ...string) string {
names := make([]string, 0, len(p)+len(args))
for _, sub := range p {
names = append(names, sub.spec.Name)
}
names = append(names, args...)
return strings.Join(names, " ")
}
func (p path) subCommands() string {
names := make([]string, 0, len(p))
for _, spec := range p.last().spec.children {
names = append(names, spec.Name)
}
return strings.Join(names, " ")
}