-
-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move open logic into separate package
- Loading branch information
Katrina Owen
committed
Aug 2, 2017
1 parent
54de9de
commit 7bf4b82
Showing
2 changed files
with
36 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package browser | ||
|
||
import ( | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// Open opens a browser to the given URL. | ||
// The terminal's open command is operating system dependent. | ||
func Open(url string) error { | ||
// Escape characters are not allowed by cmd/bash. | ||
switch runtime.GOOS { | ||
case "windows": | ||
url = strings.Replace(url, "&", `^&`, -1) | ||
default: | ||
url = strings.Replace(url, "&", `\&`, -1) | ||
} | ||
|
||
// The command to open the browser is OS-dependent. | ||
var cmd *exec.Cmd | ||
switch runtime.GOOS { | ||
case "darwin": | ||
cmd = exec.Command("open", url) | ||
case "freebsd", "linux", "netbsd", "openbsd": | ||
cmd = exec.Command("xdg-open", url) | ||
case "windows": | ||
cmd = exec.Command("cmd", "/c", "start", url) | ||
} | ||
|
||
return cmd.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters