vendor/github.com/go-rod/rod/lib/defaults/defaults.go
203 lines · 4238 bytes
1// Package defaults of commonly used options parsed from environment.
2// Check ResetWith for details.
3package defaults
4
5import (
6 "flag"
7 "log"
8 "os"
9 "regexp"
10 "strconv"
11 "strings"
12 "time"
13
14 "github.com/go-rod/rod/lib/utils"
15)
16
17// Trace is the default of rod.Browser.Trace .
18// Option name is "trace".
19var Trace bool
20
21// Slow is the default of rod.Browser.SlowMotion .
22// The format is same as https://golang.org/pkg/time/#ParseDuration
23// Option name is "slow".
24var Slow time.Duration
25
26// Monitor is the default of rod.Browser.ServeMonitor .
27// Option name is "monitor".
28var Monitor string
29
30// Show is the default of launcher.Launcher.Headless .
31// Option name is "show".
32var Show bool
33
34// Devtools is the default of launcher.Launcher.Devtools .
35// Option name is "devtools".
36var Devtools bool
37
38// Dir is the default of launcher.Launcher.UserDataDir .
39// Option name is "dir".
40var Dir string
41
42// Port is the default of launcher.Launcher.RemoteDebuggingPort .
43// Option name is "port".
44var Port string
45
46// Bin is the default of launcher.Launcher.Bin .
47// Option name is "bin".
48var Bin string
49
50// Proxy is the default of launcher.Launcher.Proxy
51// Option name is "proxy".
52var Proxy string
53
54// LockPort is the default of launcher.Browser.LockPort
55// Option name is "lock".
56var LockPort int
57
58// URL is the default websocket url for remote control a browser.
59// Option name is "url".
60var URL string
61
62// CDP is the default of cdp.Client.Logger
63// Option name is "cdp".
64var CDP utils.Logger
65
66// Reset all flags to their init values.
67func Reset() {
68 Trace = false
69 Slow = 0
70 Monitor = ""
71 Show = false
72 Devtools = false
73 Dir = ""
74 Port = "0"
75 Bin = ""
76 Proxy = ""
77 LockPort = 2978
78 URL = ""
79 CDP = utils.LoggerQuiet
80}
81
82var envParsers = map[string]func(string){
83 "trace": func(string) {
84 Trace = true
85 },
86 "slow": func(v string) {
87 var err error
88 Slow, err = time.ParseDuration(v)
89 if err != nil {
90 msg := "invalid value for \"slow\": " + err.Error() +
91 " (learn format from https://golang.org/pkg/time/#ParseDuration)"
92 panic(msg)
93 }
94 },
95 "monitor": func(v string) {
96 Monitor = ":0"
97 if v != "" {
98 Monitor = v
99 }
100 },
101 "show": func(string) {
102 Show = true
103 },
104 "devtools": func(string) {
105 Devtools = true
106 },
107 "dir": func(v string) {
108 Dir = v
109 },
110 "port": func(v string) {
111 Port = v
112 },
113 "bin": func(v string) {
114 Bin = v
115 },
116 "proxy": func(v string) {
117 Proxy = v
118 },
119 "lock": func(v string) {
120 i, err := strconv.ParseInt(v, 10, 32)
121 if err == nil {
122 LockPort = int(i)
123 }
124 },
125 "url": func(v string) {
126 URL = v
127 },
128 "cdp": func(_ string) {
129 CDP = log.New(log.Writer(), "[cdp] ", log.LstdFlags)
130 },
131}
132
133// Parse the flags.
134func init() {
135 ResetWith("")
136}
137
138// ResetWith options and "-rod" command line flag.
139// It will be called in an init() , so you don't have to call it manually.
140// It will try to load the cli flag "-rod" and then the options, the later override the former.
141// If you want to disable the global cli argument flag, set env DISABLE_ROD_FLAG.
142// Values are separated by commas, key and value are separated by "=". For example:
143//
144// go run main.go -rod=show
145// go run main.go -rod show,trace,slow=1s,monitor
146// go run main.go --rod="slow=1s,dir=path/has /space,monitor=:9223"
147func ResetWith(options string) {
148 Reset()
149
150 if _, has := os.LookupEnv("DISABLE_ROD_FLAG"); !has {
151 if !flag.Parsed() && flag.Lookup("rod") == nil {
152 flag.String("rod", "", `Set the default value of options used by rod.`)
153 }
154
155 parseFlag(os.Args)
156 }
157
158 parse(options)
159}
160
161func parseFlag(args []string) {
162 reg := regexp.MustCompile(`^--?rod$`)
163 regEq := regexp.MustCompile(`^--?rod=(.*)$`)
164 opts := ""
165 for i, arg := range args {
166 if reg.MatchString(arg) && i+1 < len(args) {
167 opts = args[i+1]
168 } else if m := regEq.FindStringSubmatch(arg); len(m) == 2 {
169 opts = m[1]
170 }
171 }
172
173 parse(opts)
174}
175
176// parse options and set them globally.
177func parse(options string) {
178 if options == "" {
179 return
180 }
181
182 reg := regexp.MustCompile(`[,\r\n]`)
183
184 for _, str := range reg.Split(options, -1) {
185 kv := strings.SplitN(str, "=", 2)
186
187 v := ""
188 if len(kv) == 2 {
189 v = kv[1]
190 }
191
192 n := strings.TrimSpace(kv[0])
193 if n == "" {
194 continue
195 }
196
197 f := envParsers[n]
198 if f == nil {
199 panic("unknown rod env option: " + n)
200 }
201 f(v)
202 }
203}