diff --git a/README.md b/README.md index cdfd7f2..86009b8 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Usage: ifacemaker [OPTIONS] Application Options: - -f, --file= Go source file to read + -f, --file= Go source file to read, either filename or glob -s, --struct= Generate an interface for this structure name -i, --iface= Name of the generated interface -p, --pkg= Package name for the generated interface diff --git a/ifacemaker.go b/ifacemaker.go index b7e9c55..9ee8d03 100644 --- a/ifacemaker.go +++ b/ifacemaker.go @@ -5,13 +5,14 @@ import ( "io/ioutil" "log" "os" + "path/filepath" - flags "github.com/jessevdk/go-flags" + "github.com/jessevdk/go-flags" "github.com/vburenin/ifacemaker/maker" ) type cmdlineArgs struct { - Files []string `short:"f" long:"file" description:"Go source file to read" required:"true"` + Files []string `short:"f" long:"file" description:"Go source file to read, either filename or glob" 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"` @@ -44,8 +45,15 @@ func main() { if args.IfaceComment == "" { args.IfaceComment = fmt.Sprintf("%s ...", args.IfaceName) } - - result, err := maker.Make(args.Files, args.StructType, args.Comment, args.PkgName, args.IfaceName, args.IfaceComment, args.copyDocs, args.CopyTypeDoc) + var files []string + for _, filePattern := range args.Files { + matches, err := filepath.Glob(filePattern) + if err != nil { + log.Fatal(err) + } + files = append(files, matches...) + } + result, err := maker.Make(files, args.StructType, args.Comment, args.PkgName, args.IfaceName, args.IfaceComment, args.copyDocs, args.CopyTypeDoc) if err != nil { log.Fatal(err.Error()) } diff --git a/ifacemaker_test.go b/ifacemaker_test.go index 4a26496..e65d82d 100644 --- a/ifacemaker_test.go +++ b/ifacemaker_test.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "strings" "testing" assert "github.com/stretchr/testify/assert" @@ -89,6 +90,15 @@ func (s *TestImpl) fooHelper() string { return "" }` +var src2_extend = `package maker +import ( + "github.com/vburenin/ifacemaker/maker/footest" +) + +func (s *TestImpl) UpdateUser(userID string) *footest.User { + return &footest.User{}, nil +} +` var src3 = `package footest type User struct { @@ -98,6 +108,7 @@ type User struct { var srcFile = os.TempDir() + "/ifacemaker_src.go" var srcFile2 = os.TempDir() + "/test_impl.go" +var srcFile2_ext = os.TempDir() + "/test_impl_extended.go" var srcFile3 = os.TempDir() + "/footest/footest.go" func TestMain(m *testing.M) { @@ -110,6 +121,7 @@ func TestMain(m *testing.M) { } writeTestSourceFile(src, srcFile) writeTestSourceFile(src2, srcFile2) + writeTestSourceFile(src2_extend, srcFile2_ext) writeTestSourceFile(src3, srcFile3) os.Exit(m.Run()) @@ -276,6 +288,32 @@ type TestInterface interface { assert.Equal(t, expected, out) } +func TestMainFileGlob(t *testing.T) { + src := strings.Replace(srcFile2, "test_impl.go", "test*.go", 1) + //assert.True(t, strings.HasSuffix(srcFile2, "test*.go")) + //assert.Equal(t, "bla", src) + //ssert.Contains(t, src, "test*") + os.Args = []string{"cmd", "-f", src, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"} + out := captureStdout(func() { + main() + }) + + expected := `// DO NOT EDIT: Auto generated + +package footest + +// TestInterface ... +type TestInterface interface { + GetUser(userID string) *User + CreateUser(user *User) (*User, error) + UpdateUser(userID string) *User +} + +` + + assert.Equal(t, expected, out) +} + // not thread safe func captureStdout(f func()) string { old := os.Stdout