Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
extract 'go list' call to kmoe/go-list package
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Aug 29, 2019
1 parent 4e0d052 commit 429c383
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 59 deletions.
62 changes: 3 additions & 59 deletions cmd/check/sdk_refs.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -107,42 +77,16 @@ 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
}

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
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 429c383

Please sign in to comment.