github.com/oNaiPs/go-generate-fast@v0.3.0/src/plugins/moq/moq.go (about)

     1  package plugin_moq
     2  
     3  import (
     4  	"flag"
     5  
     6  	"github.com/oNaiPs/go-generate-fast/src/plugins"
     7  	"github.com/oNaiPs/go-generate-fast/src/utils/pkg"
     8  	"go.uber.org/zap"
     9  )
    10  
    11  type MoqPlugin struct {
    12  	plugins.Plugin
    13  }
    14  
    15  func (p *MoqPlugin) Name() string {
    16  	return "moq"
    17  }
    18  
    19  func (p *MoqPlugin) Matches(opts plugins.GenerateOpts) bool {
    20  	return opts.ExecutableName == "moq" ||
    21  		opts.GoPackage == "github.com/matryer/moq"
    22  }
    23  
    24  type userFlags struct {
    25  	outFile    string
    26  	pkgName    string
    27  	formatter  string
    28  	stubImpl   bool
    29  	skipEnsure bool
    30  	withResets bool
    31  	remove     bool
    32  	args       []string
    33  }
    34  
    35  func (p *MoqPlugin) ComputeInputOutputFiles(opts plugins.GenerateOpts) *plugins.InputOutputFiles {
    36  	flag := flag.NewFlagSet("mockgen", flag.ContinueOnError)
    37  
    38  	var flags userFlags
    39  	flag.StringVar(&flags.outFile, "out", "", "output file (default stdout)")
    40  	flag.StringVar(&flags.pkgName, "pkg", "", "package name (default will infer)")
    41  	flag.StringVar(&flags.formatter, "fmt", "", "go pretty-printer: gofmt, goimports or noop (default gofmt)")
    42  	flag.BoolVar(&flags.stubImpl, "stub", false,
    43  		"return zero values when no mock implementation is provided, do not panic")
    44  	printVersion := flag.Bool("version", false, "show the version for moq")
    45  	flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
    46  		"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
    47  	flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists")
    48  	flag.BoolVar(&flags.withResets, "with-resets", false,
    49  		"generate functions to facilitate resetting calls made to a mock")
    50  
    51  	err := flag.Parse(opts.SanitizedArgs)
    52  	if err != nil {
    53  		zap.S().Debug("cannot parse args")
    54  		return nil
    55  	}
    56  	flags.args = flag.Args()
    57  
    58  	if *printVersion {
    59  		return nil
    60  	}
    61  
    62  	if flags.outFile == "" {
    63  		zap.S().Error("unsupported stdout mode")
    64  		return nil
    65  	}
    66  
    67  	ioFiles := plugins.InputOutputFiles{}
    68  
    69  	pkg := pkg.LoadPackages(flags.args[0], []string{}, []string{})
    70  	if pkg == nil {
    71  		//did not find package matches
    72  		return nil
    73  	} else if len(pkg.Errors) > 0 {
    74  		zap.S().Debug("Got errors parsing packages: %s", pkg.Errors)
    75  		return nil
    76  	}
    77  
    78  	ioFiles.InputFiles = append(ioFiles.InputFiles, pkg.CompiledGoFiles...)
    79  
    80  	ioFiles.OutputFiles = append(ioFiles.OutputFiles, flags.outFile)
    81  
    82  	return &ioFiles
    83  }
    84  
    85  func init() {
    86  	plugins.RegisterPlugin(&MoqPlugin{})
    87  }