-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 2.44 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/yosida95/uritemplate"
"gopkg.in/alecthomas/kingpin.v2"
)
type kingpinArgCallable interface {
Arg(name, help string) *kingpin.ArgClause
}
func templateArg(c kingpinArgCallable) *string {
return c.Arg("template", "A URI template.").Required().String()
}
var (
app = kingpin.New("uritemplate", "A command-line RFC6570 (URI Template) expander.")
newline = app.Flag("newline", "Print newline at the end of output (use --no-newline to ommit newlines).").Default("true").Bool()
expand = app.Command("expand", "Print the expanded URI template.")
expandTemplate = templateArg(expand)
expandStrings = expand.Flag("var", "Variables to substitute as key-value pairs.").PlaceHolder("KEY=VAL").Short('v').StringMap()
expandJSONs = JSONValues(expand.Flag("json", "Variables to substitute as JSON.").Short('j'))
varnames = app.Command("varnames", "Print variable names found in the template.")
varnamesTemplate = templateArg(varnames)
varnamesFormat = varnames.Flag("format", "Format to use.").Default("list").Short('f').Enum("list", "json")
regexp = app.Command("regexp", "Print a regexp that matches the template.")
regexpTemplate = templateArg(regexp)
)
func print(s ...interface{}) {
if *newline {
fmt.Println(s...)
} else {
fmt.Print(s...)
}
}
func addStrings(values uritemplate.Values) error {
for key, val := range *expandStrings {
if values.Get(key) != nil {
return fmt.Errorf("variable '%v' specified twice", key)
}
values.Set(key, uritemplate.String(val))
}
return nil
}
func addJSONs(values uritemplate.Values) error {
for key, val := range *expandJSONs {
if values.Get(key) != nil {
return fmt.Errorf("variable '%v' specified twice", key)
}
values.Set(key, val)
}
return nil
}
func main() {
command := kingpin.MustParse(app.Parse(os.Args[1:]))
switch command {
case expand.FullCommand():
template, err := uritemplate.New(*expandTemplate)
app.FatalIfError(err, "")
values := make(uritemplate.Values)
app.FatalIfError(addStrings(values), "")
app.FatalIfError(addJSONs(values), "")
uri, err := template.Expand(values)
app.FatalIfError(err, "")
print(uri)
case varnames.FullCommand():
template, err := uritemplate.New(*varnamesTemplate)
app.FatalIfError(err, "")
printVarnames(template.Varnames())
case regexp.FullCommand():
template, err := uritemplate.New(*regexpTemplate)
app.FatalIfError(err, "")
print(template.Regexp())
}
}