Compare commits

...

2 Commits

3 changed files with 40 additions and 15 deletions

View File

@ -1,7 +1,10 @@
.PHONY: all test test-color coverage coverage-cli coverate-html lint format build clean docs .PHONY: all test test-color coverage coverage-cli coverate-html lint format build clean docs
all: test-color lint format all:
go mod tidy go mod tidy
$(MAKE) test-color
$(MAKE) lint
$(MAKE) format
test: test:
go test -race -failfast ./... go test -race -failfast ./...
@ -13,26 +16,30 @@ test-color:
coverage: coverage:
go test -coverprofile=coverage.out ./... go test -coverprofile=coverage.out ./...
coverage-cli: coverage coverage-cli:
$(MAKE) coverage
go tool cover -func=coverage.out go tool cover -func=coverage.out
coverage-html: coverage coverage-html:
$(MAKE) coverage
go tool cover -html=coverage.out go tool cover -html=coverage.out
lint: lint:
golangci-lint run ./... golangci-lint run ./...
format: 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 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 . gofumpt -w -extra .
$(MAKE) clean
build: clean build:
go build ./cmd/gofimports go build ./cmd/gofimports
clean: clean:
rm -rf gofimports rm -rf ./gofimports
docs: docs:
go install go101.org/golds@latest go install go101.org/golds@latest

View File

@ -52,8 +52,8 @@ type Formatter struct {
stdPackages map[string]struct{} stdPackages map[string]struct{}
moduleNames map[string]string moduleNames map[string]string
formattedPaths map[string]struct{} formattedPaths map[string]struct{}
companyPrefixes map[string]struct{}
eg errgroup.Group eg errgroup.Group
companyPrefix string
muModuleNames sync.RWMutex muModuleNames sync.RWMutex
muFormattedPaths sync.RWMutex muFormattedPaths sync.RWMutex
isList bool isList bool
@ -316,7 +316,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
result := make(map[string][]*dst.ImportSpec) result := make(map[string][]*dst.ImportSpec)
result[stdImport] = make([]*dst.ImportSpec, 0, 8) result[stdImport] = make([]*dst.ImportSpec, 0, 8)
result[thirdPartyImport] = 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[companyImport] = make([]*dst.ImportSpec, 0, 8)
} }
result[localImport] = 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 continue
} }
if ft.companyPrefix != "" && if len(ft.companyPrefixes) != 0 {
strings.HasPrefix(importPath, ft.companyPrefix) { existImport := false
result[companyImport] = append(result[companyImport], importSpec) for companyPrefix := range ft.companyPrefixes {
continue if strings.HasPrefix(importPath, companyPrefix) {
result[companyImport] = append(result[companyImport], importSpec)
existImport = true
break
}
}
if existImport {
continue
}
} }
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec) 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: stdImport", result[stdImport])
ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport])
if ft.companyPrefix != "" { if len(ft.companyPrefixes) != 0 {
ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport])
} }
ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport])

View File

@ -1,5 +1,7 @@
package imports package imports
import "strings"
type FormatterOptionFn func(*Formatter) type FormatterOptionFn func(*Formatter)
func FormatterWithList(isList bool) FormatterOptionFn { func FormatterWithList(isList bool) FormatterOptionFn {
@ -28,6 +30,13 @@ func FormatterWithVerbose(isVerbose bool) FormatterOptionFn {
func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn { func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
return func(ft *Formatter) { 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{}{}
}
} }
} }