Skip to content

Commit f016e49

Browse files
authored
Implement cross-platform URL opening convenience func (#64)
1 parent 1938518 commit f016e49

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

url/url.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package url
33
import (
44
"fmt"
55
"net/url"
6+
"os/exec"
7+
"runtime"
68
"strings"
79

810
"github.com/gruntwork-io/go-commons/errors"
@@ -52,3 +54,24 @@ func mergeQuery(originalQuery url.Values, newQuery url.Values) url.Values {
5254
func stripSlashes(str string) string {
5355
return strings.Trim(str, "/")
5456
}
57+
58+
// Attempt to open a URL in the user's browser. We use this to open docs, PRs we've
59+
// programmatically opened, etc
60+
func OpenURL(url string) error {
61+
var err error
62+
63+
switch runtime.GOOS {
64+
case "linux":
65+
err = exec.Command("xdg-open", url).Start()
66+
case "windows":
67+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
68+
case "darwin":
69+
err = exec.Command("open", url).Start()
70+
default:
71+
err = fmt.Errorf("unsupported platform")
72+
}
73+
if err != nil {
74+
return err
75+
}
76+
return nil
77+
}

0 commit comments

Comments
 (0)