|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "flag" |
5 | 6 | "fmt" |
6 | 7 | "log" |
7 | 8 | "os" |
8 | | -) |
9 | | - |
10 | | -var asJSON bool |
11 | | - |
12 | | -var subCommands = []*subCommand{ |
13 | | - { |
14 | | - runner: &almostInlinedRunner{}, |
15 | | - name: "almostInlined", |
16 | | - shortName: "inl", |
17 | | - summary: "find functions that cross inlining threshold just barely", |
18 | | - }, |
19 | | - { |
20 | | - runner: &escapeAnalysisRunner{}, |
21 | | - name: "escapedVariables", |
22 | | - shortName: "esc", |
23 | | - summary: "find variables that are escaped to the heap", |
24 | | - }, |
25 | | - { |
26 | | - runner: &boundCheckRunner{}, |
27 | | - name: "boundChecks", |
28 | | - shortName: "bce", |
29 | | - summary: "find slice/array that has bound check", |
30 | | - }, |
31 | | -} |
32 | 9 |
|
33 | | -type subCommandRunner interface { |
34 | | - Init() |
35 | | - Run(pkg string) error |
36 | | -} |
| 10 | + "github.com/cristalhq/acmd" |
| 11 | +) |
37 | 12 |
|
38 | | -type subCommand struct { |
39 | | - runner subCommandRunner |
40 | | - name string |
41 | | - shortName string |
42 | | - summary string |
43 | | -} |
| 13 | +const version = "v0.0.0" |
44 | 14 |
|
45 | 15 | func main() { |
46 | | - log.SetFlags(0) |
47 | | - |
48 | | - argv := os.Args |
49 | | - if len(argv) < 2 { |
50 | | - terminate("not enough arguments, expected sub-command name", printUsage) |
51 | | - } |
52 | | - |
53 | | - subIdx := 1 // [0] is program name |
54 | | - sub := os.Args[subIdx] |
55 | | - // Erase sub-command argument (index=1) to make it invisible for |
56 | | - // sub commands themselves. |
57 | | - os.Args = append(os.Args[:subIdx], os.Args[subIdx+1:]...) |
58 | | - |
59 | | - // Choose and run sub-command main. |
60 | | - cmd := findSubCommand(sub) |
61 | | - if cmd == nil { |
62 | | - terminate("unknown sub-command: "+sub, printSupportedSubs) |
63 | | - } |
64 | | - flag.Usage = func() { |
65 | | - log.Printf("usage: [flags] package...") |
66 | | - flag.PrintDefaults() |
| 16 | + if len(os.Args) < 2 { |
| 17 | + panic("not enough arguments, expected sub-command name") |
67 | 18 | } |
68 | 19 |
|
| 20 | + log.SetFlags(0) |
69 | 21 | flag.BoolVar(&asJSON, "json", false, `return result as JSON`) |
70 | | - |
71 | | - cmd.runner.Init() |
72 | 22 | flag.Parse() |
73 | 23 |
|
74 | | - for _, pkg := range flag.Args() { |
75 | | - log.SetPrefix(sub + ": " + pkg + ": ") |
76 | | - if err := cmd.runner.Run(pkg); err != nil { |
77 | | - log.Printf("%s: %v", pkg, err) |
78 | | - } |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -// findSubCommand looks up subCommand by its name. |
83 | | -// Returns nil if requested command not found. |
84 | | -func findSubCommand(name string) *subCommand { |
85 | | - for _, cmd := range subCommands { |
86 | | - if cmd.name == name || cmd.shortName == name { |
87 | | - return cmd |
88 | | - } |
| 24 | + r := acmd.RunnerOf(cmds, acmd.Config{ |
| 25 | + Version: version, |
| 26 | + }) |
| 27 | + if err := r.Run(); err != nil { |
| 28 | + log.Fatal(fmt.Errorf("dbumper: %w", err)) |
89 | 29 | } |
90 | | - return nil |
91 | 30 | } |
92 | 31 |
|
93 | | -func printUsage() { |
94 | | - // TODO: implement me. For now, print supported commands. |
95 | | - printSupportedSubs() |
| 32 | +var cmds = []acmd.Command{ |
| 33 | + { |
| 34 | + Name: "inl", |
| 35 | + Description: "find functions that cross inlining threshold just barely", |
| 36 | + Do: func(_ context.Context, _ []string) error { |
| 37 | + return run(&almostInlinedRunner{}) |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + Name: "esc", |
| 42 | + Description: "find variables that are escaped to the heap", |
| 43 | + Do: func(_ context.Context, _ []string) error { |
| 44 | + return run(&escapeAnalysisRunner{}) |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "bce", |
| 49 | + Description: "find slice/array that has bound check", |
| 50 | + Do: func(_ context.Context, _ []string) error { |
| 51 | + return run(&boundCheckRunner{}) |
| 52 | + }, |
| 53 | + }, |
96 | 54 | } |
97 | 55 |
|
98 | | -func printSupportedSubs() { |
99 | | - stderrPrintf("Supported sub-commands:\n") |
100 | | - for _, cmd := range subCommands { |
101 | | - stderrPrintf("\t%s (or %s) - %s\n", cmd.name, cmd.shortName, cmd.summary) |
| 56 | +func run(cmd subCommandRunner) error { |
| 57 | + cmd.Init() |
| 58 | + for _, pkg := range flag.Args()[1:] { |
| 59 | + if err := cmd.Run(pkg); err != nil { |
| 60 | + log.Printf("%s: %v", pkg, err) |
| 61 | + } |
102 | 62 | } |
| 63 | + return nil |
103 | 64 | } |
104 | 65 |
|
105 | | -// terminate prints error specified by reason, runs optional printHelp |
106 | | -// function and then exists with non-zero status. |
107 | | -func terminate(reason string, printHelp func()) { |
108 | | - stderrPrintf("error: %s\n", reason) |
109 | | - if printHelp != nil { |
110 | | - stderrPrintf("\n") |
111 | | - printHelp() |
112 | | - } |
113 | | - os.Exit(1) |
114 | | -} |
| 66 | +var asJSON bool |
115 | 67 |
|
116 | | -// stderrPrintf writes formatted message to stderr and checks for error |
117 | | -// making "not annoying at all" linters happy. |
118 | | -func stderrPrintf(format string, args ...interface{}) { |
119 | | - _, err := fmt.Fprintf(os.Stderr, format, args...) |
120 | | - if err != nil { |
121 | | - panic(fmt.Sprintf("stderr write error: %v", err)) |
122 | | - } |
| 68 | +type subCommandRunner interface { |
| 69 | + Init() |
| 70 | + Run(pkg string) error |
123 | 71 | } |
0 commit comments