This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
124 lines (99 loc) · 2.53 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
118
119
120
121
122
123
124
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/gravitational/trace"
"github.com/nategadzhi/protoc-gen-tfschema/builder"
"github.com/nategadzhi/protoc-gen-tfschema/config"
"github.com/nategadzhi/protoc-gen-tfschema/reducer"
"github.com/nategadzhi/protoc-gen-tfschema/renderer"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
var (
request = pluginpb.CodeGeneratorRequest{}
plugin *protogen.Plugin
)
const (
generatedFileSuffix = ".tfschema.go"
)
func init() {
initRequest()
initPlugin()
}
func main() {
log.Info("Generating schema files...")
generate()
emitResponse()
}
// Parses and initializes CodeGeneratorRequest
func initRequest() {
log.Info("Reading CodeGeneratorRequest from stdin.")
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fatal(err)
}
proto.Unmarshal(input, &request)
log.Infof("Command-line arguments: %s", request.GetParameter())
}
// Parses command line options and initializes Plugin instance
func initPlugin() {
var err error
opts := &protogen.Options{
ParamFunc: config.Set, // Built-in way of using go flag package to parse CLI args
}
plugin, err = opts.New(&request)
if err != nil {
fatal(err)
}
// Save ProtocVersion
if v := plugin.Request.GetCompilerVersion(); v != nil {
config.ProtocVersion = fmt.Sprintf("v%v.%v.%v", v.GetMajor(), v.GetMinor(), v.GetPatch())
}
config.Finalize()
}
// Parses input files and writes parsed files back to plugin
func generate() {
var numFilesWritten = 0
for _, file := range plugin.Files {
if !file.Generate {
continue
}
// Target file name
filename := file.GeneratedFilenamePrefix + generatedFileSuffix
out := plugin.NewGeneratedFile(filename, ".")
// Build resource tree
resources := builder.BuildResourceMapFromFile(file)
reducer.Reduce(resources)
// Render final template
result, err := renderer.Render(resources, Version)
if err != nil {
log.Errorf("Error rendering template: %v", err)
plugin.Error(err)
}
_, err = out.Write(result.Bytes())
if err != nil {
log.Errorf("Error generating schemas: %v", err)
plugin.Error(err)
}
log.Infof("%s prepared", filename)
numFilesWritten++
}
log.Infof("%v files generated", numFilesWritten)
}
func emitResponse() {
buf, err := proto.Marshal(plugin.Response())
if err != nil {
fatal(err)
}
if _, err := os.Stdout.Write(buf); err != nil {
fatal(err)
}
}
// Sugar
func fatal(err error) {
log.Fatal(trace.Wrap(err).Error())
}