-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0e5c31
commit 1d2fc04
Showing
7 changed files
with
248 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/minio/minio/pkg/wildcard" | ||
|
||
"github.com/momotaro98/strictimportsort" | ||
) | ||
|
||
const ( | ||
invalidArgumentExitCode = 3 | ||
) | ||
|
||
var ( | ||
localPaths = flag.String("local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list") | ||
excludes = flag.String("exclude", "", "file names you wanna exclude; wile card is welcome; comma-separated list") | ||
excludeDirs = flag.String("exclude-dir", "", "directory names you wanna exclude; wile card is welcome; comma-separated list") | ||
dontRecurseFlag = flag.Bool("n", false, "don't recursively check paths") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if len(flag.Args()) == 0 { | ||
fmt.Println("missing argument: filepath") | ||
os.Exit(invalidArgumentExitCode) | ||
} | ||
|
||
var lintFailed bool | ||
for _, path := range flag.Args() { | ||
rootPath, err := filepath.Abs(path) | ||
if err != nil { | ||
fmt.Printf("Error finding absolute path: %s", err) | ||
os.Exit(invalidArgumentExitCode) | ||
} | ||
if walk(rootPath) { | ||
lintFailed = true | ||
} | ||
} | ||
|
||
if lintFailed { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func walk(rootPath string) bool { | ||
var lintFailed bool | ||
filepath.Walk(rootPath, func(filePath string, fi os.FileInfo, err error) error { | ||
if err != nil { | ||
fmt.Printf("Error during filesystem walk: %v\n", err) | ||
return nil | ||
} | ||
if fi.IsDir() { | ||
if *excludeDirs != "" { | ||
patternDirs := strings.Split(*excludeDirs, ",") | ||
p := strings.Split(filePath, "/") | ||
dirName := p[len(p)-1] | ||
for _, pattern := range patternDirs { | ||
if wildcard.MatchSimple(pattern, dirName) { | ||
return filepath.SkipDir | ||
} | ||
} | ||
} | ||
if filePath != rootPath && (*dontRecurseFlag || | ||
filepath.Base(filePath) == "testdata" || | ||
filepath.Base(filePath) == "vendor") { | ||
return filepath.SkipDir | ||
} | ||
return nil | ||
} | ||
if *excludes != "" { | ||
patternFiles := strings.Split(*excludes, ",") | ||
p := strings.Split(filePath, "/") | ||
fileName := p[len(p)-1] | ||
for _, pattern := range patternFiles { | ||
if wildcard.MatchSimple(pattern, fileName) { | ||
return nil | ||
} | ||
} | ||
} | ||
if !strings.HasSuffix(filePath, ".go") { | ||
return nil | ||
} | ||
fset, poses, correctImport := strictimportsort.Run(filePath, *localPaths) | ||
for _, pos := range poses { | ||
fmt.Printf("%s: import not sorted correctly. should be replace to\n%s\n", fset.Position(pos), correctImport) | ||
lintFailed = true | ||
} | ||
return nil | ||
}) | ||
return lintFailed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package strictimportsort | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
const ( | ||
localPath = "github.com/momotaro98" | ||
data01 = "testdata/src/testdata01.go" | ||
data02 = "testdata/src/testdata02.go" | ||
) | ||
|
||
passCases := []struct { | ||
name string | ||
filePath string | ||
localPath string | ||
}{ | ||
{ | ||
name: "pass case without local path", | ||
filePath: data01, | ||
localPath: "", | ||
}, | ||
{ | ||
name: "pass case with local path", | ||
filePath: data02, | ||
localPath: localPath, | ||
}, | ||
} | ||
for _, tt := range passCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, poses, actCorrect := Run(tt.filePath, tt.localPath) | ||
if len(poses) != 0 { | ||
t.Error("unexpected:", poses) | ||
} | ||
if actCorrect != "" { | ||
t.Error("unexpected:", actCorrect) | ||
} | ||
}) | ||
} | ||
|
||
failCases := []struct { | ||
name string | ||
filePath string | ||
localPath string | ||
expectedLineColumn string | ||
expectedCorrect string | ||
}{ | ||
{ | ||
name: "fail with local path", | ||
filePath: data01, | ||
localPath: localPath, | ||
expectedLineColumn: "7:2", | ||
expectedCorrect: `import ( | ||
_ "fmt" | ||
_ "github.com/golang/mock/gomock" | ||
_ "github.com/momotaro98/strictimportsort" | ||
)`, | ||
}, | ||
{ | ||
name: "fail without local path", | ||
filePath: data02, | ||
localPath: "", | ||
expectedLineColumn: "7:1", | ||
expectedCorrect: `import ( | ||
_ "fmt" | ||
_ "github.com/golang/mock/gomock" | ||
_ "github.com/momotaro98/strictimportsort" | ||
)`, | ||
}, | ||
} | ||
|
||
for _, tt := range failCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fset, poses, actCorrect := Run(tt.filePath, tt.localPath) | ||
for _, pos := range poses { | ||
if p := fset.Position(pos).String(); p != fmt.Sprintf("%s:%s", tt.filePath, tt.expectedLineColumn) { | ||
t.Error("unexpected:", p) | ||
} | ||
if actCorrect != tt.expectedCorrect { | ||
t.Error("unexpected:", actCorrect) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package src | ||
|
||
import ( | ||
_ "fmt" | ||
|
||
_ "github.com/golang/mock/gomock" | ||
_ "github.com/momotaro98/strictimportsort" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package src | ||
|
||
import ( | ||
_ "fmt" | ||
|
||
_ "github.com/golang/mock/gomock" | ||
|
||
_ "github.com/momotaro98/strictimportsort" | ||
) |