-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
119 lines (108 loc) · 3.3 KB
/
router.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
package clif
import (
"context"
"fmt"
"strings"
)
type commandPathEntry struct {
cmd Command
name string
}
type inputRouter struct {
cmd Command
flags map[string][]*string
args []string
path []commandPathEntry
parser *inputParser
}
func (router *inputRouter) route(ctx context.Context) error {
for pos, token := range router.parser.tokens {
switch {
case token.isType(tokenTypeCommand):
sub, err := router.getSubcommand(ctx, router.path, token.value)
if err != nil {
return err
}
router.cmd = sub
router.path = append(router.path, commandPathEntry{
cmd: sub,
name: token.value,
})
case token.isType(tokenTypeFlagKey):
// if there's a next token *and* the next token is a
// flag value, it's this flag's value
if pos < len(router.parser.tokens)-1 && router.parser.tokens[pos+1].isType(tokenTypeFlagValue) {
router.flags[token.value] = append(router.flags[token.value], &router.parser.tokens[pos+1].value)
continue
}
// if there's not a next token or it's not a flag
// value, record a nil to track that the flag was used
// without a value
router.flags[token.value] = append(router.flags[token.value], nil)
case token.isType(tokenTypeArgument):
router.args = append(router.args, token.value)
case token.isType(tokenTypeUnknown):
mightBe := []tokenType{}
for k := range token.mightBe {
mightBe = append(mightBe, k)
}
return UnknownTokenTypeError{
value: token.value,
mightBe: mightBe,
}
case token.isType(tokenTypeFlagValue):
// ignore the flag values, we've already taken care of
// them when we saw the flag keys
continue
default:
return UnexpectedTokenTypeError{
tokenType: token.tokenType,
value: token.value,
}
}
}
return nil
}
func (router *inputRouter) getSubcommand(_ context.Context, path []commandPathEntry, command string) (Command, error) {
for _, sub := range router.cmd.Subcommands {
if sub.Name == command {
return sub, nil
}
for _, alias := range sub.Aliases {
if alias == command {
return sub, nil
}
}
}
stringPath := make([]string, 0, len(path))
for _, entry := range path {
stringPath = append(stringPath, entry.name)
}
return Command{}, UnknownCommandError{Path: append(stringPath, command)}
}
// UnexpectedTokenTypeError is returned when the router encounters a token type
// it doesn't know how to handle. This usually indicates a bug in clif.
type UnexpectedTokenTypeError struct {
// tokenType is the unhandled tokenType.
tokenType tokenType
// value is the value that got classified as an unhandled tokenType.
value string
}
func (err UnexpectedTokenTypeError) Error() string {
return fmt.Sprintf("unexpected token type %s for %s", err.tokenType, err.value)
}
// UnknownTokenTypeError is returned when the router encounters a token and
// isn't sure what type it is. This usually indicates a bug in clif.
type UnknownTokenTypeError struct {
// mightBe are the different tokenTypes a token could be.
mightBe []tokenType
// value is the value that could be multiple token types.
value string
}
func (err UnknownTokenTypeError) Error() string {
mightBe := []string{}
for _, k := range err.mightBe {
mightBe = append(mightBe, k.String())
}
return fmt.Sprintf("unknown token type for %q, could be: %s", err.value, strings.Join(mightBe, ", "))
}