Skip to content

Commit 0a357f2

Browse files
committed
feat: support go tool invocation in write directive
The new -write_tool_generate_directive works the same way as the -write_generate_directive flag, except that it correctly regenerates "go tool mockgen" invocations of the mockgen tool.
1 parent df17445 commit 0a357f2

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

mockgen/mockgen.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,26 @@ var (
5454
)
5555

5656
var (
57-
archive = flag.String("archive", "", "(archive mode) Input Go archive file; enables archive mode.")
58-
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
59-
destination = flag.String("destination", "", "Output file; defaults to stdout.")
60-
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
61-
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
62-
selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
63-
writeCmdComment = flag.Bool("write_command_comment", true, "Writes the command used as a comment if true.")
64-
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
65-
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (package mode) comment if true.")
66-
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
67-
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
68-
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
69-
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
70-
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
71-
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
72-
modelGob = flag.String("model_gob", "", "Skip package/source loading entirely and use the gob encoded model.Package at the given path")
73-
excludeInterfaces = flag.String("exclude_interfaces", "", "Comma-separated names of interfaces to be excluded")
74-
debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
75-
showVersion = flag.Bool("version", false, "Print version.")
57+
archive = flag.String("archive", "", "(archive mode) Input Go archive file; enables archive mode.")
58+
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
59+
destination = flag.String("destination", "", "Output file; defaults to stdout.")
60+
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
61+
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
62+
selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
63+
writeCmdComment = flag.Bool("write_command_comment", true, "Writes the command used as a comment if true.")
64+
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
65+
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (package mode) comment if true.")
66+
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
67+
writeToolGenerateDirective = flag.Bool("write_tool_generate_directive", false, "Add //go:generate directive to regenerate the mock, for indirect \"go tool mockgen\" invocation")
68+
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
69+
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
70+
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
71+
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
72+
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
73+
modelGob = flag.String("model_gob", "", "Skip package/source loading entirely and use the gob encoded model.Package at the given path")
74+
excludeInterfaces = flag.String("exclude_interfaces", "", "Comma-separated names of interfaces to be excluded")
75+
debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
76+
showVersion = flag.Bool("version", false, "Print version.")
7677
)
7778

7879
func main() {
@@ -462,8 +463,14 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
462463
g.out()
463464
g.p(")")
464465

465-
if *writeGenerateDirective {
466+
switch {
467+
case *writeGenerateDirective && *writeToolGenerateDirective:
468+
return fmt.Errorf("write_generate_directive and write_tool_generate_directive are mutually exclusive")
469+
case *writeGenerateDirective:
466470
g.p("//go:generate %v", strings.Join(os.Args, " "))
471+
case *writeToolGenerateDirective:
472+
g.p("//go:generate %v",
473+
fmt.Sprintf("go tool %s %s", path.Base(os.Args[0]), strings.Join(os.Args[1:], " ")))
467474
}
468475

469476
for _, intf := range pkg.Interfaces {

0 commit comments

Comments
 (0)