diff options
| -rw-r--r-- | browserenv.go | 20 | ||||
| -rw-r--r-- | browserenv_unix_test.go | 20 | 
2 files changed, 38 insertions, 2 deletions
| diff --git a/browserenv.go b/browserenv.go index f5a51ca..4edb032 100644 --- a/browserenv.go +++ b/browserenv.go @@ -17,6 +17,8 @@ var Stdout io.Writer = os.Stdout  var percentS = regexp.MustCompile("%s[[:^alpha:]]?") +const commandSeparator = ":" +  func OpenFile(path string) error {  	envCommand := envBrowserCommand()  	if envCommand != "" { @@ -67,8 +69,22 @@ func envBrowserCommand() string {  }  // TODO -func runBrowserCommand(command, url string) error { -	return browserCommand(command, url).Run() +func runBrowserCommand(commands, url string) error { +	commandList := strings.Split(commands, commandSeparator) + +	var err error +	for _, command := range commandList { +		cmd := browserCommand(command, url) + +		// Keep running commands from left to right until one of them exits +		// successfully. +		err = cmd.Run() +		if err == nil || cmd.ProcessState.ExitCode() == 0 { +			return err +		} +	} + +	return err  }  // TODO diff --git a/browserenv_unix_test.go b/browserenv_unix_test.go index f79d31d..971454e 100644 --- a/browserenv_unix_test.go +++ b/browserenv_unix_test.go @@ -96,3 +96,23 @@ func TestOpenURLStderr(t *testing.T) {  		t.Errorf("got stdout value %q want %q", got, url)  	}  } + +func TestOpenURLMultipleBrowserCommands(t *testing.T) { +	// The `test -z URL` command must fail, causing `printf URL` to run. +	err := os.Setenv("BROWSER", "test -z:printf") +	if err != nil { +		t.Fatal(err) +	} + +	var stdout strings.Builder +	Stdout = &stdout + +	url := "http://localhost:8000" + +	OpenURL(url) + +	got := stdout.String() +	if got != url { +		t.Errorf("got stdout value %q want %q", got, url) +	} +} | 
