fleshed out main tool
This commit is contained in:
parent
37723c8bd4
commit
3dc5361304
51
main.go
51
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...)
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user