Skip to content

Commit

Permalink
Merge pull request #39 from Ntr0/file-globs
Browse files Browse the repository at this point in the history
Extend file option to accept file globs
  • Loading branch information
vburenin authored Aug 2, 2019
2 parents b7104cb + 0763968 commit 29e10f4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions ifacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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())
}
Expand Down
38 changes: 38 additions & 0 deletions ifacemaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"
"testing"

assert "github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 29e10f4

Please sign in to comment.