-
Notifications
You must be signed in to change notification settings - Fork 43
/
ifacemaker.go
91 lines (80 loc) · 3.19 KB
/
ifacemaker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"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, 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"`
IfaceComment string `short:"y" long:"iface-comment" description:"Comment for the interface, default is '// <iface> ...'"`
ImportModule string `short:"m" long:"import-module" description:"Fully qualified module import for packages with a different target package '// <iface> ...'"`
ExcludeMethods []string `short:"e" long:"exclude-method" description:"Name of method that will be excluded from output interface"`
WithNotExported bool `short:"x" long:"not-exported" description:"Include not exported methods"`
// 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, default is '// Code generated by ifacemaker; DO NOT EDIT.'"`
Output string `short:"o" long:"output" description:"Output file name. If not provided, result will be printed to stdout."`
}
func main() {
var args cmdlineArgs
_, err := flags.ParseArgs(&args, os.Args)
if err != nil {
if flags.WroteHelp(err) {
return
}
// 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)
}
if args.Comment == "" {
args.Comment = "Code generated by ifacemaker; DO NOT EDIT."
}
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(maker.MakeOptions{
Files: files,
StructType: args.StructType,
Comment: args.Comment,
PkgName: args.PkgName,
IfaceName: args.IfaceName,
IfaceComment: args.IfaceComment,
CopyDocs: args.copyDocs,
CopyTypeDoc: args.CopyTypeDoc,
ImportModule: args.ImportModule,
ExcludeMethods: args.ExcludeMethods,
WithNotExported: args.WithNotExported,
})
if err != nil {
log.Fatal(err.Error())
}
if args.Output == "" {
fmt.Println(string(result))
} else {
f, err := os.Create(args.Output)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(result); err != nil {
log.Fatal(err)
}
}
}