Skip to content

Commit

Permalink
Merge pull request #31 from markus-wa/flags-backwards-compatible
Browse files Browse the repository at this point in the history
cli: use jessevdk/go-flags for parsing options
  • Loading branch information
vburenin authored Dec 5, 2018
2 parents bcfefec + 1116a20 commit 08359b1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 56 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ Here is the help output of ifacemaker:

```
$ ifacemaker --help
Options:
-h, --help display help information
-f, --file *Go source file to read
-s, --struct *Generate an interface for this structure name
-i, --iface *Name of the generated interface
-p, --pkg *Package name for the generated interface
-y, --iface-comment Comment for the interface, default is '// <iface> ...'
-d, --doc[=true] Copy docs from methods
-D, --type-doc Copy type doc from struct
-c, --comment Append comment to top
-o, --output Output file name. If not provided, result will be printed to stdout.
Usage:
ifacemaker [OPTIONS]
Application Options:
-f, --file= Go source file to read
-s, --struct= Generate an interface for this structure name
-i, --iface= Name of the generated interface
-p, --pkg= Package name for the generated interface
-y, --iface-comment= Comment for the interface, default is '// <iface> ...'
-d, --doc= Copy docs from methods (default: true)
-D, --type-doc Copy type doc from struct
-c, --comment= Append comment to top
-o, --output= Output file name. If not provided, result will be printed to stdout.
Help Options:
-h, --help Show this help message
$
```

Expand Down
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
module github.com/vburenin/ifacemaker

require (
github.com/Bowery/prompt v0.0.0-20180817134258-8a1d5376df1c // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/labstack/gommon v0.2.8 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mkideal/cli v0.0.2
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc // indirect
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 // indirect
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 // indirect
golang.org/x/tools v0.0.0-20181201035826-d0ca3933b724
)
20 changes: 4 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
github.com/Bowery/prompt v0.0.0-20180817134258-8a1d5376df1c h1:fAMg70P5ydy1uiIj6CdA69h6nmQKbv18VlVOXhKNrcM=
github.com/Bowery/prompt v0.0.0-20180817134258-8a1d5376df1c/go.mod h1:4/6eNcqZ09BZ9wLK3tZOjBA1nDj+B0728nlX5YRlSmQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0=
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mkideal/cli v0.0.2 h1:B46XEaWqNVwh2P+Mje3iG4W+C2x33KwDhNJlhpWbweg=
github.com/mkideal/cli v0.0.2/go.mod h1:HLuSls75T7LFlTgByGeuLwcvdUmmx/aUQxnnEKxoZzY=
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc h1:eyN9UWVX+CeeCQZPudCUAPc84xQYTjEu9MWNa2HuJrs=
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc/go.mod h1:DECgB56amjU/mmmsKuooNPQ1856HASOMC3D4ntSVU70=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 h1:dAOsPLhnBzIyxu0VvmnKjlNcIlgMK+erD6VRHDtweMI=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20181201035826-d0ca3933b724 h1:eV9myT/I6o1p8salzgZ0f1pz54PEgUf2NkCxEf6t+xs=
golang.org/x/tools v0.0.0-20181201035826-d0ca3933b724/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
55 changes: 35 additions & 20 deletions ifacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/mkideal/cli"
flags "github.com/jessevdk/go-flags"
"github.com/vburenin/ifacemaker/maker"
)

type cmdlineArgs struct {
cli.Helper
Files []string `cli:"*f,file" usage:"Go source file to read"`
StructType string `cli:"*s,struct" usage:"Generate an interface for this structure name"`
IfaceName string `cli:"*i,iface" usage:"Name of the generated interface"`
PkgName string `cli:"*p,pkg" usage:"Package name for the generated interface"`
IfaceComment string `cli:"y,iface-comment" usage:"Comment for the interface, default is '// <iface> ...'"`
CopyDocs bool `cli:"d,doc" usage:"Copy docs from methods" dft:"true"`
CopyTypeDoc bool `cli:"D,type-doc" usage:"Copy type doc from struct"`
Comment string `cli:"c,comment" usage:"Append comment to top"`
Output string `cli:"o,output" usage:"Output file name. If not provided, result will be printed to stdout."`
Files []string `short:"f" long:"file" description:"Go source file to read" required:"true"`
StructType string `short:"s" long:"struct" description:"Generate an interface for this structure name" required:"true"`
IfaceName string `short:"i" long:"iface" description:"Name of the generated interface" required:"true"`
PkgName string `short:"p" long:"pkg" description:"Package name for the generated interface" required:"true"`
IfaceComment string `short:"y" long:"iface-comment" description:"Comment for the interface, default is '// <iface> ...'"`

// jessevdk/go-flags doesn't support default values for boolean flags,
// so we use a string for backwards-compatibility and then convert it to a bool later.
CopyDocs string `short:"d" long:"doc" description:"Copy docs from methods" option:"true" option:"false" default:"true"`
copyDocs bool

CopyTypeDoc bool `short:"D" long:"type-doc" description:"Copy type doc from struct"`
Comment string `short:"c" long:"comment" description:"Append comment to top"`
Output string `short:"o" long:"output" description:"Output file name. If not provided, result will be printed to stdout."`
}

func run(args *cmdlineArgs) {
func run(args cmdlineArgs) {
allMethods := []string{}
allImports := []string{}
mset := make(map[string]struct{})
Expand All @@ -33,7 +38,7 @@ func run(args *cmdlineArgs) {
if err != nil {
log.Fatal(err.Error())
}
methods, imports, parsedTypeDoc := maker.ParseStruct(src, args.StructType, args.CopyDocs, args.CopyTypeDoc)
methods, imports, parsedTypeDoc := maker.ParseStruct(src, args.StructType, args.copyDocs, args.CopyTypeDoc)
for _, m := range methods {
if _, ok := mset[m.Code]; !ok {
allMethods = append(allMethods, m.Lines()...)
Expand Down Expand Up @@ -68,12 +73,22 @@ func run(args *cmdlineArgs) {
}

func main() {
cli.Run(&cmdlineArgs{}, func(ctx *cli.Context) error {
argv := ctx.Argv().(*cmdlineArgs)
if argv.IfaceComment == "" {
argv.IfaceComment = fmt.Sprintf("%s ...", argv.IfaceName)
var args cmdlineArgs
_, err := flags.ParseArgs(&args, os.Args)
if err != nil {
if flags.WroteHelp(err) {
return
}
run(argv)
return nil
})
// No need to log the error, flags.ParseArgs() already does this
os.Exit(1)
}

// Workaround because jessevdk/go-flags doesn't support default values for boolean flags
args.copyDocs = args.CopyDocs == "true"

if args.IfaceComment == "" {
args.IfaceComment = fmt.Sprintf("%s ...", args.IfaceName)
}

run(args)
}
27 changes: 27 additions & 0 deletions ifacemaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ type PersonIface interface {
assert.Equal(t, expected, out)
}

func TestMainNoCopyMethodDocs(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-d=false"}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package gen
// PersonIface ...
type PersonIface interface {
Name() string
SetName(name string)
Age() int
SetAge(age int) int
AgeAndName() (int, string)
SetAgeAndName(name string, age int)
GetNameAndTelephone() (name, telephone string)
SetNameAndTelephone(name, telephone string)
}
`

assert.Equal(t, expected, out)
}

// not thread safe
func captureStdout(f func()) string {
old := os.Stdout
Expand Down

0 comments on commit 08359b1

Please sign in to comment.