/
cli.go
93 lines (80 loc) · 2.17 KB
/
cli.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
package main
import (
"fmt"
"github.com/Jeffail/gabs"
"github.com/hoisie/mustache"
"os"
"regexp"
"strings"
)
// Run executes the command.
func Run(args []string, jsonString string, templateFileName string, templateData string) {
jsonContainer := ParseArgs(args, jsonString)
template := createTemplate(templateFileName, templateData)
output := template.Render(jsonContainer.Data())
if templateFileName == "" {
fmt.Println(output)
} else {
fmt.Print(output)
}
}
// ParseArgs parses arguments to JSON data
func ParseArgs(args []string, jsonString string) *gabs.Container {
jsonContainer := createJSONContainer(jsonString)
argPattern := regexp.MustCompile("([^=]+)=([^=]+)")
for _, arg := range args {
group := argPattern.FindStringSubmatch(arg)
if len(group) != 3 {
continue
}
values := strings.Split(strings.Trim(group[2], ","), ",")
if len(values) == 1 {
jsonContainer.SetP(group[2], group[1])
continue
}
arrayItems := make([]map[string]interface{}, len(values))
for index, value := range values {
arrayItems[index] = map[string]interface{}{
"index": index,
"first": index == 0,
"last": index == len(values)-1,
"value": value,
}
}
jsonContainer.SetP(arrayItems, group[1])
}
return jsonContainer
}
// Fatalf writes to stderr and exits.
func Fatalf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "\n %s\n\n", fmt.Sprintf(msg, args...))
os.Exit(1)
}
func createTemplate(templateFileName string, templateData string) *mustache.Template {
if templateFileName != "" {
template, err := mustache.ParseFile(templateFileName)
if err != nil {
Fatalf("Failed to load a template file: ", err)
}
return template
} else if templateData != "" {
template, err := mustache.ParseString(templateData)
if err != nil {
Fatalf("Failed to parse a template data: ", err)
}
return template
} else {
Fatalf("No template file or template data")
return nil
}
}
func createJSONContainer(jsonString string) *gabs.Container {
if jsonString == "" {
return gabs.New()
}
container, err := gabs.ParseJSON([]byte(jsonString))
if err != nil {
Fatalf("Failed to parse piped JSON string: %s", err)
}
return container
}