From 6f7aacdc4d1b9bbada283f8434bc1921a5eacde7 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 30 Nov 2024 23:51:23 +0100 Subject: [PATCH] refactor: use grignotin --- go.mod | 1 + go.sum | 2 ++ gomoddirectives.go | 3 ++- module.go | 35 ----------------------------------- 4 files changed, 5 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 531465d..a9efa0d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 905e8e7..a8b9b92 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/gomoddirectives.go b/gomoddirectives.go index 22db67f..3fb8b3a 100644 --- a/gomoddirectives.go +++ b/gomoddirectives.go @@ -6,6 +6,7 @@ import ( "go/token" "strings" + "github.com/ldez/grignotin/gomod" "golang.org/x/mod/modfile" "golang.org/x/tools/go/analysis" ) @@ -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) } diff --git a/module.go b/module.go index d6a4298..7f74d0b 100644 --- a/module.go +++ b/module.go @@ -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 -}