5454)
5555
5656var (
57+ archive = flag .String ("archive" , "" , "(archive mode) Input Go archive file; enables archive mode." )
5758 source = flag .String ("source" , "" , "(source mode) Input Go source file; enables source mode." )
5859 destination = flag .String ("destination" , "" , "Output file; defaults to stdout." )
5960 mockNames = flag .String ("mock_names" , "" , "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix." )
@@ -68,11 +69,10 @@ var (
6869 typed = flag .Bool ("typed" , false , "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function" )
6970 imports = flag .String ("imports" , "" , "(source mode) Comma-separated name=path pairs of explicit imports to use." )
7071 auxFiles = flag .String ("aux_files" , "" , "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files." )
71- excludeInterfaces = flag .String ("exclude_interfaces" , "" , "(source mode) Comma-separated names of interfaces to be excluded" )
7272 modelGob = flag .String ("model_gob" , "" , "Skip package/source loading entirely and use the gob encoded model.Package at the given path" )
73-
74- debugParser = flag .Bool ("debug_parser" , false , "Print out parser results only." )
75- showVersion = flag .Bool ("version" , false , "Print version." )
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." )
7676)
7777
7878func main () {
@@ -89,17 +89,24 @@ func main() {
8989 var pkg * model.Package
9090 var err error
9191 var packageName string
92- if * modelGob != "" {
92+
93+ // Switch between modes
94+ switch {
95+ case * modelGob != "" : // gob mode
9396 pkg , err = gobMode (* modelGob )
94- } else if * source != "" {
97+ case * source != "" : // source mode
9598 pkg , err = sourceMode (* source )
96- } else {
97- if flag .NArg () != 2 {
98- usage ()
99- log .Fatal ("Expected exactly two arguments" )
100- }
99+ case * archive != "" : // archive mode
100+ checkArgs ()
101+ packageName = flag .Arg (0 )
102+ interfaces := strings .Split (flag .Arg (1 ), "," )
103+ pkg , err = archiveMode (packageName , interfaces , * archive )
104+
105+ default : // package mode
106+ checkArgs ()
101107 packageName = flag .Arg (0 )
102108 interfaces := strings .Split (flag .Arg (1 ), "," )
109+
103110 if packageName == "." {
104111 dir , err := os .Getwd ()
105112 if err != nil {
@@ -109,10 +116,12 @@ func main() {
109116 if err != nil {
110117 log .Fatalf ("Parse package name failed: %v" , err )
111118 }
119+
112120 }
113121 parser := packageModeParser {}
114122 pkg , err = parser .parsePackage (packageName , interfaces )
115123 }
124+
116125 if err != nil {
117126 log .Fatalf ("Loading input failed: %v" , err )
118127 }
@@ -155,6 +164,8 @@ func main() {
155164 }
156165 if * source != "" {
157166 g .filename = * source
167+ } else if * archive != "" {
168+ g .filename = * archive
158169 } else {
159170 g .srcPackage = packageName
160171 g .srcInterfaces = flag .Arg (1 )
@@ -230,12 +241,19 @@ func parseExcludeInterfaces(names string) map[string]struct{} {
230241 return namesSet
231242}
232243
244+ func checkArgs () {
245+ if flag .NArg () != 2 {
246+ usage ()
247+ log .Fatal ("Expected exactly two arguments" )
248+ }
249+ }
250+
233251func usage () {
234252 _ , _ = io .WriteString (os .Stderr , usageText )
235253 flag .PrintDefaults ()
236254}
237255
238- const usageText = `mockgen has two modes of operation: source and package.
256+ const usageText = `mockgen has three modes of operation: archive, source and package.
239257
240258Source mode generates mock interfaces from a source file.
241259It is enabled by using the -source flag. Other flags that
@@ -245,12 +263,19 @@ Example:
245263
246264Package mode works by specifying the package and interface names.
247265It is enabled by passing two non-flag arguments: an import path, and a
248- comma-separated list of symbols.
266+ comma-separated list of symbols.
249267You can use "." to refer to the current path's package.
250268Example:
251269 mockgen database/sql/driver Conn,Driver
252270 mockgen . SomeInterface
253271
272+ Archive mode generates mock interfaces from a package archive
273+ file (.a). It is enabled by using the -archive flag and two
274+ non-flag arguments: an import path, and a comma-separated
275+ list of symbols.
276+ Example:
277+ mockgen -archive=pkg.a database/sql/driver Conn,Driver
278+
254279`
255280
256281type generator struct {
0 commit comments