Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix: excluding files from unneededGoFiles was not working
  • Loading branch information
shamil committed Nov 9, 2023
commit 990c77ab699c164b97021301742e3c87c942db7e
33 changes: 31 additions & 2 deletions pkg/codegen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ package codegen
import (
"bytes"
"fmt"
"path"
"path/filepath"
"strings"

ijson "github.com/pulumi/crd2pulumi/internal/json"
"github.com/pulumi/pulumi/pkg/v3/codegen"
goGen "github.com/pulumi/pulumi/pkg/v3/codegen/go"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)

var unneededGoFiles = codegen.NewStringSet(
Expand Down Expand Up @@ -63,21 +66,47 @@ func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Bu
pkg.Language[langName] = jsonData

files, err := goGen.GeneratePackage("crd2pulumi", pkg)

if err != nil {
return nil, fmt.Errorf("could not generate Go package: %w", err)
}

packageRoot, err := getPackageRoot(pkg.Reference())
if err != nil {
return nil, fmt.Errorf("could not get package root: %w", err)
}

pkg.Name = oldName
delete(pkg.Language, langName)

buffers = map[string]*bytes.Buffer{}
for path, code := range files {
newPath, _ := filepath.Rel(name, path)
if !unneededGoFiles.Has(newPath) {
pkgRelPath := strings.TrimPrefix(path, packageRoot+"/")

if !unneededGoFiles.Has(pkgRelPath) {
buffers[newPath] = bytes.NewBuffer(code)
}
}

return buffers, err
}

// Similar to "packageRoot" method from pulumi/pkg/codegen/go/gen.go
func getPackageRoot(pkg schema.PackageReference) (string, error) {
def, err := pkg.Definition()
if err != nil {
return "", err
}
var info goGen.GoPackageInfo
if goInfo, ok := def.Language["go"].(goGen.GoPackageInfo); ok {
info = goInfo
}
if info.RootPackageName != "" {
// package structure is flat
return "", nil
}
if info.ImportBasePath != "" {
return path.Base(info.ImportBasePath), nil
}
return strings.ReplaceAll(pkg.Name(), "-", ""), nil
}