added go generate example

This commit is contained in:
Mat Ryer 2016-09-21 22:02:49 +01:00
parent 5d872a540b
commit 18033310b9
2 changed files with 23 additions and 6 deletions

9
generate/generate.go Normal file
View File

@ -0,0 +1,9 @@
package generate
//go:generate moq -out generated.go . MyInterface
type MyInterface interface {
One() bool
Two() int
Three() string
}

20
main.go
View File

@ -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)
}
}