From 429c3837e4c7e809f8f6778322a5b0838a4613af Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Thu, 29 Aug 2019 15:47:53 +0100 Subject: [PATCH] extract 'go list' call to kmoe/go-list package --- cmd/check/sdk_refs.go | 62 +++---------------------------------------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 6 insertions(+), 59 deletions(-) diff --git a/cmd/check/sdk_refs.go b/cmd/check/sdk_refs.go index b16da8c..cddd5b6 100644 --- a/cmd/check/sdk_refs.go +++ b/cmd/check/sdk_refs.go @@ -1,18 +1,14 @@ package check import ( - "bytes" - "encoding/json" "fmt" "go/ast" "go/parser" "go/token" - "io" - "os" - "os/exec" "path" "github.com/hashicorp/tf-sdk-migrator/util" + goList "github.com/kmoe/go-list" refsParser "github.com/radeksimko/go-refs/parser" ) @@ -65,32 +61,6 @@ var deprecations = []*identDeprecation{ }, } -// Package represents the subset of `go list` output we are interested in -type Package struct { - Dir string // directory containing package sources - ImportPath string // import path of package in dir - ImportComment string // path in import comment on package statement - - // Source files - GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) - TestGoFiles []string // _test.go files in package - - // Dependency information - Imports []string // import paths used by this package - ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) - Deps []string // all (recursively) imported dependencies - TestImports []string // imports from TestGoFiles - - // Error information - Incomplete bool // this package or a dependency has an error - Error *PackageError // error loading package - DepsErrors []*PackageError // errors loading dependencies -} - -type PackageError struct { - Err string -} - // ProviderImports is a data structure we parse the `go list` output into // for efficient searching type ProviderImportDetails struct { @@ -107,25 +77,8 @@ type ProviderPackage struct { TestImports []string } -func GoCmd(workDir string, args ...string) (*bytes.Buffer, string, error) { - cmd := exec.Command("go", args...) - cmd.Dir = workDir - - var stdout, stderr bytes.Buffer - cmd.Env = append(os.Environ(), "GO111MODULE=on") - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err := cmd.Run() - if err != nil { - return nil, stderr.String(), fmt.Errorf("%q: %s", args, err) - } - - return &stdout, stderr.String(), nil -} - func GoListPackageImports(providerPath string) (*ProviderImportDetails, error) { - out, _, err := GoCmd(providerPath, "list", "-json", "./...") + packages, err := goList.GoList(providerPath, "./...") if err != nil { return nil, err } @@ -133,16 +86,7 @@ func GoListPackageImports(providerPath string) (*ProviderImportDetails, error) { allImportPathsHash := make(map[string]bool) providerPackages := make(map[string]ProviderPackage) - dec := json.NewDecoder(bytes.NewReader(out.Bytes())) - for { - var p Package - if err := dec.Decode(&p); err != nil { - if err == io.EOF { - break - } - return nil, err - } - + for _, p := range packages { for _, i := range p.Imports { allImportPathsHash[i] = true } diff --git a/go.mod b/go.mod index 4da1903..2bdc03a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/hashicorp/go-multierror v1.0.0 github.com/hashicorp/go-version v1.2.0 + github.com/kmoe/go-list v0.0.1 github.com/mitchellh/cli v1.0.0 github.com/radeksimko/go-refs v0.0.0-20190614111518-1b15b5989e59 github.com/radeksimko/mod v0.0.0-20190807092412-93f771451dae diff --git a/go.sum b/go.sum index bcb593c..1193487 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uP github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/kmoe/go-list v0.0.1 h1:hKNir14OZh/LBpw1EQNvy8LeCtlKidF00Bigly1h4ec= +github.com/kmoe/go-list v0.0.1/go.mod h1:8bBhPyBnH3wJago3lVPOKvxA+KDmvAHFgLLmhpiQORE= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=