/
main.go
112 lines (101 loc) · 2.13 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
108
109
110
111
112
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/writescript/writescript"
"os"
"io/ioutil"
)
// main cli tool
func main() {
//
// meta
//
app := cli.NewApp()
app.Name = "writescript"
app.Version = writescript.Version
app.Usage = "plugin based generator tool backpacked with data"
app.Author = "Paul Vollmer"
app.Email = "paul.vollmer@fh-potsdam.de"
//
// flags
//
app.Flags = []cli.Flag{
// input
cli.StringFlag{
Name: "plugin, p",
Value: "",
Usage: "the generator plugin as file",
},
cli.StringFlag{
Name: "data, d",
Value: "",
Usage: "the data formatted as json or yaml, as file or string",
},
// output
cli.StringFlag{
Name: "output, o",
Value: "",
Usage: "the output filepath",
},
// settings
cli.StringFlag{
Name: "linebreak, l",
Value: "\\n",
Usage: "the linebreak for each row",
},
cli.StringFlag{
Name: "whitespace, w",
Value: "\\t",
Usage: "the level whitespace",
},
cli.BoolFlag{
Name: "header-off, H",
Usage: "disables header output",
},
}
//
// process
//
app.Action = func(c *cli.Context) {
//
// cli flags
//
flagPlugin := c.String("plugin")
flagData := c.String("data")
flagOutput := c.String("output")
flagLinebreak := c.String("linebreak")
flagWhitespace := c.String("whitespace")
flagHeaderOff := c.Bool("header-off")
// read plugin
pluginBytes, err := writescript.LoadPlugin(flagPlugin)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// read data
data := Data{}
data.Init(flagData)
// run the generator
ws := writescript.WriteScript{}
err = ws.Process(string(pluginBytes), data.JSON, !flagHeaderOff)
if err != nil {
fmt.Println("writescript plugin error!\n", err)
os.Exit(1)
}
if flagLinebreak == "\\n" {
flagLinebreak = "\n"
}
if flagWhitespace == "\\t" {
flagWhitespace = "\t"
}
// write output
if flagOutput == "" {
fmt.Println(ws.Content.GetString(flagLinebreak, flagWhitespace))
} else {
fileBytes := []byte(ws.Content.GetString(flagLinebreak, flagWhitespace))
ioutil.WriteFile(flagOutput, fileBytes, 0644)
}
}
app.Run(os.Args)
}