lint imports grouping (#993)

This commit is contained in:
Egon Elbre 2019-01-08 16:05:14 +02:00 committed by GitHub
parent 425ac45d89
commit db5a990719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 20 deletions

View File

@ -59,6 +59,7 @@ matrix:
- cp go.mod.backup go.mod - cp go.mod.backup go.mod
script: script:
- go run ./scripts/check-copyright.go - go run ./scripts/check-copyright.go
- go run ./scripts/check-imports.go ./...
- go run ./scripts/protobuf.go --protoc=$HOME/protoc/bin/protoc lint - go run ./scripts/protobuf.go --protoc=$HOME/protoc/bin/protoc lint
- golangci-lint run - golangci-lint run
- gospace istidy - gospace istidy

View File

@ -13,6 +13,7 @@ import (
"sync/atomic" "sync/atomic"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"storj.io/storj/pkg/cfgstruct" "storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/identity" "storj.io/storj/pkg/identity"
"storj.io/storj/pkg/process" "storj.io/storj/pkg/process"

View File

@ -10,12 +10,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"storj.io/storj/internal/fpath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/zeebo/errs" "github.com/zeebo/errs"
"storj.io/storj/cmd/statreceiver/luacfg" "storj.io/storj/cmd/statreceiver/luacfg"
"storj.io/storj/internal/fpath"
"storj.io/storj/pkg/cfgstruct" "storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/process" "storj.io/storj/pkg/process"
) )

View File

@ -6,9 +6,8 @@ package fpath
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"os/exec" "os/exec"
"strings"
) )
//EditFile opens the best OS-specific text editor we can find //EditFile opens the best OS-specific text editor we can find

View File

@ -11,7 +11,6 @@ import (
"path/filepath" "path/filepath"
"sync/atomic" "sync/atomic"
"time" "time"
"unsafe" "unsafe"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"

View File

@ -11,9 +11,8 @@ import (
"io" "io"
"time" "time"
"github.com/zeebo/errs"
"github.com/skyrings/skyring-common/tools/uuid" "github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
) )
// APIKeys is interface for working with api keys store // APIKeys is interface for working with api keys store

View File

@ -4,10 +4,10 @@
package satellitedb package satellitedb
import ( import (
"github.com/zeebo/errs"
"context" "context"
"github.com/zeebo/errs"
"storj.io/storj/internal/migrate" "storj.io/storj/internal/migrate"
"storj.io/storj/pkg/satellite" "storj.io/storj/pkg/satellite"
"storj.io/storj/pkg/satellite/satellitedb/dbx" "storj.io/storj/pkg/satellite/satellitedb/dbx"

View File

@ -5,6 +5,7 @@ package satelliteql
import ( import (
"github.com/graphql-go/graphql" "github.com/graphql-go/graphql"
"storj.io/storj/pkg/satellite" "storj.io/storj/pkg/satellite"
) )

View File

@ -8,14 +8,11 @@ import (
"crypto/subtle" "crypto/subtle"
"time" "time"
"golang.org/x/crypto/bcrypt"
"github.com/skyrings/skyring-common/tools/uuid" "github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs" "github.com/zeebo/errs"
"go.uber.org/zap" "go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"gopkg.in/spacemonkeygo/monkit.v2" monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/auth" "storj.io/storj/pkg/auth"
"storj.io/storj/pkg/satellite/satelliteauth" "storj.io/storj/pkg/satellite/satelliteauth"

View File

@ -6,10 +6,12 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"go/ast" "go/ast"
"go/token" "go/token"
"io"
"os" "os"
"runtime" "runtime"
"sort" "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 std packages
external packages external packages
@ -45,6 +47,8 @@ func main() {
panic(err) panic(err)
} }
fmt.Println("checking import order:")
seen := map[*packages.Package]bool{} seen := map[*packages.Package]bool{}
pkgs := []*packages.Package{} pkgs := []*packages.Package{}
@ -69,18 +73,36 @@ func main() {
} }
sort.Slice(pkgs, func(i, k int) bool { return pkgs[i].ID < pkgs[k].ID }) sort.Slice(pkgs, func(i, k int) bool { return pkgs[i].ID < pkgs[k].ID })
correct := true
for _, pkg := range pkgs { 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 { 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 { for _, d := range f.Decls {
d, ok := d.(*ast.GenDecl) d, ok := d.(*ast.GenDecl)
if !ok || d.Tok != token.IMPORT { 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:]) specgroups = append(specgroups, d.Specs[lastGroup:])
if !correctOrder(specgroups) { if !correctOrder(specgroups) {
fmt.Println(name) return false
} }
} }
return true
} }
func correctOrder(specgroups [][]ast.Spec) bool { func correctOrder(specgroups [][]ast.Spec) bool {
@ -182,3 +205,26 @@ func includeStd(p *packages.Package) {
return 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`))
}