-
Notifications
You must be signed in to change notification settings - Fork 40
/
generate_go.go
70 lines (61 loc) · 2.09 KB
/
generate_go.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
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the
// goconst linter's warning.
//
// nolint: goconst
package tfgen
import (
"github.com/pkg/errors"
gogen "github.com/pulumi/pulumi/pkg/codegen/go"
"github.com/pulumi/pulumi-terraform-bridge/pkg/tfbridge"
)
type goGenerator struct {
pkg string
version string
info tfbridge.ProviderInfo
overlaysDir string
outDir string
}
// newGoGenerator returns a language generator that understands how to produce Go packages.
func newGoGenerator(pkg, version string, info tfbridge.ProviderInfo, overlaysDir, outDir string) langGenerator {
return &goGenerator{
pkg: pkg,
version: version,
info: info,
overlaysDir: overlaysDir,
outDir: outDir,
}
}
// typeName returns a type name for a given resource type.
func (g *goGenerator) typeName(r *resourceType) string {
return r.name
}
// emitPackage emits an entire package pack into the configured output directory with the configured settings.
func (g *goGenerator) emitPackage(pack *pkg) error {
ppkg, err := genPulumiSchema(pack, g.pkg, g.version, g.info)
if err != nil {
return errors.Wrap(err, "generating Pulumi schema")
}
files, err := gogen.GeneratePackage(tfgen, ppkg)
if err != nil {
return errors.Wrap(err, "generating Pulumi package")
}
for f, contents := range files {
if err := emitFile(g.outDir, f, contents); err != nil {
return errors.Wrapf(err, "emitting file %v", f)
}
}
return nil
}