Skip to content

Commit a559594

Browse files
add auto open auth link (#187)
* add utility in system package to use command for triggering web * improve phrasing of auth dialog --------- Co-authored-by: Scott Cotton <scott@mindowl.com>
1 parent b88ec89 commit a559594

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

internal/command/auth/login.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/signadot/cli/internal/auth"
1111
"github.com/signadot/cli/internal/config"
1212
"github.com/signadot/cli/internal/spinner"
13+
"github.com/signadot/cli/internal/utils/system"
1314
sdkauth "github.com/signadot/go-sdk/client/auth"
1415
"github.com/signadot/go-sdk/client/orgs"
1516
"github.com/signadot/go-sdk/models"
@@ -127,8 +128,13 @@ func getDeviceCode(cfg *config.AuthLogin) (*models.AuthdevicesCode, error) {
127128

128129
func waitForUserAuth(cfg *config.AuthLogin, out io.Writer,
129130
code *models.AuthdevicesCode) (*models.AuthdevicesToken, error) {
130-
fmt.Fprintf(out, "To authenticate, visit: "+code.VerificationURI+"\n")
131-
fmt.Fprintf(out, "and confirm the code: "+code.UserCode+"\n\n")
131+
if err := system.OpenBrowser(code.VerificationURI); err != nil {
132+
// If browser opening fails, fall back to just showing the URL
133+
fmt.Fprintf(out, "Please visit: %s\n", code.VerificationURI)
134+
} else {
135+
fmt.Fprintf(out, "Opening browser at: %s\n", code.VerificationURI)
136+
}
137+
fmt.Fprintf(out, "to authenticate using the code: %s\n\n", code.UserCode)
132138
spin := spinner.Start(out, "Waiting for authentication")
133139
defer spin.Stop()
134140

internal/utils/system/system.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"os/exec"
78
"path"
89
"path/filepath"
10+
"runtime"
911

1012
"github.com/panta/machineid"
1113
"gopkg.in/natefinch/lumberjack.v2"
@@ -63,6 +65,20 @@ func GetMachineID() (string, error) {
6365
return machineID[:63], nil
6466
}
6567

68+
// OpenBrowser opens the specified URL in the user's default browser
69+
func OpenBrowser(url string) error {
70+
var cmd *exec.Cmd
71+
switch runtime.GOOS {
72+
case "darwin":
73+
cmd = exec.Command("open", url)
74+
case "windows":
75+
cmd = exec.Command("cmd", "/c", "start", url)
76+
default: // "linux", "freebsd", "openbsd", "netbsd"
77+
cmd = exec.Command("xdg-open", url)
78+
}
79+
return cmd.Start()
80+
}
81+
6682
func GetSandboxesDir() (string, error) {
6783
sdDir, err := GetSignadotDir()
6884
if err != nil {

0 commit comments

Comments
 (0)