From 20b95e48094c81b6088ae973855cbe2e82f350f6 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Thu, 14 Apr 2022 18:14:31 +0800 Subject: [PATCH] replace .rod file with rod cli flag --- lib/defaults/defaults.go | 83 +++++++++++++++++++---------------- lib/defaults/defaults_test.go | 78 ++++++++++++++++---------------- lib/launcher/launcher_test.go | 2 +- lib/launcher/private_test.go | 2 +- 4 files changed, 84 insertions(+), 81 deletions(-) diff --git a/lib/defaults/defaults.go b/lib/defaults/defaults.go index 47ed42a2..6b4d3205 100644 --- a/lib/defaults/defaults.go +++ b/lib/defaults/defaults.go @@ -3,7 +3,7 @@ package defaults import ( - "io/ioutil" + "flag" "log" "os" "regexp" @@ -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. @@ -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 diff --git a/lib/defaults/defaults_test.go b/lib/defaults/defaults_test.go index ed5828a7..247369e6 100644 --- a/lib/defaults/defaults_test.go +++ b/lib/defaults/defaults_test.go @@ -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{}) { @@ -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) +} diff --git a/lib/launcher/launcher_test.go b/lib/launcher/launcher_test.go index 75df623a..f8646dae 100644 --- a/lib/launcher/launcher_test.go +++ b/lib/launcher/launcher_test.go @@ -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() diff --git a/lib/launcher/private_test.go b/lib/launcher/private_test.go index 2ba41e5d..aa4d5757 100644 --- a/lib/launcher/private_test.go +++ b/lib/launcher/private_test.go @@ -73,7 +73,7 @@ func TestLaunchOptions(t *testing.T) { // restore defer func() { - defaults.ResetWithEnv("") + defaults.ResetWith("") inContainer = utils.InContainer }()