diff --git a/mmv1/main.go b/mmv1/main.go index d7af7dec68c0..19ac6c6c7ec7 100644 --- a/mmv1/main.go +++ b/mmv1/main.go @@ -56,6 +56,8 @@ var yamlTempMode = flag.Bool("yaml-temp", false, "copy text over from ruby yaml var handwrittenTempFiles = flag.String("handwritten-temp", "", "copy specific handwritten files over from .erb to go .tmpl.temp comma separated") var templateTempFiles = flag.String("template-temp", "", "copy specific templates over from .erb to go .tmpl.temp comma separated") +var showImportDiffs = flag.Bool("show-import-diffs", false, "write go import diffs to stdout") + func main() { flag.Parse() @@ -193,6 +195,8 @@ func main() { if generateCode { providerToGenerate.CompileCommonFiles(*outputPath, productsForVersion, "") } + + provider.FixImports(*outputPath, *showImportDiffs) } func GenerateProduct(productChannel chan string, providerToGenerate provider.Provider, productsForVersionChannel chan *api.Product, startTime time.Time, productsToGenerate []string, resourceToGenerate, overrideDirectory string, generateCode, generateDocs bool) { diff --git a/mmv1/provider/template_data.go b/mmv1/provider/template_data.go index eadc67ffe24d..862f85a84cb4 100644 --- a/mmv1/provider/template_data.go +++ b/mmv1/provider/template_data.go @@ -15,6 +15,7 @@ package provider import ( "bytes" + "errors" "fmt" "go/format" "log" @@ -22,6 +23,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "text/template" @@ -48,6 +50,8 @@ var BETA_VERSION = "beta" var ALPHA_VERSION = "alpha" var PRIVATE_VERSION = "private" +var goimportFiles sync.Map + func NewTemplateData(outputFolder string, versionName string) *TemplateData { td := TemplateData{OutputFolder: outputFolder, VersionName: versionName} @@ -208,20 +212,15 @@ func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, g } else { sourceByte = formattedByte } + if !strings.Contains(templatePath, "third_party/terraform") { + goimportFiles.Store(filePath, struct{}{}) + } } err = os.WriteFile(filePath, sourceByte, 0644) if err != nil { glog.Exit(err) } - - if goFormat && !strings.Contains(templatePath, "third_party/terraform") { - cmd := exec.Command("goimports", "-w", filepath.Base(filePath)) - cmd.Dir = filepath.Dir(filePath) - if err := cmd.Run(); err != nil { - log.Fatal(err) - } - } } func (td *TemplateData) ImportPath() string { @@ -233,6 +232,44 @@ func (td *TemplateData) ImportPath() string { return "github.com/hashicorp/terraform-provider-google-beta/google-beta" } +func FixImports(outputPath string, dumpDiffs bool) { + log.Printf("Fixing go import paths") + + baseArgs := []string{"-w"} + if dumpDiffs { + baseArgs = []string{"-d", "-w"} + } + + // -w and -d are mutually exclusive; if dumpDiffs is requested we need to run twice. + for _, base := range baseArgs { + hasFiles := false + args := []string{base} + goimportFiles.Range(func(filePath, _ any) bool { + p, err := filepath.Rel(outputPath, filePath.(string)) + if err != nil { + log.Fatal(err) + } + args = append(args, p) + hasFiles = true + return true + }) + + if hasFiles { + cmd := exec.Command("goimports", args...) + cmd.Dir = outputPath + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 { + glog.Error(string(exitErr.Stderr)) + } + log.Fatal(err) + } + } + } +} + type TestInput struct { Res api.Resource ImportPath string diff --git a/mmv1/templates/terraform/resource.go.tmpl b/mmv1/templates/terraform/resource.go.tmpl index 494767eb3e3b..5f52de444c19 100644 --- a/mmv1/templates/terraform/resource.go.tmpl +++ b/mmv1/templates/terraform/resource.go.tmpl @@ -35,8 +35,12 @@ import ( "fmt" "log" + "net/http" "reflect" -{{- if and (not $.Immutable) ($.UpdateMask) }} +{{- if $.SupportsIndirectUserProjectOverride }} + "regexp" +{{- end }} +{{- if or (and (not $.Immutable) ($.UpdateMask)) $.LegacyLongFormProject }} "strings" {{- end }} "time"