forked from andresterba/go-ygot-generator-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (55 loc) · 1.33 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
package main
import (
"fmt"
"os"
"text/template"
)
var generatorTemplate = `package {{.PackageName}}
// This file is a placeholder in order to ensure that Go does not
// find this directory to contain an empty package.
//go:generate {{.PathToGenerator}} -path={{.PathToModels}} {{range .GeneratorOptions}} -{{.Option}}={{.Value}} {{end}} {{range .Models}} {{.}} {{end}}
`
func main() {
providedArgs := os.Args
if len(providedArgs) != 3 {
showHelp()
}
inputPath := providedArgs[1]
outputPath := providedArgs[2]
err := generateYgotGeneratorFileFromInput(inputPath, outputPath)
if err != nil {
panic(err)
}
}
func showHelp() {
fmt.Println(`Usage:
go-yang-generator-generator <path-to-input-file> <path-to-output-file>`)
os.Exit(1)
}
func generateYgotGeneratorFileFromInput(inputPath string, outputPath string) error {
manifest, err := readConfiguration(inputPath)
if err != nil {
return err
}
err = executeAndWriteManifest(outputPath, manifest)
if err != nil {
return err
}
return nil
}
func executeAndWriteManifest(path string, manifest *Configuration) error {
t1 := template.New("template")
t1parsed, err := t1.Parse(generatorTemplate)
if err != nil {
panic(err)
}
f, err := os.Create(path)
if err != nil {
panic(err)
}
err = t1parsed.Execute(f, &manifest)
if err != nil {
panic(err)
}
return nil
}