Refactor flags, add run() for better extensibility (#115)
This commit is contained in:
parent
0e0395200a
commit
fc1865052c
75
main.go
75
main.go
@ -13,54 +13,65 @@ import (
|
|||||||
"github.com/matryer/moq/pkg/moq"
|
"github.com/matryer/moq/pkg/moq"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type userFlags struct {
|
||||||
var err error
|
outFile string
|
||||||
defer func() {
|
pkgName string
|
||||||
if err != nil {
|
args []string
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
flag.Usage()
|
|
||||||
os.Exit(1)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
var (
|
func main() {
|
||||||
outFile = flag.String("out", "", "output file (default stdout)")
|
var flags userFlags
|
||||||
pkgName = flag.String("pkg", "", "package name (default will infer)")
|
flag.StringVar(&flags.outFile, "out", "", "output file (default stdout)")
|
||||||
)
|
flag.StringVar(&flags.pkgName, "pkg", "", "package name (default will infer)")
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Println(`moq [flags] destination interface [interface2 [interface3 [...]]]`)
|
fmt.Println(`moq [flags] destination interface [interface2 [interface3 [...]]]`)
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
fmt.Println(`Specifying an alias for the mock is also supported with the format 'interface:alias'`)
|
fmt.Println(`Specifying an alias for the mock is also supported with the format 'interface:alias'`)
|
||||||
fmt.Println(`Ex: moq -pkg different . MyInterface:MyMock`)
|
fmt.Println(`Ex: moq -pkg different . MyInterface:MyMock`)
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
flags.args = flag.Args()
|
||||||
if len(args) < 2 {
|
|
||||||
err = errors.New("not enough arguments")
|
if err := run(flags); err != nil {
|
||||||
return
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
destination := args[0]
|
}
|
||||||
args = args[1:]
|
|
||||||
|
func run(flags userFlags) error {
|
||||||
|
if len(flags.args) < 2 {
|
||||||
|
return errors.New("not enough arguments")
|
||||||
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var out io.Writer
|
var out io.Writer = os.Stdout
|
||||||
out = os.Stdout
|
if flags.outFile != "" {
|
||||||
if len(*outFile) > 0 {
|
|
||||||
out = &buf
|
out = &buf
|
||||||
}
|
}
|
||||||
m, err := moq.New(destination, *pkgName)
|
|
||||||
|
destination := flags.args[0]
|
||||||
|
args := flags.args[1:]
|
||||||
|
m, err := moq.New(destination, flags.pkgName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
err = m.Mock(out, args...)
|
|
||||||
if err != nil {
|
if err = m.Mock(out, args...); err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flags.outFile == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// create the file
|
// create the file
|
||||||
if len(*outFile) > 0 {
|
err = os.MkdirAll(filepath.Dir(flags.outFile), 0755)
|
||||||
err = os.MkdirAll(filepath.Dir(*outFile), 0755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
|
||||||
err = ioutil.WriteFile(*outFile, buf.Bytes(), 0644)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(flags.outFile, buf.Bytes(), 0644)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user