lint imports grouping (#993)
This commit is contained in:
parent
425ac45d89
commit
db5a990719
@ -59,6 +59,7 @@ matrix:
|
||||
- cp go.mod.backup go.mod
|
||||
script:
|
||||
- go run ./scripts/check-copyright.go
|
||||
- go run ./scripts/check-imports.go ./...
|
||||
- go run ./scripts/protobuf.go --protoc=$HOME/protoc/bin/protoc lint
|
||||
- golangci-lint run
|
||||
- gospace istidy
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"storj.io/storj/pkg/cfgstruct"
|
||||
"storj.io/storj/pkg/identity"
|
||||
"storj.io/storj/pkg/process"
|
||||
|
@ -10,12 +10,11 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"storj.io/storj/internal/fpath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/storj/cmd/statreceiver/luacfg"
|
||||
"storj.io/storj/internal/fpath"
|
||||
"storj.io/storj/pkg/cfgstruct"
|
||||
"storj.io/storj/pkg/process"
|
||||
)
|
||||
|
@ -6,9 +6,8 @@ package fpath
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//EditFile opens the best OS-specific text editor we can find
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"unsafe"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
@ -11,9 +11,8 @@ import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/zeebo/errs"
|
||||
)
|
||||
|
||||
// APIKeys is interface for working with api keys store
|
||||
|
@ -4,10 +4,10 @@
|
||||
package satellitedb
|
||||
|
||||
import (
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"context"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/storj/internal/migrate"
|
||||
"storj.io/storj/pkg/satellite"
|
||||
"storj.io/storj/pkg/satellite/satellitedb/dbx"
|
||||
|
@ -5,6 +5,7 @@ package satelliteql
|
||||
|
||||
import (
|
||||
"github.com/graphql-go/graphql"
|
||||
|
||||
"storj.io/storj/pkg/satellite"
|
||||
)
|
||||
|
||||
|
@ -8,14 +8,11 @@ import (
|
||||
"crypto/subtle"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"gopkg.in/spacemonkeygo/monkit.v2"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
||||
|
||||
"storj.io/storj/pkg/auth"
|
||||
"storj.io/storj/pkg/satellite/satelliteauth"
|
||||
|
@ -6,10 +6,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
@ -20,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
/*
|
||||
This tool verifies whether imports are divided into three blocks:
|
||||
check-imports verifies whether imports are divided into three blocks:
|
||||
|
||||
std packages
|
||||
external packages
|
||||
@ -45,6 +47,8 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("checking import order:")
|
||||
|
||||
seen := map[*packages.Package]bool{}
|
||||
pkgs := []*packages.Package{}
|
||||
|
||||
@ -69,18 +73,36 @@ func main() {
|
||||
}
|
||||
|
||||
sort.Slice(pkgs, func(i, k int) bool { return pkgs[i].ID < pkgs[k].ID })
|
||||
correct := true
|
||||
for _, pkg := range pkgs {
|
||||
process(pkg)
|
||||
if !correctPackage(pkg) {
|
||||
correct = false
|
||||
}
|
||||
}
|
||||
|
||||
if !correct {
|
||||
fmt.Fprintln(os.Stderr, "imports not in the correct order")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func process(pkg *packages.Package) {
|
||||
func correctPackage(pkg *packages.Package) bool {
|
||||
correct := true
|
||||
for i, file := range pkg.Syntax {
|
||||
checkImports(pkg.Fset, pkg.CompiledGoFiles[i], file)
|
||||
path := pkg.CompiledGoFiles[i]
|
||||
if !correctImports(pkg.Fset, path, file) {
|
||||
if !isGenerated(path) { // ignore generated files
|
||||
fmt.Fprintln(os.Stderr, path)
|
||||
correct = false
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "(ignoring generated)", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
return correct
|
||||
}
|
||||
|
||||
func checkImports(fset *token.FileSet, name string, f *ast.File) {
|
||||
func correctImports(fset *token.FileSet, name string, f *ast.File) bool {
|
||||
for _, d := range f.Decls {
|
||||
d, ok := d.(*ast.GenDecl)
|
||||
if !ok || d.Tok != token.IMPORT {
|
||||
@ -108,9 +130,10 @@ func checkImports(fset *token.FileSet, name string, f *ast.File) {
|
||||
specgroups = append(specgroups, d.Specs[lastGroup:])
|
||||
|
||||
if !correctOrder(specgroups) {
|
||||
fmt.Println(name)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func correctOrder(specgroups [][]ast.Spec) bool {
|
||||
@ -182,3 +205,26 @@ func includeStd(p *packages.Package) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func isGenerated(path string) bool {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to read %v: %v\n", path, err)
|
||||
return false
|
||||
}
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
}()
|
||||
|
||||
var header [256]byte
|
||||
n, err := file.Read(header[:])
|
||||
if err != nil && err != io.EOF {
|
||||
fmt.Fprintf(os.Stderr, "failed to read %v: %v\n", path, err)
|
||||
return false
|
||||
}
|
||||
|
||||
return bytes.Contains(header[:n], []byte(`AUTOGENERATED`)) ||
|
||||
bytes.Contains(header[:n], []byte(`Code generated`))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user