-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
118 lines (98 loc) · 2.79 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
113
114
115
116
117
package main
import (
"encoding/json"
"github.com/ghodss/yaml"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/Masterminds/sprig"
"text/template"
"bytes"
"unicode"
)
type MultipleFileData struct {
Files []struct {
FileName string
Data interface{}
}
}
func main() {
dataFileName := flag.String("d", "", "json or yaml data file")
templateFileName := flag.String("t", "", "go template file")
outputDirectory := flag.String("o", ".", "output directory")
multipleFiles := flag.String("m", "", "-m multi : generates one file for each File object in Json (or Yaml) data file")
flag.Parse()
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Printf(" gocodegen -d data-file (Json or Yaml) -t template-file [-o output-directory] [-m multiple-files] \n")
flag.PrintDefaults()
os.Exit(1)
}
if *dataFileName == "" || *templateFileName == "" {
flag.Usage()
}
hasMultipleFiles := (*multipleFiles != "") && (*multipleFiles == "multi")
template, err := template.New(path.Base(*templateFileName)).Funcs(template.FuncMap(MyFuncMap)).Funcs(sprig.TxtFuncMap()).ParseFiles(*templateFileName)
if err != nil {
fmt.Println(err)
}
dataFile, err := os.Open(*dataFileName)
if err != nil {
fmt.Println(err)
}
defer dataFile.Close()
byteValue, err := ioutil.ReadAll(dataFile)
if err != nil {
fmt.Println(err)
}
byteValue,err = ToJSON(byteValue)
if hasMultipleFiles {
var multidata MultipleFileData
err = json.Unmarshal(byteValue, &multidata)
if err != nil {
fmt.Println(err)
}
for _, mfile := range multidata.Files {
outputFileName := path.Join(path.Dir(*dataFileName), mfile.FileName)
mdata := mfile.Data
generateFile(template, *outputDirectory, outputFileName, mdata)
}
} else {
var data interface{}
err = json.Unmarshal(byteValue, &data)
if err != nil {
fmt.Println(err)
}
outputFileName := strings.TrimSuffix(*dataFileName, filepath.Ext(*dataFileName)) + ".generated.txt"
generateFile(template, *outputDirectory, outputFileName, data)
}
}
func generateFile(template *template.Template, outputDirectory string, outputFileName string, data interface{}) {
absOutputFileName := path.Join(outputDirectory, outputFileName)
os.MkdirAll(path.Dir(absOutputFileName), os.ModePerm)
outputFile, err := os.Create(absOutputFileName)
fmt.Println("Generating file : " + absOutputFileName )
defer outputFile.Close()
if err != nil {
fmt.Println(err)
}
err = template.Execute(outputFile, data)
if err != nil {
fmt.Println(err)
}
}
func ToJSON(data []byte) ([]byte, error) {
if hasJSONPrefix(data) {
return data, nil
}
return yaml.YAMLToJSON(data)
}
var jsonPrefix = []byte("{")
func hasJSONPrefix(buf []byte) bool {
trim := bytes.TrimLeftFunc(buf, unicode.IsSpace)
return bytes.HasPrefix(trim, jsonPrefix)
}