-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm-util.go
More file actions
67 lines (60 loc) · 1.63 KB
/
sm-util.go
File metadata and controls
67 lines (60 loc) · 1.63 KB
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
package main
import (
"flag"
"fmt"
"errors"
"log"
"os"
"github.com/sm/util/json"
"github.com/sm/util/mustache"
)
var (
command string
mustacheData string
outputFile string
jsonPath string
templateFile string
jsonUri string
jsonString string
)
func init() {
flag.StringVar(&command, "c", "help", "Command to run, one of {mustache,json}")
flag.StringVar(&mustacheData, "data", "{data}", "string of key=value pairs separated by a :~")
flag.StringVar(&outputFile, "output", "{file}", "path to output file")
flag.StringVar(&jsonPath, "path", "{path}", "path of variable")
flag.StringVar(&templateFile, "template", "{file}", "path to template file")
flag.StringVar(&jsonUri, "uri", "{uri}", "json uri or path to file")
flag.StringVar(&jsonString, "json", "{}", "json string")
flag.Parse()
}
func errN(msg string) (err error){
log.Fatal(msg)
err = errors.New(msg)
return
}
func main() {
var err error
value := ""
if command == "mustache" {
if templateFile == "{file}" {
err = errN("ERROR: A template file location must be specified with -template {{path to template file}}")
}
if mustacheData == "{data}" {
err = errN("ERROR: -data must be given as a string of key=value pairs separated by a :~")
}
if outputFile == "{file}" {
err = errN("ERROR: -output filename with path must be given.")
}
value, err = mustache.Run(templateFile, mustacheData, outputFile)
} else if command == "json" {
value, err = json.Run(jsonUri, jsonPath)
} else {
err = errN("ERROR: Unknown Command or not specified: -c {mustache|json}")
}
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Println(value)
os.Exit(0)
}