/
base_config.go
133 lines (120 loc) · 3.68 KB
/
base_config.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
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
package cli
import (
"fmt"
"strconv"
"time"
"github.com/Sirupsen/logrus"
"github.com/zach-klippenstein/goadb"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
DefaultPoolSize = 2
DefaultCacheTtl = 300 * time.Millisecond
DefaultLogLevel = logrus.InfoLevel
)
type BaseConfig struct {
// Command-line arguments. Each variable in this block should have a line in AsArgs().
AdbPort int
ConnectionPoolSize int
LogLevel string
Verbose bool
CacheTtl time.Duration
ServeDebug bool
DeviceRoot string
ReadOnly bool
PathToAdb string
}
const (
AdbPortFlag = "port"
ConnectionPoolSizeFlag = "pool"
CacheTtlFlag = "cachettl"
LogLevelFlag = "log"
VerboseFlag = "verbose"
ServeDebugFlag = "debug"
DeviceRootFlag = "device-root"
ReadOnlyFlag = "readonly"
PathToAdb = "adb"
)
func registerBaseFlags(config *BaseConfig) {
kingpin.Flag(AdbPortFlag,
"Port to connect to adb server on.").
Default(strconv.Itoa(adb.AdbPort)).
IntVar(&config.AdbPort)
kingpin.Flag(ConnectionPoolSizeFlag,
"Size of the connection pool. Not used for open files.").
Default(strconv.Itoa(DefaultPoolSize)).
IntVar(&config.ConnectionPoolSize)
kingpin.Flag(CacheTtlFlag,
"Duration to keep cached file info.").
Default(DefaultCacheTtl.String()).
DurationVar(&config.CacheTtl)
kingpin.Flag(ServeDebugFlag,
"If set, will start an HTTP server to expose profiling and trace logs. Off by default.").
BoolVar(&config.ServeDebug)
kingpin.Flag(DeviceRootFlag,
"The device directory to mount.").
Default("/sdcard").
StringVar(&config.DeviceRoot)
kingpin.Flag(ReadOnlyFlag,
"Mount as a readonly filesystem. True by default, since write support is still experimental. Use --no-readonly to enable writes.").
Short('r').
Default("true").
BoolVar(&config.ReadOnly)
kingpin.Flag(PathToAdb,
"Path to the adb executable. If unspecified, the PATH environment variable will be searched.").
StringVar(&config.PathToAdb)
logLevels := []string{
logrus.PanicLevel.String(),
logrus.FatalLevel.String(),
logrus.ErrorLevel.String(),
logrus.WarnLevel.String(),
logrus.InfoLevel.String(),
logrus.DebugLevel.String(),
}
kingpin.Flag(LogLevelFlag, fmt.Sprintf("Detail of logs to show. Options are: %v", logLevels)).
Default(DefaultLogLevel.String()).
EnumVar(&config.LogLevel, logLevels...)
kingpin.Flag(VerboseFlag, "Alias for --log=debug.").
Short('v').
BoolVar(&config.Verbose)
}
// AsArgs returns a string array suitable to be passed to exec.Command that copies
// the arguments defined in this package.
func (c *BaseConfig) AsArgs() []string {
return []string{
formatFlag(AdbPortFlag, c.AdbPort),
formatFlag(ConnectionPoolSizeFlag, c.ConnectionPoolSize),
formatFlag(LogLevelFlag, c.LogLevel),
formatFlag(CacheTtlFlag, c.CacheTtl),
formatFlag(ServeDebugFlag, c.ServeDebug),
formatFlag(VerboseFlag, c.Verbose),
formatFlag(DeviceRootFlag, c.DeviceRoot),
formatFlag(ReadOnlyFlag, c.ReadOnly),
formatFlag(PathToAdb, c.PathToAdb),
}
}
// ServerConfig returns a adb.ServerConfig from CLI arguments.
func (c *BaseConfig) ServerConfig() adb.ServerConfig {
return adb.ServerConfig{
Port: c.AdbPort,
PathToAdb: c.PathToAdb,
}
}
func (c *BaseConfig) createLogger() *logrus.Logger {
log := logrus.StandardLogger()
if c.Verbose {
log.Level = logrus.DebugLevel
} else {
logLevel, err := logrus.ParseLevel(c.LogLevel)
if err != nil {
log.Fatal(err)
}
log.Level = logLevel
}
log.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
// RFC 3339 with milliseconds.
TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
}
return log
}