/
main.go
107 lines (93 loc) · 2.42 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bufio"
"io/ioutil"
"os"
"text/template"
"github.com/badugisoft/go-template/funcs"
"github.com/badugisoft/xson"
cli "gopkg.in/urfave/cli.v2"
)
func main() {
app := cli.App{
Name: "go-template",
Usage: "go template commandline tool",
Version: "0.0.1",
Action: run,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "input data file path\n\tif this is missing, stdin is used",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Value: "autodetect",
Usage: "input data file or stdin data type.\n\tif path is missing, default type is yaml\n\tsupported types : yaml, xml, json\n\t",
},
&cli.StringFlag{
Name: "main",
Aliases: []string{"m"},
Value: "main",
Usage: "main template name\n\t",
},
},
ArgsUsage: "[template files]",
}
app.Run(os.Args)
}
func run(c *cli.Context) error {
dataPath := c.String("path")
dataType := c.String("type")
var realType xson.Type
if dataType == "autodetect" {
if dataPath == "" {
realType = xson.YAML
} else {
realType = xson.GetType(dataPath)
if realType == xson.UNKNOWN {
return cli.Exit("unsupported data type : "+dataType, -1)
}
}
} else {
realType = xson.GetType(dataType)
if realType == xson.UNKNOWN {
return cli.Exit("unsupported data type : "+dataType, -1)
}
}
if c.Args().Len() == 0 {
return cli.Exit("at least one template file is required", -2)
}
var fileData []byte
var err error
if dataPath != "" {
fileData, err = ioutil.ReadFile(dataPath)
if err != nil {
return cli.Exit("read data file failed : "+dataType+", "+err.Error(), -3)
}
} else {
fileData, err = ioutil.ReadAll(bufio.NewReader(os.Stdin))
if err != nil {
return cli.Exit("read data from stdin : "+dataType+", "+err.Error(), -3)
}
}
data := map[string]interface{}{}
err = xson.Unmarshal(realType, fileData, &data)
if err != nil {
return cli.Exit("read data failed : "+err.Error(), -4)
}
t, err := template.New("").Funcs(funcs.GetMap).ParseFiles(c.Args().Slice()...)
if err != nil {
return cli.Exit("parsing template failed : "+err.Error(), -6)
}
if len(t.Templates()) == 1 {
err = t.ExecuteTemplate(os.Stdout, t.Templates()[0].Name(), data)
} else {
err = t.ExecuteTemplate(os.Stdout, c.String("main"), data)
}
if err != nil {
return cli.Exit("executing template failed : "+err.Error(), -7)
}
return nil
}