Skip to content

Commit

Permalink
replace .rod file with rod cli flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Apr 14, 2022
1 parent 1ee5762 commit 20b95e4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 81 deletions.
83 changes: 45 additions & 38 deletions lib/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package defaults

import (
"io/ioutil"
"flag"
"log"
"os"
"regexp"
Expand All @@ -15,52 +15,52 @@ import (
)

// Trace is the default of rod.Browser.Trace .
// Env name is "trace".
// Option name is "trace".
var Trace bool

// Slow is the default of rod.Browser.Slowmotion .
// The format is same as https://golang.org/pkg/time/#ParseDuration
// Env name is "slow".
// Option name is "slow".
var Slow time.Duration

// Monitor is the default of rod.Browser.ServeMonitor .
// Env name is "monitor".
// Option name is "monitor".
var Monitor string

// Show is the default of launcher.Launcher.Headless .
// Env name is "show".
// Option name is "show".
var Show bool

// Devtools is the default of launcher.Launcher.Devtools .
// Env name is "devtools".
// Option name is "devtools".
var Devtools bool

// Dir is the default of launcher.Launcher.UserDataDir .
// Env name is "dir".
// Option name is "dir".
var Dir string

// Port is the default of launcher.Launcher.RemoteDebuggingPort .
// Env name is "port".
// Option name is "port".
var Port string

// Bin is the default of launcher.Launcher.Bin .
// Env name is "bin".
// Option name is "bin".
var Bin string

// Proxy is the default of launcher.Launcher.Proxy
// Env name is "trace".
// Option name is "trace".
var Proxy string

// LockPort is the default of launcher.Browser.LockPort
// Env name is "lock".
// Option name is "lock".
var LockPort int

// URL is the default of cdp.Client.New .
// Env name is "url".
// URL is the default websocket url for remote control a browser.
// Option name is "url".
var URL string

// CDP is the default of cdp.Client.Logger
// Env name is "cdp".
// Option name is "cdp".
var CDP utils.Logger

// Reset all flags to their init values.
Expand Down Expand Up @@ -132,39 +132,46 @@ var envParsers = map[string]func(string){

// Parse the flags
func init() {
ResetWithEnv("")
ResetWith("")
}

// ResetWithEnv set the default value of options used by rod.
// ResetWith options and "-rod" command line flag.
// 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:
// It will try to load the cli flag "-rod" and then the options, the later override the former.
// If you want to disable the global cli argument flag, set env DISABLE_ROD_FLAG.
// Values are separated by commas, key and value are separated by "=". For example:
//
// os.Open(".rod")
// os.Getenv("rod")
// env
// go run main.go -rod=show
// go run main.go -rod show,trace,slow=1s,monitor
// go run main.go --rod="slow=1s,dir=path/has /space,monitor=:9223"
//
// 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) {
func ResetWith(options string) {
Reset()

b, _ := ioutil.ReadFile(".rod")
parse(string(b))
if _, has := os.LookupEnv("DISABLE_ROD_FLAG"); !has {
if !flag.Parsed() {
flag.String("rod", "", `Set the default value of options used by rod.`)
}

parseFlag(os.Args)
}

parse(options)
}

parse(os.Getenv("rod"))
func parseFlag(args []string) {
reg := regexp.MustCompile(`^--?rod$`)
regEq := regexp.MustCompile(`^--?rod=(.*)$`)
opts := ""
for i, arg := range args {
if reg.MatchString(arg) {
opts = args[i+1]
} else if m := regEq.FindStringSubmatch(arg); len(m) == 2 {
opts = m[1]
}
}

parse(env)
parse(opts)
}

// parse options and set them globally
Expand Down
78 changes: 37 additions & 41 deletions lib/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,47 @@ import (
)

func TestBasic(t *testing.T) {
as := got.New(t)
g := got.T(t)

Show = true
Devtools = true
URL = "test"
Monitor = "test"

ResetWithEnv("")
ResetWith("")
parse("")
as.False(Show)
as.False(Devtools)
as.Eq("", Monitor)
as.Eq("", URL)
as.Eq(2978, LockPort)
g.False(Show)
g.False(Devtools)
g.Eq("", Monitor)
g.Eq("", URL)
g.Eq(2978, LockPort)

parse("show,devtools,trace,slow=2s,port=8080,dir=tmp," +
"url=http://test.com,cdp,monitor,bin=/path/to/chrome," +
"proxy=localhost:8080,lock=9981",
"proxy=localhost:8080,lock=9981,",
)

as.True(Show)
as.True(Devtools)
as.True(Trace)
as.Eq(2*time.Second, Slow)
as.Eq("8080", Port)
as.Eq("/path/to/chrome", Bin)
as.Eq("tmp", Dir)
as.Eq("http://test.com", URL)
as.NotNil(CDP.Println)
as.Eq(":0", Monitor)
as.Eq("localhost:8080", Proxy)
as.Eq(9981, LockPort)
g.True(Show)
g.True(Devtools)
g.True(Trace)
g.Eq(2*time.Second, Slow)
g.Eq("8080", Port)
g.Eq("/path/to/chrome", Bin)
g.Eq("tmp", Dir)
g.Eq("http://test.com", URL)
g.NotNil(CDP.Println)
g.Eq(":0", Monitor)
g.Eq("localhost:8080", Proxy)
g.Eq(9981, LockPort)

parse("monitor=:1234")
as.Eq(":1234", Monitor)
g.Eq(":1234", Monitor)

as.Panic(func() {
g.Panic(func() {
parse("a")
})

as.Eq(try(func() { parse("slow=1") }), "invalid value for \"slow\": time: missing unit in duration \"1\" (learn format from https://golang.org/pkg/time/#ParseDuration)")
}

func TestDotFile(t *testing.T) {
as := got.New(t)

ResetWithEnv("")
parse(`
show
port=9999
dir=path =to file
`)

as.True(Show)
as.Eq("9999", Port)
as.Eq("path =to file", Dir)
g.Eq(try(func() { parse("slow=1") }), "invalid value for \"slow\": time: missing unit in duration \"1\" (learn format from https://golang.org/pkg/time/#ParseDuration)")
}

func try(fn func()) (err interface{}) {
Expand All @@ -78,3 +60,17 @@ func try(fn func()) (err interface{}) {

return err
}

func TestParseFlag(t *testing.T) {
g := got.T(t)

Reset()

parseFlag([]string{"-rod=show"})
g.True(Show)

Reset()

parseFlag([]string{"-rod", "show"})
g.True(Show)
}
2 changes: 1 addition & 1 deletion lib/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestLaunch(t *testing.T) {
g := setup(t)

defaults.Proxy = "test.com"
defer func() { defaults.ResetWithEnv("") }()
defer func() { defaults.ResetWith("") }()

l := launcher.New()
defer l.Kill()
Expand Down
2 changes: 1 addition & 1 deletion lib/launcher/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestLaunchOptions(t *testing.T) {

// restore
defer func() {
defaults.ResetWithEnv("")
defaults.ResetWith("")
inContainer = utils.InContainer
}()

Expand Down

0 comments on commit 20b95e4

Please sign in to comment.