-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
105 lines (89 loc) 路 1.89 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"flag"
"fmt"
"os"
"goa.design/clue/log"
cluemockgen "goa.design/clue/mock/cmd/cmg/pkg"
)
func main() {
var (
gSet = flag.NewFlagSet("global", flag.ExitOnError)
debug, help, h *bool
addGlobals = func(set *flag.FlagSet) {
debug = set.Bool("debug", false, "Print debug output")
help = set.Bool("help", false, "Print help information")
h = set.Bool("h", false, "Print help information")
}
genSet = flag.NewFlagSet("gen", flag.ExitOnError)
versionSet = flag.NewFlagSet("version", flag.ExitOnError)
showUsage = func(code int) {
printUsage(gSet)
os.Exit(code)
}
)
addGlobals(gSet)
if len(os.Args) == 1 {
showUsage(1)
}
var (
cmd = os.Args[1]
args []string
)
switch cmd {
case "gen":
addGlobals(genSet)
_ = genSet.Parse(os.Args[2:])
args = genSet.Args()
case "version":
addGlobals(versionSet)
_ = versionSet.Parse(os.Args[2:])
args = versionSet.Args()
case "help":
showUsage(0)
default:
_ = gSet.Parse(os.Args[1:])
}
if *h || *help {
showUsage(0)
}
switch cmd {
case "gen":
ctx := context.Background()
if *debug {
ctx = log.Context(ctx, log.WithDebug())
} else {
ctx = log.Context(ctx)
}
err := cluemockgen.Generate(ctx, args, "")
if err != nil {
os.Exit(1)
}
case "version":
fmt.Println(os.Args[0], "version", cluemockgen.Version())
default:
fmt.Fprintf(os.Stderr, `unknown command %q, use "--help" for usage`, cmd)
os.Exit(1)
}
}
func printUsage(fss ...*flag.FlagSet) {
cmd := os.Args[0]
fmt.Fprintf(os.Stderr, `%v is the Clue Mock Generation tool for the Goa framework.
Usage:
%v gen PACKAGE...
%v version
Commands:
gen
Generate mocks for interfaces in packages
version
Print version information
Args:
PACKAGE
Go import path(s) to look for interfaces
Flags:
`, cmd, cmd, cmd)
for _, fs := range fss {
fs.PrintDefaults()
}
}