-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
defaults.go
185 lines (158 loc) · 3.39 KB
/
defaults.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package defaults of commonly used options parsed from environment.
// Check ResetWithEnv for details.
package defaults
import (
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/go-rod/rod/lib/utils"
)
// Trace is the default of rod.Browser.Trace
var Trace bool
// Slow is the default of rod.Browser.Slowmotion
// The format is same as https://golang.org/pkg/time/#ParseDuration
var Slow time.Duration
// Monitor is the default of rod.Browser.ServeMonitor
var Monitor string
// Show is the default of launcher.Launcher.Headless
var Show bool
// Devtools is the default of launcher.Launcher.Devtools
var Devtools bool
// Dir is the default of launcher.Launcher.UserDataDir
var Dir string
// Port is the default of launcher.Launcher.RemoteDebuggingPort
var Port string
// Bin is the default of launcher.Launcher.Bin
var Bin string
// Proxy is the default of launcher.Launcher.Proxy
var Proxy string
// Lock is the default of launcher.Browser.Lock
var Lock int
// URL is the default of cdp.Client.New
var URL string
// CDP is the default of cdp.Client.Logger
var CDP utils.Logger
// Reset all flags to their init values.
func Reset() {
Trace = false
Slow = 0
Monitor = ""
Show = false
Devtools = false
Dir = ""
Port = "0"
Bin = ""
Proxy = ""
Lock = 2978
URL = ""
CDP = utils.LoggerQuiet
}
var rules = map[string]func(string){
"trace": func(string) {
Trace = true
},
"slow": func(v string) {
var err error
Slow, err = time.ParseDuration(v)
if err != nil {
msg := "invalid value for \"slow\": " + err.Error() +
" (learn format from https://golang.org/pkg/time/#ParseDuration)"
panic(msg)
}
},
"monitor": func(v string) {
Monitor = ":0"
if v != "" {
Monitor = v
}
},
"show": func(string) {
Show = true
},
"devtools": func(string) {
Devtools = true
},
"dir": func(v string) {
Dir = v
},
"port": func(v string) {
Port = v
},
"bin": func(v string) {
Bin = v
},
"proxy": func(v string) {
Proxy = v
},
"lock": func(v string) {
i, err := strconv.ParseInt(v, 10, 32)
if err == nil {
Lock = int(i)
}
},
"url": func(v string) {
URL = v
},
"cdp": func(v string) {
CDP = log.New(log.Writer(), "[cdp] ", log.LstdFlags)
},
}
// Parse the flags
func init() {
ResetWithEnv("")
}
// ResetWithEnv set the default value of options used by rod.
// It will be called in an init() , so you don't have to call it manually.
// The followings will be parsed and merged, later overrides previous:
//
// os.Open(".rod")
// os.Getenv("rod")
// env
//
// Values are separated by commas, key and value are separated by "=",
// For example, on unix-like OS:
//
// rod="show,trace,slow=1s,monitor" go run main.go
//
// rod="slow=1s,dir=path/has /space,monitor=:9223" go run main.go
//
// An example of ".rod" file content:
//
// slow=1s
// dir=path/has /space
// monitor=:9223
//
func ResetWithEnv(env string) {
Reset()
b, _ := ioutil.ReadFile(".rod")
parse(string(b))
parse(os.Getenv("rod"))
parse(env)
}
// parse options and set them globally
func parse(options string) {
if options == "" {
return
}
reg := regexp.MustCompile(`[,\r\n]`)
for _, str := range reg.Split(options, -1) {
kv := strings.SplitN(str, "=", 2)
v := ""
if len(kv) == 2 {
v = kv[1]
}
n := strings.TrimSpace(kv[0])
if n == "" {
continue
}
f := rules[n]
if f == nil {
panic("unknown rod env option: " + n)
}
f(v)
}
}