From 3dc536130452f38c9ff639ddde27b788eddde0e7 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Wed, 21 Sep 2016 21:49:12 +0100 Subject: [PATCH] fleshed out main tool --- main.go | 51 +++++++++++++++++++++++++++++++++++++++++----- package/moq/moq.go | 7 +++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 6326e9e..7b6dc44 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,50 @@ package main +import ( + "errors" + "flag" + "fmt" + "os" + + "github.com/matryer/moq/package/moq" +) + func main() { - // var ( - // ) - // out := os.Stdout - // m := moq.New(".") - // m.Mock(out, os.Args...) + var err error + defer func() { + if err != nil { + fmt.Println(err) + 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:] + out := os.Stdout + if len(*outFile) > 0 { + out, err = os.Create(*outFile) + if err != nil { + return + } + defer out.Close() + } + m, err := moq.New(destination, *pkgName) + if err != nil { + return + } + err = m.Mock(out, args...) } diff --git a/package/moq/moq.go b/package/moq/moq.go index 5bf4e66..f36a3f2 100644 --- a/package/moq/moq.go +++ b/package/moq/moq.go @@ -60,6 +60,9 @@ func New(src, packageName string) (*Mocker, error) { // Mock generates a mock for the specified interface name. func (m *Mocker) Mock(w io.Writer, name ...string) error { + if len(name) == 0 { + return errors.New("must specify one interface") + } var objs []*obj for _, pkg := range m.pkgs { i := 0 @@ -75,6 +78,10 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { } for _, n := range name { iface := tpkg.Scope().Lookup(n) + if iface == nil { + return fmt.Errorf("cannot find interface %s", n) + continue + } if !types.IsInterface(iface.Type()) { return fmt.Errorf("%s (%s) not an interface", n, iface.Type().String()) }