Skip to content

Commit 1ec2d10

Browse files
committed
refactor: simplify
1 parent 983d889 commit 1ec2d10

File tree

4 files changed

+21
-55
lines changed

4 files changed

+21
-55
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ require (
66
github.com/ettle/strcase v0.1.1
77
github.com/hexops/gotextdiff v1.0.3
88
github.com/stretchr/testify v1.7.4
9-
golang.org/x/mod v0.5.1
109
)
1110

1211
require (
1312
github.com/davecgh/go-spew v1.1.1 // indirect
1413
github.com/pmezard/go-difflib v1.0.0 // indirect
15-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
1614
gopkg.in/yaml.v3 v3.0.1 // indirect
1715
)

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
1313
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1414
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
1515
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
16-
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
17-
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
18-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
19-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2016
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2117
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2218
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

mocktail.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,19 @@ type InterfaceDesc struct {
4040
}
4141

4242
func main() {
43-
modulePath, err := getModulePath(os.Getenv("MOCKTAIL_TEST_PATH"))
43+
info, err := getModuleInfo(os.Getenv("MOCKTAIL_TEST_PATH"))
4444
if err != nil {
4545
log.Fatal("get module path", err)
4646
}
4747

48-
moduleName, err := getModuleName(modulePath)
49-
if err != nil {
50-
log.Fatal("get module name", err)
51-
}
52-
53-
root := filepath.Dir(modulePath)
54-
55-
// moduleName: github.com/traefik/mocktail
56-
// modulePath: /home/ldez/go/src/github.com/traefik/mocktail/go.mod
57-
// root: /home/ldez/go/src/github.com/traefik/mocktail
48+
root := info.Dir
5849

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

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

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

123-
filePkgName, err := filepath.Rel(root, filepath.Dir(fp))
124-
if err != nil {
125-
return err
126-
}
127-
128-
var pkgName string
114+
var importPath string
129115
if index := strings.LastIndex(interfaceName, "."); index > 0 {
130-
pkgName = interfaceName[:index]
116+
importPath = path.Join(moduleName, interfaceName[:index])
117+
131118
interfaceName = interfaceName[index+1:]
132119
} else {
133-
pkgName = filePkgName
134-
}
120+
filePkgName, err := filepath.Rel(root, filepath.Dir(fp))
121+
if err != nil {
122+
return err
123+
}
135124

136-
importPathFile := path.Join(moduleName, filePkgName)
137-
importPath := path.Join(moduleName, pkgName)
125+
importPath = path.Join(moduleName, filePkgName)
126+
}
138127

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

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

164-
for _, imp := range getMethodImports(method, importPathFile) {
153+
for _, imp := range getMethodImports(method, packageDesc.PkgPath) {
165154
packageDesc.Imports[imp] = struct{}{}
166155
}
167156
}

mod.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"os"
98
"os/exec"
10-
11-
"golang.org/x/mod/modfile"
129
)
1310

1411
type modInfo struct {
15-
Path string `json:"Path"`
16-
Dir string `json:"Dir"`
17-
GoMod string `json:"GoMod"`
12+
Path string `json:"Path"` // module name
13+
Dir string `json:"Dir"` // absolute path to the module
14+
GoMod string `json:"GoMod"` // absolute path to the go.mod
1815
GoVersion string `json:"GoVersion"`
1916
Main bool `json:"Main"`
2017
}
2118

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

2926
raw, err := cmd.CombinedOutput()
3027
if err != nil {
31-
return "", fmt.Errorf("command go list: %w: %s", err, string(raw))
28+
return modInfo{}, fmt.Errorf("command go list: %w: %s", err, string(raw))
3229
}
3330

3431
var v modInfo
3532
err = json.NewDecoder(bytes.NewBuffer(raw)).Decode(&v)
3633
if err != nil {
37-
return "", fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
34+
return modInfo{}, fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
3835
}
3936

4037
if v.GoMod == "" {
41-
return "", errors.New("working directory is not part of a module")
42-
}
43-
44-
return v.GoMod, nil
45-
}
46-
47-
func getModuleName(modulePath string) (string, error) {
48-
raw, err := os.ReadFile(modulePath)
49-
if err != nil {
50-
return "", fmt.Errorf("reading go.mod file: %w", err)
51-
}
52-
53-
modData, err := modfile.Parse("go.mod", raw, nil)
54-
if err != nil {
55-
return "", fmt.Errorf("parsing go.mod file: %w", err)
38+
return modInfo{}, errors.New("working directory is not part of a module")
5639
}
5740

58-
return modData.Module.Mod.String(), nil
41+
return v, nil
5942
}

0 commit comments

Comments
 (0)