moq/main.go

59 lines
1019 B
Go
Raw Normal View History

2016-08-29 14:00:18 +02:00
package main
2016-09-21 22:49:12 +02:00
import (
2016-09-21 23:02:49 +02:00
"bytes"
2016-09-21 22:49:12 +02:00
"errors"
"flag"
"fmt"
2016-09-21 23:02:49 +02:00
"io"
"io/ioutil"
2016-09-21 22:49:12 +02:00
"os"
"github.com/matryer/moq/package/moq"
)
2016-08-29 14:00:18 +02:00
func main() {
2016-09-21 22:49:12 +02:00
var err error
defer func() {
if err != nil {
2016-09-21 22:55:13 +02:00
fmt.Fprintln(os.Stderr, err)
2016-09-21 22:49:12 +02:00
flag.Usage()
os.Exit(1)
}
}()
var (
outFile = flag.String("out", "", "output file (default stdout)")
pkgName = flag.String("pkg", "", "package name (default will infer)")
)
flag.Usage = func() {
fmt.Println(`moq [flags] destination interface [interface2 [interface3 [...]]]`)
flag.PrintDefaults()
}
flag.Parse()
args := flag.Args()
if len(args) < 2 {
err = errors.New("not enough arguments")
return
}
destination := args[0]
args = args[1:]
2016-09-21 23:02:49 +02:00
var buf bytes.Buffer
var out io.Writer
out = os.Stdout
2016-09-21 22:49:12 +02:00
if len(*outFile) > 0 {
2016-09-21 23:02:49 +02:00
out = &buf
2016-09-21 22:49:12 +02:00
}
m, err := moq.New(destination, *pkgName)
if err != nil {
return
}
err = m.Mock(out, args...)
2016-09-21 23:02:49 +02:00
if err != nil {
return
}
// create the file
if len(*outFile) > 0 {
err = ioutil.WriteFile(*outFile, buf.Bytes(), 0777)
}
2016-08-29 14:00:18 +02:00
}