forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clihelper.go
51 lines (41 loc) · 914 Bytes
/
clihelper.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
package action
import (
"strings"
"github.com/urfave/cli/v2"
)
type argList []string
func (a argList) Get(n int) string {
if len(a) > n {
return a[n]
}
return ""
}
func parseArgs(c *cli.Context) (argList, map[string]string) {
args := make(argList, 0, c.Args().Len())
kvps := make(map[string]string, c.Args().Len())
if c.Args().Len() == 1 {
// If there is only one arg, assume it is
// the secret name, so don't attempt to
// parse into args and kvps
args = append(args, c.Args().Get(0))
return args, kvps
}
OUTER:
for _, arg := range c.Args().Slice() {
for _, sep := range []string{":", "="} {
if !strings.Contains(arg, sep) {
continue
}
p := strings.Split(arg, sep)
if len(p) < 2 {
args = append(args, arg)
continue OUTER
}
key := p[0]
kvps[key] = strings.Join(p[1:], ":")
continue OUTER
}
args = append(args, arg)
}
return args, kvps
}