Add fmt flag to specify formatter (#117)
- Support formatting output with gofmt(default) or goimports via flag. - Introduce moq.Config struct to configure moq.Mocker instance. This breaks backward compatibility but facilitates it in the future if and when any features are added. - Use golden file tests for validating formatters. Use github.com/pmezard/go-difflib to print the diff between expected and actual.
This commit is contained in:
19
main.go
19
main.go
@@ -14,18 +14,20 @@ import (
|
||||
)
|
||||
|
||||
type userFlags struct {
|
||||
outFile string
|
||||
pkgName string
|
||||
args []string
|
||||
outFile string
|
||||
pkgName string
|
||||
formatter string
|
||||
args []string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var flags userFlags
|
||||
flag.StringVar(&flags.outFile, "out", "", "output file (default stdout)")
|
||||
flag.StringVar(&flags.pkgName, "pkg", "", "package name (default will infer)")
|
||||
flag.StringVar(&flags.formatter, "fmt", "", "go pretty-printer: gofmt (default) or goimports")
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Println(`moq [flags] destination interface [interface2 [interface3 [...]]]`)
|
||||
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
|
||||
flag.PrintDefaults()
|
||||
fmt.Println(`Specifying an alias for the mock is also supported with the format 'interface:alias'`)
|
||||
fmt.Println(`Ex: moq -pkg different . MyInterface:MyMock`)
|
||||
@@ -52,9 +54,12 @@ func run(flags userFlags) error {
|
||||
out = &buf
|
||||
}
|
||||
|
||||
destination := flags.args[0]
|
||||
args := flags.args[1:]
|
||||
m, err := moq.New(destination, flags.pkgName)
|
||||
srcDir, args := flags.args[0], flags.args[1:]
|
||||
m, err := moq.New(moq.Config{
|
||||
SrcDir: srcDir,
|
||||
PkgName: flags.pkgName,
|
||||
Formatter: flags.formatter,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user