Skip to content

Commit

Permalink
Revert: keep standard package list
Browse files Browse the repository at this point in the history
If no go in the PATH, packages will raise error:
```
err: go command required, not found: exec: "go": executable file not found in $PATH: stderr:
```

Signed-off-by: Loong <long0dai@foxmail.com>
  • Loading branch information
daixiang0 committed Mar 13, 2023
1 parent 15e6842 commit 46cd2bc
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 32 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.PHONY: clean test build
.PHONY: clean generate test build

BIN_OUTPUT := $(if $(filter $(shell go env GOOS), windows), dist/gci.exe, dist/gci)

default: clean test build
default: clean generate test build

clean:
@echo BIN_OUTPUT: ${BIN_OUTPUT}
Expand All @@ -13,3 +13,6 @@ build: clean

test: clean
@go test -v -count=1 -cover ./...

generate:
@go generate ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/daixiang0/gci

go 1.20
go 1.18

require (
github.com/hexops/gotextdiff v1.0.3
Expand Down
87 changes: 87 additions & 0 deletions internal/generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/section/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Parse(data []string) (SectionList, error) {
if s == "default" {
list = append(list, Default{})
} else if s == "standard" {
list = append(list, NewStandard())
list = append(list, Standard{})
} else if s == "newline" {
list = append(list, NewLine{})
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (list SectionList) String() []string {
}

func DefaultSections() SectionList {
return SectionList{NewStandard(), Default{}}
return SectionList{Standard{}, Default{}}
}

func DefaultSectionSeparators() SectionList {
Expand Down
26 changes: 7 additions & 19 deletions pkg/section/standard.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
package section

import (
"golang.org/x/tools/go/packages"

"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
)

const StandardType = "standard"

type Standard struct {
standardPackages map[string]struct{}
}

func NewStandard() Standard {
pkgs, err := packages.Load(nil, "std")
if err != nil {
panic(err)
}

standardPackages := make(map[string]struct{})
for _, p := range pkgs {
standardPackages[p.PkgPath] = struct{}{}
}
return Standard{standardPackages: standardPackages}
}
type Standard struct{}

func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if _, ok := s.standardPackages[spec.Path]; ok {
if isStandard(spec.Path) {
return specificity.StandardMatch{}
}
return specificity.MisMatch{}
Expand All @@ -40,3 +23,8 @@ func (s Standard) String() string {
func (s Standard) Type() string {
return StandardType
}

func isStandard(pkg string) bool {
_, ok := standardPackages[pkg]
return ok
}
160 changes: 160 additions & 0 deletions pkg/section/standard_list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions pkg/section/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
)

func TestStandardPackageSpecificity(t *testing.T) {
standard := NewStandard()
testCases := []specificityTestData{
{"context", standard, specificity.StandardMatch{}},
{"contexts", standard, specificity.MisMatch{}},
{"crypto", standard, specificity.StandardMatch{}},
{"crypto1", standard, specificity.MisMatch{}},
{"crypto/ae", standard, specificity.MisMatch{}},
{"crypto/aes", standard, specificity.StandardMatch{}},
{"crypto/aes2", standard, specificity.MisMatch{}},
{"context", Standard{}, specificity.StandardMatch{}},
{"contexts", Standard{}, specificity.MisMatch{}},
{"crypto", Standard{}, specificity.StandardMatch{}},
{"crypto1", Standard{}, specificity.MisMatch{}},
{"crypto/ae", Standard{}, specificity.MisMatch{}},
{"crypto/aes", Standard{}, specificity.StandardMatch{}},
{"crypto/aes2", Standard{}, specificity.MisMatch{}},
}
testSpecificity(t, testCases)
}

0 comments on commit 46cd2bc

Please sign in to comment.