Skip to content

Commit

Permalink
refactor: use grignotin
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 30, 2024
1 parent c07d193 commit 6f7aacd
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ldez/gomoddirectives
go 1.22.0

require (
github.com/ldez/grignotin v0.6.0
github.com/stretchr/testify v1.10.0
golang.org/x/mod v0.22.0
golang.org/x/tools v0.27.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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=
github.com/ldez/grignotin v0.6.0 h1:i++3002hxD5TpVto0iLjLrfz1V+yEJ+BBk4glb3aqC8=
github.com/ldez/grignotin v0.6.0/go.mod h1:uaVTr0SoZ1KBii33c47O1M8Jp3OP3YDwhZCmzT9GHEk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
Expand Down
3 changes: 2 additions & 1 deletion gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/token"
"strings"

"github.com/ldez/grignotin/gomod"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/analysis"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ type Options struct {

// AnalyzePass analyzes a pass.
func AnalyzePass(pass *analysis.Pass, opts Options) ([]Result, error) {
info, err := getModuleInfo()
info, err := gomod.GetModuleInfo()
if err != nil {
return nil, fmt.Errorf("get information about modules: %w", err)
}
Expand Down
35 changes: 0 additions & 35 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,3 @@ func parseGoMod(goMod string) (*modfile.File, error) {

return modfile.Parse("go.mod", raw, nil)
}

func getModuleInfo() ([]modInfo, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")

out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("command go list: %w: %s", err, string(out))
}

var infos []modInfo

for dec := json.NewDecoder(bytes.NewBuffer(out)); dec.More(); {
var v modInfo
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("unmarshaling error: %w: %s", err, string(out))
}

if v.GoMod == "" {
return nil, errors.New("working directory is not part of a module")
}

if !v.Main || v.Dir == "" {
continue
}

infos = append(infos, v)
}

if len(infos) == 0 {
return nil, errors.New("go.mod file not found")
}

return infos, nil
}

0 comments on commit 6f7aacd

Please sign in to comment.