Merge pull request #8862 from planetscale/goimports-asthelpergen

Apply goimports in `asthelpergen`
This commit is contained in:
Manan Gupta 2021-09-22 12:29:07 +05:30 коммит произвёл GitHub
Родитель b6e33ffe86 d16a89f8e6
Коммит 43ce83280d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 67 добавлений и 29 удалений

1
.github/workflows/check_make_visitor.yml поставляемый
Просмотреть файл

@ -34,6 +34,7 @@ jobs:
sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/
sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
go mod download
go install golang.org/x/tools/cmd/goimports@latest
- name: Run make minimaltools
run: |

Просмотреть файл

@ -25,6 +25,8 @@ import (
"path"
"strings"
"vitess.io/vitess/go/tools/goimports"
"vitess.io/vitess/go/tools/common"
"github.com/dave/jennifer/jen"
@ -182,13 +184,13 @@ func VerifyFilesOnDisk(result map[string]*jen.File) (errors []error) {
continue
}
var buf bytes.Buffer
if err := file.Render(&buf); err != nil {
errors = append(errors, fmt.Errorf("render error for '%s': %w", fullPath, err))
genFile, err := goimports.FormatJenFile(file)
if err != nil {
errors = append(errors, fmt.Errorf("goimport error: %w", err))
continue
}
if !bytes.Equal(existing, buf.Bytes()) {
if !bytes.Equal(existing, genFile) {
errors = append(errors, fmt.Errorf("'%s' has changed", fullPath))
continue
}

Просмотреть файл

@ -18,8 +18,11 @@ package main
import (
"flag"
"io/ioutil"
"log"
"vitess.io/vitess/go/tools/goimports"
. "vitess.io/vitess/go/tools/asthelpergen"
)
@ -46,7 +49,12 @@ func main() {
log.Printf("%d files OK", len(result))
} else {
for fullPath, file := range result {
if err := file.Save(fullPath); err != nil {
content, err := goimports.FormatJenFile(file)
if err != nil {
log.Fatalf("failed to apply goimport to '%s': %v", fullPath, err)
}
err = ioutil.WriteFile(fullPath, content, 0664)
if err != nil {
log.Fatalf("failed to save file to '%s': %v", fullPath, err)
}
log.Printf("saved '%s'", fullPath)

Просмотреть файл

@ -0,0 +1,47 @@
/*
Copyright 2021 The Vitess Authors.
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.
*/
package goimports
import (
"io/ioutil"
"os"
"os/exec"
"github.com/dave/jennifer/jen"
)
// FormatJenFile formats the given *jen.File with goimports and return a slice
// of byte corresponding to the formatted file.
func FormatJenFile(file *jen.File) ([]byte, error) {
tempFile, err := ioutil.TempFile("/tmp", "*.go")
if err != nil {
return nil, err
}
err = file.Save(tempFile.Name())
if err != nil {
return nil, err
}
cmd := exec.Command("goimports", "-local", "vitess.io/vitess", "-w", tempFile.Name())
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return nil, err
}
return ioutil.ReadFile(tempFile.Name())
}

Просмотреть файл

@ -23,12 +23,12 @@ import (
"go/types"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"
"vitess.io/vitess/go/tools/goimports"
"vitess.io/vitess/go/hack"
"vitess.io/vitess/go/tools/common"
@ -475,7 +475,7 @@ func main() {
log.Printf("%d files OK", len(result))
} else {
for fullPath, file := range result {
content, err := getFileInGoimportFormat(file)
content, err := goimports.FormatJenFile(file)
if err != nil {
log.Fatalf("failed to apply goimport to '%s': %v", fullPath, err)
}
@ -499,7 +499,7 @@ func VerifyFilesOnDisk(result map[string]*jen.File) (errors []error) {
continue
}
genFile, err := getFileInGoimportFormat(file)
genFile, err := goimports.FormatJenFile(file)
if err != nil {
errors = append(errors, fmt.Errorf("goimport error: %w", err))
continue
@ -565,23 +565,3 @@ func GenerateSizeHelpers(packagePatterns []string, typePatterns []string) (map[s
return sizegen.finalize(), nil
}
func getFileInGoimportFormat(file *jen.File) ([]byte, error) {
tempFile, err := ioutil.TempFile("/tmp", "*.go")
if err != nil {
return nil, err
}
err = file.Save(tempFile.Name())
if err != nil {
return nil, err
}
cmd := exec.Command("goimports", "-local", "vitess.io/vitess", "-w", tempFile.Name())
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return nil, err
}
return ioutil.ReadFile(tempFile.Name())
}