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 package main
import ( import (
"bytes"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil"
"os" "os"
"github.com/matryer/moq/package/moq" "github.com/matryer/moq/package/moq"
@ -34,17 +37,22 @@ func main() {
} }
destination := args[0] destination := args[0]
args = args[1:] args = args[1:]
out := os.Stdout var buf bytes.Buffer
var out io.Writer
out = os.Stdout
if len(*outFile) > 0 { if len(*outFile) > 0 {
out, err = os.Create(*outFile) out = &buf
if err != nil {
return
}
defer out.Close()
} }
m, err := moq.New(destination, *pkgName) m, err := moq.New(destination, *pkgName)
if err != nil { if err != nil {
return return
} }
err = m.Mock(out, args...) err = m.Mock(out, args...)
if err != nil {
return
}
// create the file
if len(*outFile) > 0 {
err = ioutil.WriteFile(*outFile, buf.Bytes(), 0777)
}
} }