Skip to content

Commit 258cf2d

Browse files
authored
Merge pull request #31 from utgwkk/use-go-tool
Support `go tool` command and deprecate `-use_go_run` argument
2 parents 30276d9 + 5e8eaca commit 258cf2d

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ var Iset = []any{
3131
```
3232

3333
```
34-
$ bulkmockgen -use_go_run Iset -- -package mock_foo -destination ./mock_foo/mock.go
34+
$ bulkmockgen -exec_mode go_run Iset -- -package mock_foo -destination ./mock_foo/mock.go
3535
```
3636

3737
You can use bulkmockgen with `go:generate` comment.
3838

3939
```go
4040
package foo
4141

42-
//go:generate bulkmockgen -use_go_run Iset -- -package mock_foo -destination ./mock_foo/mock.go
42+
//go:generate bulkmockgen -exec_mode go_run Iset -- -package mock_foo -destination ./mock_foo/mock.go
4343

4444
var Iset = []any{
4545
new(IFoo),

cmd/bulkmockgen/main.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import (
1414
)
1515

1616
var (
17-
useGoRun = flag.Bool("use_go_run", false, "Whether to use go run command to execute mockgen; defaults to false")
18-
dryRun = flag.Bool("dry_run", false, "Print command to be executed and exit")
17+
useGoRun = flag.Bool("use_go_run", false, "(Deprecated) Whether to use go run command to execute mockgen; defaults to false")
18+
19+
flagExecMode = flag.String("exec_mode", "direct", "How to execute mockgen. Supported values are: direct, go_run, go_tool")
20+
21+
dryRun = flag.Bool("dry_run", false, "Print command to be executed and exit")
1922
)
2023

2124
func main() {
@@ -32,8 +35,23 @@ func main() {
3235
log.Fatal("rest separator (--) is required")
3336
}
3437

38+
execMode := generator.ExecModeDirect
39+
if *useGoRun {
40+
execMode = generator.ExecModeGoRun
41+
} else {
42+
switch *flagExecMode {
43+
case "direct":
44+
execMode = generator.ExecModeDirect
45+
case "go_run":
46+
execMode = generator.ExecModeGoRun
47+
case "go_tool":
48+
execMode = generator.ExecModeGoTool
49+
default:
50+
log.Fatalf("unsupported exec mode (%s)", *flagExecMode)
51+
}
52+
}
3553
g := &generator.Generator{
36-
UseGoRun: *useGoRun,
54+
ExecMode: execMode,
3755
DryRun: *dryRun,
3856
MockSetName: mockSetName,
3957
RestArgs: args[restSeparatorIdx+1:],

generator/generator.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,33 @@ const (
1818
mockgenPackage = "go.uber.org/mock/mockgen"
1919
)
2020

21+
type ExecMode int
22+
23+
const (
24+
ExecModeDirect ExecMode = iota + 1
25+
26+
ExecModeGoRun
27+
28+
ExecModeGoTool
29+
)
30+
31+
func (e ExecMode) Command() (string, []string) {
32+
switch e {
33+
case ExecModeDirect:
34+
return "mockgen", []string{}
35+
case ExecModeGoRun:
36+
return "go", []string{"run", mockgenPackage}
37+
case ExecModeGoTool:
38+
return "go", []string{"tool", mockgenPackage}
39+
default:
40+
panic("unsupported exec mode")
41+
}
42+
}
43+
2144
type runnerFactory func(ctx context.Context, cmdExecutable string, cmdArgs ...string) Runner
2245

2346
type Generator struct {
24-
UseGoRun bool
47+
ExecMode ExecMode
2548
DryRun bool
2649
MockSetName string
2750
RestArgs []string
@@ -33,14 +56,7 @@ func (g *Generator) Generate(ctx context.Context, rf runnerFactory) error {
3356
return err
3457
}
3558

36-
var cmdExecutable string
37-
var cmdArgs []string
38-
if g.UseGoRun {
39-
cmdExecutable = "go"
40-
cmdArgs = append(cmdArgs, "run", mockgenPackage)
41-
} else {
42-
cmdExecutable = "mockgen"
43-
}
59+
cmdExecutable, cmdArgs := g.ExecMode.Command()
4460
cmdArgs = append(cmdArgs, g.RestArgs...)
4561
if externalPkg != "" {
4662
cmdArgs = append(cmdArgs, externalPkg)
@@ -100,7 +116,7 @@ func (g *Generator) findMockSetFromDirectory(sourceDir string) ([]string, string
100116
}
101117

102118
splittedImpPath := strings.Split(impPath, "/")
103-
if externalPkg == splittedImpPath[len(splittedImpPath) - 1] {
119+
if externalPkg == splittedImpPath[len(splittedImpPath)-1] {
104120
externalPkg = impPath
105121
break
106122
}

generator/generator_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestGenerate(t *testing.T) {
3535
name: "basic",
3636
baseDir: "fixtures/multifile",
3737
g: &Generator{
38-
UseGoRun: false,
38+
ExecMode: ExecModeDirect,
3939
DryRun: false,
4040
MockSetName: "MockInterfaces",
4141
RestArgs: []string{"-package", "mock_multifile", "-destination", "mock_multifile/mock.go"},
@@ -47,19 +47,31 @@ func TestGenerate(t *testing.T) {
4747
name: "with go run",
4848
baseDir: "fixtures/multifile",
4949
g: &Generator{
50-
UseGoRun: true,
50+
ExecMode: ExecModeGoRun,
5151
DryRun: false,
5252
MockSetName: "MockInterfaces",
5353
RestArgs: []string{"-package", "mock_multifile", "-destination", "mock_multifile/mock.go"},
5454
},
5555
wantCmdExecutable: "go",
5656
wantCmdArgs: []string{"run", "go.uber.org/mock/mockgen", "-package", "mock_multifile", "-destination", "mock_multifile/mock.go", ".", "IFoo,IBar"},
5757
},
58+
{
59+
name: "with go tool",
60+
baseDir: "fixtures/multifile",
61+
g: &Generator{
62+
ExecMode: ExecModeGoTool,
63+
DryRun: false,
64+
MockSetName: "MockInterfaces",
65+
RestArgs: []string{"-package", "mock_multifile", "-destination", "mock_multifile/mock.go"},
66+
},
67+
wantCmdExecutable: "go",
68+
wantCmdArgs: []string{"tool", "go.uber.org/mock/mockgen", "-package", "mock_multifile", "-destination", "mock_multifile/mock.go", ".", "IFoo,IBar"},
69+
},
5870
{
5971
name: "with external package",
6072
baseDir: "fixtures/external",
6173
g: &Generator{
62-
UseGoRun: false,
74+
ExecMode: ExecModeDirect,
6375
DryRun: false,
6476
MockSetName: "MockInterfaces",
6577
RestArgs: []string{"-package", "mock_sql", "-destination", "mock_sql/mock.go"},

0 commit comments

Comments
 (0)