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"
|
2018-06-04 14:22:28 +02:00
|
|
|
"errors"
|
2016-09-21 22:49:12 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2016-09-21 23:02:49 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-09-21 22:49:12 +02:00
|
|
|
"os"
|
2019-11-03 16:12:56 +01:00
|
|
|
"path/filepath"
|
2016-09-21 22:49:12 +02:00
|
|
|
|
2017-07-11 21:02:46 +02:00
|
|
|
"github.com/matryer/moq/pkg/moq"
|
2016-09-21 22:49:12 +02:00
|
|
|
)
|
|
|
|
|
2019-12-23 16:10:26 +01:00
|
|
|
type userFlags struct {
|
2020-03-10 14:08:14 +01:00
|
|
|
outFile string
|
|
|
|
pkgName string
|
|
|
|
formatter string
|
|
|
|
args []string
|
2019-12-23 16:10:26 +01:00
|
|
|
}
|
|
|
|
|
2016-08-29 14:00:18 +02:00
|
|
|
func main() {
|
2019-12-23 16:10:26 +01:00
|
|
|
var flags userFlags
|
|
|
|
flag.StringVar(&flags.outFile, "out", "", "output file (default stdout)")
|
|
|
|
flag.StringVar(&flags.pkgName, "pkg", "", "package name (default will infer)")
|
2020-03-10 14:08:14 +01:00
|
|
|
flag.StringVar(&flags.formatter, "fmt", "", "go pretty-printer: gofmt (default) or goimports")
|
2016-10-14 11:57:57 +02:00
|
|
|
|
2016-09-21 22:49:12 +02:00
|
|
|
flag.Usage = func() {
|
2020-03-10 14:08:14 +01:00
|
|
|
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
|
2016-09-21 22:49:12 +02:00
|
|
|
flag.PrintDefaults()
|
2019-11-05 08:43:49 +01:00
|
|
|
fmt.Println(`Specifying an alias for the mock is also supported with the format 'interface:alias'`)
|
|
|
|
fmt.Println(`Ex: moq -pkg different . MyInterface:MyMock`)
|
2016-09-21 22:49:12 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
|
2016-09-21 22:49:12 +02:00
|
|
|
flag.Parse()
|
2019-12-23 16:10:26 +01:00
|
|
|
flags.args = flag.Args()
|
|
|
|
|
|
|
|
if err := run(flags); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
2016-09-21 22:49:12 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func run(flags userFlags) error {
|
|
|
|
if len(flags.args) < 2 {
|
|
|
|
return errors.New("not enough arguments")
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:02:49 +02:00
|
|
|
var buf bytes.Buffer
|
2019-12-23 16:10:26 +01:00
|
|
|
var out io.Writer = os.Stdout
|
|
|
|
if flags.outFile != "" {
|
2016-09-21 23:02:49 +02:00
|
|
|
out = &buf
|
2016-09-21 22:49:12 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
|
2020-03-10 14:08:14 +01:00
|
|
|
srcDir, args := flags.args[0], flags.args[1:]
|
|
|
|
m, err := moq.New(moq.Config{
|
|
|
|
SrcDir: srcDir,
|
|
|
|
PkgName: flags.pkgName,
|
|
|
|
Formatter: flags.formatter,
|
|
|
|
})
|
2016-09-21 22:49:12 +02:00
|
|
|
if err != nil {
|
2019-12-23 16:10:26 +01:00
|
|
|
return err
|
2016-09-21 22:49:12 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
|
|
|
|
if err = m.Mock(out, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.outFile == "" {
|
|
|
|
return nil
|
2016-09-21 23:02:49 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
|
2016-09-21 23:02:49 +02:00
|
|
|
// create the file
|
2019-12-23 16:10:26 +01:00
|
|
|
err = os.MkdirAll(filepath.Dir(flags.outFile), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-21 23:02:49 +02:00
|
|
|
}
|
2019-12-23 16:10:26 +01:00
|
|
|
|
|
|
|
return ioutil.WriteFile(flags.outFile, buf.Bytes(), 0644)
|
2016-08-29 14:00:18 +02:00
|
|
|
}
|