/
main.go
78 lines (68 loc) · 1.77 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
// This generates structs and conversion functions
// so that you can use a "typed" terraform provider.
//
// Usage:
//
// go run . foo.Bar github.com/some/source/foo
// go run . Bar
//
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"github.com/pkg/errors"
)
const tfSchema = "github.com/stanistan/tf-schema"
func main() {
packageName := os.Getenv("GOPACKAGE")
if packageName == "" {
die(errors.New("missing package name in GOPACKAGE"))
}
vars, err := makeVars(os.Args[1:])
if err != nil {
die(errors.Wrapf(err, "could not make vars from args %q", os.Args[1:]))
}
filename := fmt.Sprintf("generated_%s_schema.go", vars.NS)
f, err := os.Create(filename)
die(err)
defer f.Close()
var buf bytes.Buffer
die(packageTemplate.Execute(&buf, struct {
*packageVars
Self string
Args []string
Package string
TypeDefs map[string]*templateTypeDef
Signatures map[string]signatureType
}{
packageVars: vars,
Self: tfSchema,
Args: os.Args[1:],
Package: os.Getenv("GOPACKAGE"),
TypeDefs: map[string]*templateTypeDef{
"Configure": {"Configure", typedReturnSignature},
"Create": {"Create", singleReturnSignature},
"Read": {"Read", singleReturnSignature},
"Delete": {"Delete", singleReturnSignature},
"Update": {"Update", singleReturnSignature},
"Exists": {"Exists", boolReturnSignature},
},
Signatures: map[string]signatureType{
"singleReturn": singleReturnSignature,
"typedReturn": typedReturnSignature,
"boolReturn": boolReturnSignature,
},
}))
p, err := format.Source(buf.Bytes())
die(errors.Wrapf(err, "could not format source"))
_, err = f.Write(p)
die(errors.Wrapf(err, "could not write the file"))
}
func die(err error) {
if err != nil {
log.Fatal(err)
}
}