diff --git a/go.mod b/go.mod index cbce542b..21572310 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.0.7 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/sergi/go-diff v1.3.1 // indirect diff --git a/go.sum b/go.sum index 5958399c..ad105d80 100644 --- a/go.sum +++ b/go.sum @@ -229,6 +229,8 @@ github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9Cjg github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= diff --git a/internal/auth/callback.go b/internal/auth/callback.go index 16885031..3ce419a1 100644 --- a/internal/auth/callback.go +++ b/internal/auth/callback.go @@ -3,10 +3,9 @@ package auth import ( "context" "fmt" + "github.com/pkg/browser" "log" "net/http" - "os/exec" - "runtime" "time" ) @@ -59,26 +58,6 @@ func (awh AuthWebHelper) Callback(state string) string { return authCode } -func (awh AuthWebHelper) openBrowserCmd(runtimeOS, url string) *exec.Cmd { - var cmd string - var args []string - switch runtimeOS { - case "windows": - cmd = "cmd" - url = "\"" + url + "\"" // Windows does not like "&" - args = []string{"/c", "start"} - case "darwin": - cmd = "open" - default: // "linux", "freebsd", "openbsd", "netbsd" - cmd = "xdg-open" - } - args = append(args, url) - - return exec.Command(cmd, args...) -} - func (awh AuthWebHelper) OpenURL(authURL string) error { - openCmd := awh.openBrowserCmd(runtime.GOOS, authURL) - - return openCmd.Start() + return browser.OpenURL(authURL) } diff --git a/internal/auth/callback_test.go b/internal/auth/callback_test.go index 3eb8506d..09e124e4 100644 --- a/internal/auth/callback_test.go +++ b/internal/auth/callback_test.go @@ -4,11 +4,8 @@ import ( "context" "fmt" "net/http" - "os/exec" "testing" "time" - - "github.com/stretchr/testify/assert" ) const testState = "test_state" @@ -93,31 +90,3 @@ func TestCallbackServerError(t *testing.T) { t.Fatal("Test timed out") } } - -func TestOpenBrowserCmd(t *testing.T) { - a := NewAuthWebHelper() - cases := []struct { - runtimeOS string - expectedCmd *exec.Cmd - }{ - { - runtimeOS: "darwin", - expectedCmd: exec.Command("open", "url"), - }, - { - runtimeOS: "linux", - expectedCmd: exec.Command("xdg-open", "url"), - }, - { - runtimeOS: "windows", - expectedCmd: exec.Command("cmd", "/c", "start", "\"url\""), - }, - } - - for _, c := range cases { - t.Run(c.runtimeOS, func(t *testing.T) { - authCmd := a.openBrowserCmd(c.runtimeOS, "url") - assert.Equal(t, c.expectedCmd.Args, authCmd.Args) - }) - } -}