Skip to content

Commit 17bfd7c

Browse files
authored
Migrate to acmd (#6)
1 parent 2662165 commit 17bfd7c

File tree

3 files changed

+49
-97
lines changed

3 files changed

+49
-97
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/cristaloleg/go-perftuner
22

33
go 1.10
4+
5+
require github.com/cristalhq/acmd v0.1.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/cristalhq/acmd v0.1.5 h1:sQJ4hfJsGyMa0opX8uqNNnH9Bvf5wL2Png5F1yuIX+A=
2+
github.com/cristalhq/acmd v0.1.5/go.mod h1:LG5oa43pE/BbxtfMoImHCQN++0Su7dzipdgBjMCBVDQ=

perftune.go

Lines changed: 45 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,71 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
78
"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-
}
329

33-
type subCommandRunner interface {
34-
Init()
35-
Run(pkg string) error
36-
}
10+
"github.com/cristalhq/acmd"
11+
)
3712

38-
type subCommand struct {
39-
runner subCommandRunner
40-
name string
41-
shortName string
42-
summary string
43-
}
13+
const version = "v0.0.0"
4414

4515
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")
6718
}
6819

20+
log.SetFlags(0)
6921
flag.BoolVar(&asJSON, "json", false, `return result as JSON`)
70-
71-
cmd.runner.Init()
7222
flag.Parse()
7323

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))
8929
}
90-
return nil
9130
}
9231

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+
},
9654
}
9755

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+
}
10262
}
63+
return nil
10364
}
10465

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
11567

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
12371
}

0 commit comments

Comments
 (0)