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

Conversation

phoenixuprising
Copy link
Contributor

Add the pkg/browser module to handle cross platform opening of urls in the user's default browser.

When calling `engflow_auth login $cluster`, the browser will now automatically open to the correct url with the necessary query params appended.
@rogerhu
Copy link
Contributor

rogerhu commented Oct 17, 2024

Ready for review? Looks good!

Copy link
Contributor

@minor-fixes minor-fixes left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! I'm certainly excited to not have to click the link as part of the flow anymore :)

cmd/engflow_auth/main.go Outdated Show resolved Hide resolved
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",
os.Stdout,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the switch to stdout? It does mean the object should probably be renamed, as StderrPrint now no longer only prints to stderr

Originally this was stderr because I figured it followed UNIX best practices (stdout for machine-consumable output, stderr for human-only output)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I was wondering about the choice for stderr originally. You make a good point about stdout being used for machine output. Will revert it back to stderr.

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

Verified this works on Windows. I have a couple small comments, but this is looking good!

var err error
switch runtime.GOOS {
case "darwin": // macOS
err = exec.Command("open", url).Start()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Run rather than Start. Run waits for the subprocess to terminate, so its error will be more useful. On all platforms, these commands complete pretty quickly, so it's fine to block.

// 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 _, err := exec.LookPath(provider); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: assign the result of LookPath and use that in the call to exec.Command to avoid repeating the lookup.

// Look for one that exists and run it
for _, provider := range providers {
if _, err := exec.LookPath(provider); err == nil {
err = exec.Command(provider, url).Start()
Copy link
Contributor

Choose a reason for hiding this comment

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

break or return nil if there was no error. If for some reason all of these commands are installed, we don't want to open the browser three times.

default:
err = fmt.Errorf("unsupported platform")
}
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: err will be nil on linux if none of those commands are installed. Let's either add a err = fmt.Errorf("unsupported platform") at the end of the loop or change each of these case to return their error directly.

internal/browser/browser.go Outdated Show resolved Hide resolved
os.Stderr,
"Please open the following URL in your web browser to authenticate:\n\n\t%s\n\n",
os.Stdout,
"Opening the following URL in your web browser to authenticate:\n\n\t%s\n\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

The user might be running this command on a remote machine whether there is no browser or where they can't see the browser. I think it still makes sense to tell them to open the link manually in the case.

For reference, this is what aws sso login prints:

Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.us-east-2.amazonaws.com/

Then enter the code:

@@ -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 they are not pre-populated;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was meant to be an abbreviation rather than a misspelling. Let's expand it.

Suggested change
// These vars are initialized by `build()` if they are not pre-populated;
// These vars are initialized by `build()` if and only if they are not pre-populated;

Co-authored-by: Scott Minor <[email protected]>
@phoenixuprising phoenixuprising force-pushed the chayes/auto-open-browser branch 2 times, most recently from 4858c83 to d88e55e Compare October 17, 2024 21:08
@phoenixuprising phoenixuprising marked this pull request as ready for review October 17, 2024 21:11
"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.

@phoenixuprising phoenixuprising enabled auto-merge (squash) October 18, 2024 05:57
@phoenixuprising phoenixuprising enabled auto-merge (squash) October 18, 2024 05:58
@phoenixuprising phoenixuprising merged commit 2d27df2 into main Oct 18, 2024
5 checks passed
@phoenixuprising phoenixuprising deleted the chayes/auto-open-browser branch October 18, 2024 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants