Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REC-57: open browser automatically #54

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This repository provides `engflow_auth`, a [Bazel credential helper](https://blo

## Use

1. Run `engflow_auth login [CLUSTER URL]` to obtain a credential. This prints a URL to visit in your browser.
1. Run `engflow_auth login [CLUSTER URL]` to obtain a credential. This opens a URL in your browser.
1. Visit the URL to complete the process, logging in if necessary. `engflow_auth` will download and store a credential in on your system's encrypted keyring.

This process needs to be repeated after the credential expires, typically every 90 days.
Expand Down
2 changes: 1 addition & 1 deletion cmd/engflow_auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
)

type appState struct {
// These vars are initialized by `build()` iff they are not pre-populated;
// These vars are initialized by `build()` if and only if they are not pre-populated;
// they should be pre-populated in tests and left nil otherwise.
userConfigDir string
browserOpener browser.Opener
Expand Down
33 changes: 32 additions & 1 deletion internal/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"net/url"
"os"
"os/exec"
"runtime"
)

type Opener interface {
Expand All @@ -26,11 +28,40 @@ type Opener interface {

type StderrPrint struct{}

func openURL(url string) error {
switch runtime.GOOS {
case "darwin": // macOS
return exec.Command("open", url).Run()
case "linux":
providers := []string{"xdg-open", "x-www-browser", "www-browser"}

// There are multiple possible providers to open a browser on linux
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
// Look for one that exists and run it
for _, provider := range providers {
if binPath, err := exec.LookPath(provider); err == nil {
err = exec.Command(binPath, url).Run()
if err == nil {
return nil
}
}
}
return fmt.Errorf("unsupported platform")
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Run()
default:
return fmt.Errorf("unsupported platform")
}
}

func (p *StderrPrint) Open(u *url.URL) error {
fmt.Fprintf(
os.Stderr,
"Please open the following URL in your web browser to authenticate:\n\n\t%s\n\n",
`Attempting to automaticaly open the authentication URL in your web browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:
%s`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two tabs ends up being a lot of space. Let's take it back to one.

Also make sure to keep the newline at the end here.

u,
)
_ = openURL(u.String())
return nil
}