Compare commits
2 Commits
c518272fb9
...
d539b904cc
Author | SHA1 | Date |
---|---|---|
sudo pacman -Syu | d539b904cc | |
sudo pacman -Syu | e60f1202e6 |
21
Makefile
21
Makefile
|
@ -1,7 +1,10 @@
|
|||
.PHONY: all test test-color coverage coverage-cli coverate-html lint format build clean docs
|
||||
|
||||
all: test-color lint format
|
||||
all:
|
||||
go mod tidy
|
||||
$(MAKE) test-color
|
||||
$(MAKE) lint
|
||||
$(MAKE) format
|
||||
|
||||
test:
|
||||
go test -race -failfast ./...
|
||||
|
@ -13,26 +16,30 @@ test-color:
|
|||
coverage:
|
||||
go test -coverprofile=coverage.out ./...
|
||||
|
||||
coverage-cli: coverage
|
||||
coverage-cli:
|
||||
$(MAKE) coverage
|
||||
go tool cover -func=coverage.out
|
||||
|
||||
coverage-html: coverage
|
||||
coverage-html:
|
||||
$(MAKE) coverage
|
||||
go tool cover -html=coverage.out
|
||||
|
||||
lint:
|
||||
golangci-lint run ./...
|
||||
|
||||
format:
|
||||
go install github.com/haunt98/gofimports/cmd/gofimports@latest
|
||||
$(MAKE) build
|
||||
# go install github.com/haunt98/gofimports/cmd/gofimports@latest
|
||||
go install mvdan.cc/gofumpt@latest
|
||||
gofimports -w -company github.com/make-go-great .
|
||||
./gofimports -w --company github.com/make-go-great,github.com/haunt98 .
|
||||
gofumpt -w -extra .
|
||||
$(MAKE) clean
|
||||
|
||||
build: clean
|
||||
build:
|
||||
go build ./cmd/gofimports
|
||||
|
||||
clean:
|
||||
rm -rf gofimports
|
||||
rm -rf ./gofimports
|
||||
|
||||
docs:
|
||||
go install go101.org/golds@latest
|
||||
|
|
|
@ -52,8 +52,8 @@ type Formatter struct {
|
|||
stdPackages map[string]struct{}
|
||||
moduleNames map[string]string
|
||||
formattedPaths map[string]struct{}
|
||||
companyPrefixes map[string]struct{}
|
||||
eg errgroup.Group
|
||||
companyPrefix string
|
||||
muModuleNames sync.RWMutex
|
||||
muFormattedPaths sync.RWMutex
|
||||
isList bool
|
||||
|
@ -316,7 +316,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
|
|||
result := make(map[string][]*dst.ImportSpec)
|
||||
result[stdImport] = make([]*dst.ImportSpec, 0, 8)
|
||||
result[thirdPartyImport] = make([]*dst.ImportSpec, 0, 8)
|
||||
if ft.companyPrefix != "" {
|
||||
if len(ft.companyPrefixes) != 0 {
|
||||
result[companyImport] = make([]*dst.ImportSpec, 0, 8)
|
||||
}
|
||||
result[localImport] = make([]*dst.ImportSpec, 0, 8)
|
||||
|
@ -335,10 +335,19 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
|
|||
continue
|
||||
}
|
||||
|
||||
if ft.companyPrefix != "" &&
|
||||
strings.HasPrefix(importPath, ft.companyPrefix) {
|
||||
result[companyImport] = append(result[companyImport], importSpec)
|
||||
continue
|
||||
if len(ft.companyPrefixes) != 0 {
|
||||
existImport := false
|
||||
for companyPrefix := range ft.companyPrefixes {
|
||||
if strings.HasPrefix(importPath, companyPrefix) {
|
||||
result[companyImport] = append(result[companyImport], importSpec)
|
||||
existImport = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if existImport {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
|
||||
|
@ -346,7 +355,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
|
|||
|
||||
ft.logDSTImportSpecs("groupDSTImportSpecs: stdImport", result[stdImport])
|
||||
ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport])
|
||||
if ft.companyPrefix != "" {
|
||||
if len(ft.companyPrefixes) != 0 {
|
||||
ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport])
|
||||
}
|
||||
ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport])
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package imports
|
||||
|
||||
import "strings"
|
||||
|
||||
type FormatterOptionFn func(*Formatter)
|
||||
|
||||
func FormatterWithList(isList bool) FormatterOptionFn {
|
||||
|
@ -28,6 +30,13 @@ func FormatterWithVerbose(isVerbose bool) FormatterOptionFn {
|
|||
|
||||
func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
|
||||
return func(ft *Formatter) {
|
||||
ft.companyPrefix = companyPrefix
|
||||
ft.companyPrefixes = make(map[string]struct{})
|
||||
for _, prefix := range strings.Split(companyPrefix, ",") {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
if prefix == "" {
|
||||
continue
|
||||
}
|
||||
ft.companyPrefixes[prefix] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue