diff --git a/generate/generate.go b/generate/generate.go new file mode 100644 index 0000000..1873ed4 --- /dev/null +++ b/generate/generate.go @@ -0,0 +1,9 @@ +package generate + +//go:generate moq -out generated.go . MyInterface + +type MyInterface interface { + One() bool + Two() int + Three() string +} diff --git a/main.go b/main.go index 51fbbea..1d35af0 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "bytes" "errors" "flag" "fmt" + "io" + "io/ioutil" "os" "github.com/matryer/moq/package/moq" @@ -34,17 +37,22 @@ func main() { } destination := args[0] args = args[1:] - out := os.Stdout + var buf bytes.Buffer + var out io.Writer + out = os.Stdout if len(*outFile) > 0 { - out, err = os.Create(*outFile) - if err != nil { - return - } - defer out.Close() + out = &buf } m, err := moq.New(destination, *pkgName) if err != nil { return } err = m.Mock(out, args...) + if err != nil { + return + } + // create the file + if len(*outFile) > 0 { + err = ioutil.WriteFile(*outFile, buf.Bytes(), 0777) + } }