/
main.go
49 lines (44 loc) · 1.17 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
)
func getOutput(o string) (io.Writer, error) {
if o == "stdout" {
return os.Stdout, nil
}
f, err := os.Create(o)
if err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
return f, nil
}
func main() {
// setup and parse command line arguments
schemaFile := flag.String("schema", "config_schema.json", "configuration schema file")
outputDst := flag.String("output", "stdout", "output file name")
packageName := flag.String("package", "main", "package name for generated files")
addHeaderFooter := flag.Bool("headers", true, "add header to generated files")
flag.Parse()
// get output destination
output, err := getOutput(*outputDst)
if err != nil {
log.Fatalf("error getting output destination: %s", err)
}
// prepare templates
tmpls, err := prepareTemplates()
if err != nil {
log.Fatalf("error preparing templates: %s", err)
}
// parse schema
schema, err := parseSchema(*schemaFile)
if err != nil {
log.Fatalf("error parsing schema: %s", err)
}
// generate code
formatted, err := genFromSchema(tmpls, schema, *addHeaderFooter, *packageName)
fmt.Fprint(output, string(formatted))
}