Skip to content

Commit a0b99b6

Browse files
committed
z0 - browser based login
1 parent 441f483 commit a0b99b6

32 files changed

Lines changed: 3083 additions & 131 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v1.1.3] - 2026-10-12
8+
9+
### Added
10+
- browser based login using device/app login flow
11+
712
## [v1.1.2] - 2026-09-04
813

914
### Added

‎go.mod‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ module github.com/zeropsio/zcli
22

33
go 1.26
44

5-
require github.com/zeropsio/zerops-go v1.0.23
5+
require github.com/zeropsio/zerops-go v1.0.24-0.20261009070312-063d0b57de7d
66

77
require (
88
github.com/charmbracelet/bubbles v0.20.0
99
github.com/charmbracelet/bubbletea v1.3.5
1010
github.com/charmbracelet/lipgloss v1.1.0
1111
github.com/charmbracelet/x/exp/teatest v0.0.0-20260511125431-fe5d686e0c99
12+
github.com/gofrs/flock v0.13.1
1213
github.com/golang/mock v1.6.0
1314
github.com/google/uuid v1.6.0
1415
github.com/gorilla/websocket v1.5.3
@@ -20,7 +21,7 @@ require (
2021
github.com/spf13/cobra v1.9.1
2122
github.com/spf13/pflag v1.0.6
2223
github.com/spf13/viper v1.20.0
23-
github.com/stretchr/testify v1.10.0
24+
github.com/stretchr/testify v1.12.1
2425
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
2526
golang.org/x/mod v0.35.0
2627
golang.org/x/term v0.43.0
@@ -39,7 +40,6 @@ require (
3940
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
4041
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b // indirect
4142
github.com/charmbracelet/x/term v0.2.1 // indirect
42-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
4343
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
4444
github.com/fsnotify/fsnotify v1.8.0 // indirect
4545
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
@@ -51,7 +51,6 @@ require (
5151
github.com/muesli/cancelreader v0.2.2 // indirect
5252
github.com/muesli/termenv v0.16.0 // indirect
5353
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
54-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5554
github.com/rivo/uniseg v0.4.7 // indirect
5655
github.com/sagikazarmark/locafero v0.7.0 // indirect
5756
github.com/shopspring/decimal v1.4.0 // indirect
@@ -61,8 +60,9 @@ require (
6160
github.com/subosito/gotenv v1.6.0 // indirect
6261
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
6362
go.uber.org/multierr v1.11.0 // indirect
63+
go.yaml.in/yaml/v3 v3.0.5 // indirect
6464
golang.org/x/crypto v0.52.0 // indirect
6565
golang.org/x/sync v0.20.0 // indirect
66-
golang.org/x/sys v0.45.0 // indirect
66+
golang.org/x/sys v0.47.0 // indirect
6767
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
6868
)

‎go.sum‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNE
2727
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2828
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2929
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
30-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
31-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3230
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
3331
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
3432
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
3533
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
3634
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
3735
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
38-
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
39-
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
4036
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
4137
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
38+
github.com/gofrs/flock v0.13.1 h1:jjREztyBeSKBZYAC+mgc1laB+xsgy4kYMf3FbKF2UBo=
39+
github.com/gofrs/flock v0.13.1/go.mod h1:sf4BFiHwnvgxa25DlQoDqXQnwRMEOwqxRq37P6MzzmE=
4240
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
4341
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
4442
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -77,8 +75,6 @@ github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xl
7775
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
7876
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7977
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
80-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
81-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8278
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
8379
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
8480
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -108,17 +104,19 @@ github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqj
108104
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
109105
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
110106
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
111-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
112-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
107+
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
108+
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
113109
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
114110
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
115111
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
116112
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
117113
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
118-
github.com/zeropsio/zerops-go v1.0.23 h1:BtGSONtULSlcwjfF3t3ytq4t2BuFEFc6u+7Dy5/R5Qk=
119-
github.com/zeropsio/zerops-go v1.0.23/go.mod h1:SY4o+4jAIrsDTwWACooogf1OyY1jNK8NqWBe9KFcjAA=
114+
github.com/zeropsio/zerops-go v1.0.24-0.20261009070312-063d0b57de7d h1:UxjUH7aYLgJRo8VqznKbFWr6TdeiBaXwdcFL4kYbYcs=
115+
github.com/zeropsio/zerops-go v1.0.24-0.20261009070312-063d0b57de7d/go.mod h1:SY4o+4jAIrsDTwWACooogf1OyY1jNK8NqWBe9KFcjAA=
120116
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
121117
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
118+
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
119+
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
122120
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
123121
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
124122
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
@@ -150,8 +148,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
150148
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
151149
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
152150
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
153-
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
154-
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
151+
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
152+
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
155153
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
156154
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
157155
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=

‎src/browser/browser.go‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package browser
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"os"
7+
"os/exec"
8+
"runtime"
9+
"strings"
10+
11+
"github.com/pkg/errors"
12+
)
13+
14+
var ErrNoBrowser = errors.New("no browser available")
15+
16+
// Open starts the default browser with rawURL and does not wait for it.
17+
// It returns an error when no browser can be started.
18+
func Open(rawURL string) error {
19+
parsed, err := url.Parse(rawURL)
20+
if err != nil {
21+
return errors.Wrap(err, "parse url")
22+
}
23+
// rundll32 and open also start local files and programs, so only web links are passed on.
24+
if parsed.Scheme != "http" && parsed.Scheme != "https" {
25+
return errors.Errorf("unsupported url scheme %q", parsed.Scheme)
26+
}
27+
28+
h := host{
29+
goos: runtime.GOOS,
30+
getenv: os.Getenv,
31+
readFile: os.ReadFile,
32+
lookPath: exec.LookPath,
33+
}
34+
argv, err := h.command(rawURL)
35+
if err != nil {
36+
return err
37+
}
38+
39+
// The process is not tied to a caller context, so cancelling the login never kills a browser that is starting up.
40+
// Nil stdin, stdout and stderr connect to the null device, so launcher warnings never reach the terminal.
41+
cmd := exec.CommandContext(context.Background(), argv[0], argv[1:]...)
42+
if err := cmd.Start(); err != nil {
43+
return errors.Wrapf(err, "start %s", argv[0])
44+
}
45+
// The launcher exit code says nothing about the browser, so the process is only reaped.
46+
go func() { _ = cmd.Wait() }()
47+
return nil
48+
}
49+
50+
type host struct {
51+
goos string
52+
getenv func(string) string
53+
readFile func(string) ([]byte, error)
54+
lookPath func(string) (string, error)
55+
}
56+
57+
func (h host) command(rawURL string) ([]string, error) {
58+
switch h.goos {
59+
case "darwin":
60+
return []string{"open", rawURL}, nil
61+
case "windows":
62+
// rundll32 hands the url to ShellExecute without a shell, so every & stays intact.
63+
return []string{"rundll32", "url.dll,FileProtocolHandler", rawURL}, nil
64+
case "linux":
65+
// WSLg sets DISPLAY, so xdg-open would start a Linux browser inside WSL instead of the Windows one.
66+
if h.isWSL() {
67+
return h.wslCommand(rawURL)
68+
}
69+
return h.xdgCommand(rawURL)
70+
case "freebsd", "openbsd", "netbsd", "dragonfly", "solaris", "illumos":
71+
return h.xdgCommand(rawURL)
72+
default:
73+
return nil, errors.Wrapf(ErrNoBrowser, "unsupported OS %s", h.goos)
74+
}
75+
}
76+
77+
func (h host) isWSL() bool {
78+
if h.getenv("WSL_DISTRO_NAME") != "" {
79+
return true
80+
}
81+
osRelease, err := h.readFile("/proc/sys/kernel/osrelease")
82+
return err == nil && strings.Contains(strings.ToLower(string(osRelease)), "microsoft")
83+
}
84+
85+
// wslCommand never uses explorer.exe, which opens the Documents folder for some URLs.
86+
// The absolute rundll32.exe path covers `appendWindowsPath = false` in /etc/wsl.conf, and a Linux browser in WSLg is the last resort.
87+
func (h host) wslCommand(rawURL string) ([]string, error) {
88+
if _, err := h.lookPath("wslview"); err == nil {
89+
return []string{"wslview", rawURL}, nil
90+
}
91+
for _, name := range []string{"rundll32.exe", "/mnt/c/Windows/System32/rundll32.exe"} {
92+
if _, err := h.lookPath(name); err == nil {
93+
return []string{name, "url.dll,FileProtocolHandler", rawURL}, nil
94+
}
95+
}
96+
return h.xdgCommand(rawURL)
97+
}
98+
99+
// xdgCommand never falls back to x-www-browser, www-browser or $BROWSER, which may start a text browser that takes over the terminal.
100+
func (h host) xdgCommand(rawURL string) ([]string, error) {
101+
if h.getenv("DISPLAY") == "" && h.getenv("WAYLAND_DISPLAY") == "" {
102+
return nil, errors.Wrap(ErrNoBrowser, "no graphical session, DISPLAY and WAYLAND_DISPLAY are unset")
103+
}
104+
if _, err := h.lookPath("xdg-open"); err != nil {
105+
return nil, errors.Wrap(ErrNoBrowser, "xdg-open not found")
106+
}
107+
return []string{"xdg-open", rawURL}, nil
108+
}

0 commit comments

Comments
 (0)