Skip to content

gci: update the way to call Run() #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Djarvur/go-err113 v0.0.0-20200511133814-5174e21577d5
github.com/OpenPeeDeeP/depguard v1.0.1
github.com/bombsimon/wsl/v3 v3.1.0
github.com/daixiang0/gci v0.2.2
github.com/daixiang0/gci v0.2.4
github.com/denis-tingajkin/go-header v0.3.1
github.com/fatih/color v1.9.0
github.com/go-critic/go-critic v0.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/daixiang0/gci v0.2.2 h1:ql+M0OX7Z+tPt0WAMpN11rgEvZGc+dqxuw5dn6d3UJM=
github.com/daixiang0/gci v0.2.2/go.mod h1:+AV8KmHTGxxwp/pY84TLQfFKp2vuKXXJVzF3kD/hfR4=
github.com/daixiang0/gci v0.2.4 h1:BUCKk5nlK2m+kRIsoj+wb/5hazHvHeZieBKWd9Afa8Q=
github.com/daixiang0/gci v0.2.4/go.mod h1:+AV8KmHTGxxwp/pY84TLQfFKp2vuKXXJVzF3kD/hfR4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
23 changes: 19 additions & 4 deletions pkg/golinters/gci.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package golinters

import (
"bytes"
"fmt"
"sync"

"github.com/daixiang0/gci/pkg/gci"
"github.com/pkg/errors"
"github.com/shazow/go-diff/difflib"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
Expand All @@ -16,6 +19,7 @@ const gciName = "gci"
func NewGci() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
differ := difflib.New()

analyzer := &analysis.Analyzer{
Name: gciName,
Expand Down Expand Up @@ -43,17 +47,28 @@ func NewGci() *goanalysis.Linter {
var issues []goanalysis.Issue

for _, f := range fileNames {
diff, err := gci.Run(f, &gci.FlagSet{LocalFlag: localFlag})
source, result, err := gci.Run(f, &gci.FlagSet{LocalFlag: localFlag})
if err != nil {
return nil, err
}
if diff == nil {
if result == nil {
continue
}

is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gciName)
diff := bytes.Buffer{}
_, err = diff.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", string(diff))
return nil, fmt.Errorf("can't write diff header: %v", err)
}

err = differ.Diff(&diff, bytes.NewReader(source), bytes.NewReader(result))
if err != nil {
return nil, fmt.Errorf("can't get gci diff output: %v", err)
}

is, err := extractIssuesFromPatch(diff.String(), lintCtx.Log, lintCtx, gciName)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", diff.String())
}

for i := range is {
Expand Down
2 changes: 1 addition & 1 deletion test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGciLocal(t *testing.T) {
assert.NoError(t, err)

testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue("testdata/gci/gci.go:8: File is not `gci`-ed")
ExpectHasIssue("testdata/gci/gci.go:7: File is not `gci`-ed")
}

func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
Expand Down