Skip to content

Commit

Permalink
Speed up goimports usage (GoogleCloudPlatform#11896)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirGitsalot authored and gontech committed Oct 16, 2024
1 parent 88190f3 commit c68d97c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 45 additions & 8 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ package provider

import (
"bytes"
"errors"
"fmt"
"go/format"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

"text/template"

Expand All @@ -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}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion mmv1/templates/terraform/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c68d97c

Please sign in to comment.