-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (141 loc) · 3.97 KB
/
main.go
File metadata and controls
155 lines (141 loc) · 3.97 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"io"
"os"
"runtime/pprof"
"strings"
"github.com/facebookincubator/go-belt"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/facebookincubator/go-belt/tool/logger/implementation/logrus"
"github.com/spf13/cobra"
)
const (
defaultBinderDevice = "/dev/binder"
defaultMapSize = 128 * 1024
)
var cpuProfileFile *os.File
func newRootCmd() *cobra.Command {
logLevel := logger.LevelWarning
cmd := &cobra.Command{
Use: "bindercli",
Short: "CLI tool for interacting with Android Binder services",
Long: `bindercli is a command-line interface for listing, inspecting,
and invoking Android Binder services using AIDL-generated Go bindings.`,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
l := logrus.Default().WithLevel(logLevel)
ctx := belt.CtxWithBelt(cmd.Context(), belt.New())
ctx = logger.CtxWithLogger(ctx, l)
cmd.SetContext(ctx)
cpuFile, _ := cmd.Flags().GetString("cpuprofile")
if cpuFile != "" {
f, err := os.Create(cpuFile)
if err != nil {
return fmt.Errorf("creating CPU profile: %w", err)
}
cpuProfileFile = f
if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("starting CPU profile: %w", err)
}
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, _ []string) error {
cpuFile, _ := cmd.Flags().GetString("cpuprofile")
if cpuFile != "" {
pprof.StopCPUProfile()
if cpuProfileFile != nil {
cpuProfileFile.Close()
cpuProfileFile = nil
}
}
memFile, _ := cmd.Flags().GetString("memprofile")
if memFile != "" {
f, err := os.Create(memFile)
if err != nil {
return fmt.Errorf("creating memory profile: %w", err)
}
defer f.Close()
if err := pprof.WriteHeapProfile(f); err != nil {
return fmt.Errorf("writing memory profile: %w", err)
}
}
return nil
},
}
cmd.PersistentFlags().Var(
&logLevel,
"log-level",
"log level: trace, debug, info, warning, error, fatal, panic",
)
cmd.PersistentFlags().String(
"format",
"auto",
"output format: json, text, or auto (detect terminal vs pipe)",
)
cmd.PersistentFlags().String(
"binder-device",
defaultBinderDevice,
"path to the binder device",
)
cmd.PersistentFlags().Int(
"map-size",
defaultMapSize,
"binder mmap size in bytes",
)
cmd.PersistentFlags().Int(
"target-api",
0,
"Android API level to target (0 = auto-detect from device)",
)
cmd.PersistentFlags().String(
"cpuprofile",
"",
"write CPU profile to file",
)
cmd.PersistentFlags().String(
"memprofile",
"",
"write memory profile to file",
)
cmd.AddCommand(newServiceCmd())
cmd.AddCommand(newAIDLCmd())
cmd.AddCommand(newCameraCmd())
return cmd
}
// isUnknownCommandError reports whether err is cobra's "unknown command" error,
// which means the user invoked a subcommand that doesn't exist yet (likely a
// generated proxy command that hasn't been registered).
func isUnknownCommandError(err error) bool {
return err != nil && strings.HasPrefix(err.Error(), "unknown command ")
}
func main() {
// Phase 1: try executing with only the lightweight built-in commands
// (service, aidl, camera). Suppress cobra's error output so that an
// "unknown command" failure for a generated proxy command is invisible.
// Stdout is left alone so --help and normal command output still work.
cmd := newRootCmd()
cmd.SilenceErrors = true
cmd.SilenceUsage = true
cmd.SetErr(io.Discard)
err := cmd.Execute()
switch {
case err == nil:
return
case isUnknownCommandError(err):
// Phase 2: the user invoked a generated command. Register all
// generated commands and retry with normal output.
addGeneratedCommands(cmd)
cmd.SilenceErrors = false
cmd.SilenceUsage = false
cmd.SetErr(os.Stderr)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
default:
// A built-in command failed. Print the error ourselves since
// cobra's error printing was silenced for phase 1.
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}