-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
90 lines (79 loc) · 1.75 KB
/
utils.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
package gowpcli
import (
"fmt"
"regexp"
"strings"
)
// todo special treatment
//"[--field=<field>]": "[--field=<field>]",
//"[--<field>=<value>]": "[--<field>=<value>]",
func formatKey(k string) string {
var r = []string{"[", "]", "..."}
for _, s := range r {
k = strings.Replace(k, s, "", -1)
}
return k
}
func formatMap(m map[string]string) []string {
var s []string
for k, v := range m {
s = append(s, fmt.Sprintf("%s=%s", k, v))
}
return s
}
// formatNamed
func formatNamed(k, v string) string {
var reg = regexp.MustCompile("(--.*?)\\=")
param := reg.Find([]byte(k))
return fmt.Sprintf("%s%s", param, v)
}
// isNamed checks if parameter is in form of `--name=`
func isNamed(s string) (t bool) {
if strings.Contains(s, "--") {
t = true
}
return
}
func mult(s string) bool {
return strings.HasSuffix(s, "...") || strings.HasSuffix(s, "...]")
}
func isRequired(s string) bool {
return strings.HasPrefix(s, "<") || strings.HasPrefix(s, "--") || strings.HasPrefix(s, "[<")
}
func opt(s string) bool {
return strings.HasPrefix(s, "[")
}
func MakeArg(args []string, k string, v interface{}) []string {
Logger.Debugf("INPUT:%v, %v, %v", args, k, v)
k = formatKey(k)
switch t := v.(type) {
case map[string]string:
Logger.Debug("map[string]string")
if len(t) != 0 {
args = append(args, formatMap(t)...)
}
case []string:
Logger.Debug("[]string")
if len(t) != 0 {
args = append(args, t...)
}
case bool:
Logger.Debug("bool")
if t {
args = append(args, k)
}
case string:
Logger.Debug("string")
if t != "" {
if isNamed(k) {
Logger.Debug("is named")
args = append(args, formatNamed(k, t))
} else {
Logger.Debug("is not named")
args = append(args, t)
}
}
}
Logger.Debugf("RETURN:%v", args)
return args
}