Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 24, 2022
1 parent 983d889 commit 1ec2d10
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 55 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ require (
github.com/ettle/strcase v0.1.1
github.com/hexops/gotextdiff v1.0.3
github.com/stretchr/testify v1.7.4
golang.org/x/mod v0.5.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
37 changes: 13 additions & 24 deletions mocktail.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,19 @@ type InterfaceDesc struct {
}

func main() {
modulePath, err := getModulePath(os.Getenv("MOCKTAIL_TEST_PATH"))
info, err := getModuleInfo(os.Getenv("MOCKTAIL_TEST_PATH"))
if err != nil {
log.Fatal("get module path", err)
}

moduleName, err := getModuleName(modulePath)
if err != nil {
log.Fatal("get module name", err)
}

root := filepath.Dir(modulePath)

// moduleName: github.com/traefik/mocktail
// modulePath: /home/ldez/go/src/github.com/traefik/mocktail/go.mod
// root: /home/ldez/go/src/github.com/traefik/mocktail
root := info.Dir

err = os.Chdir(root)
if err != nil {
log.Fatalf("Chdir: %v", err)
}

model, err := walk(root, moduleName)
model, err := walk(root, info.Path)
if err != nil {
log.Fatalf("walk: %v", err)
}
Expand Down Expand Up @@ -120,21 +111,19 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) {

interfaceName := line[i+len(commentTagPattern):]

filePkgName, err := filepath.Rel(root, filepath.Dir(fp))
if err != nil {
return err
}

var pkgName string
var importPath string
if index := strings.LastIndex(interfaceName, "."); index > 0 {
pkgName = interfaceName[:index]
importPath = path.Join(moduleName, interfaceName[:index])

interfaceName = interfaceName[index+1:]
} else {
pkgName = filePkgName
}
filePkgName, err := filepath.Rel(root, filepath.Dir(fp))
if err != nil {
return err
}

importPathFile := path.Join(moduleName, filePkgName)
importPath := path.Join(moduleName, pkgName)
importPath = path.Join(moduleName, filePkgName)
}

pkg, err := importR.Import(importPath)
if err != nil {
Expand All @@ -161,7 +150,7 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) {

interfaceDesc.Methods = append(interfaceDesc.Methods, method)

for _, imp := range getMethodImports(method, importPathFile) {
for _, imp := range getMethodImports(method, packageDesc.PkgPath) {
packageDesc.Imports[imp] = struct{}{}
}
}
Expand Down
33 changes: 8 additions & 25 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"

"golang.org/x/mod/modfile"
)

type modInfo struct {
Path string `json:"Path"`
Dir string `json:"Dir"`
GoMod string `json:"GoMod"`
Path string `json:"Path"` // module name
Dir string `json:"Dir"` // absolute path to the module
GoMod string `json:"GoMod"` // absolute path to the go.mod
GoVersion string `json:"GoVersion"`
Main bool `json:"Main"`
}

func getModulePath(dir string) (string, error) {
func getModuleInfo(dir string) (modInfo, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")
if dir != "" {
Expand All @@ -28,32 +25,18 @@ func getModulePath(dir string) (string, error) {

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

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

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

return v.GoMod, nil
}

func getModuleName(modulePath string) (string, error) {
raw, err := os.ReadFile(modulePath)
if err != nil {
return "", fmt.Errorf("reading go.mod file: %w", err)
}

modData, err := modfile.Parse("go.mod", raw, nil)
if err != nil {
return "", fmt.Errorf("parsing go.mod file: %w", err)
return modInfo{}, errors.New("working directory is not part of a module")
}

return modData.Module.Mod.String(), nil
return v, nil
}

0 comments on commit 1ec2d10

Please sign in to comment.