-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
When calling `engflow_auth login $cluster`, the browser will now automatically open to the correct url with the necessary query params appended.
11f357d
to
f8a2a44
Compare
Ready for review? Looks good! |
There was a problem hiding this 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 :)
internal/browser/browser.go
Outdated
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, |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
internal/browser/browser.go
Outdated
var err error | ||
switch runtime.GOOS { | ||
case "darwin": // macOS | ||
err = exec.Command("open", url).Start() |
There was a problem hiding this comment.
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.
internal/browser/browser.go
Outdated
// 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 { |
There was a problem hiding this comment.
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.
internal/browser/browser.go
Outdated
// 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() |
There was a problem hiding this comment.
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.
internal/browser/browser.go
Outdated
default: | ||
err = fmt.Errorf("unsupported platform") | ||
} | ||
return err |
There was a problem hiding this comment.
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
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", |
There was a problem hiding this comment.
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:
cmd/engflow_auth/main.go
Outdated
@@ -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; |
There was a problem hiding this comment.
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.
// 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]>
4858c83
to
d88e55e
Compare
d88e55e
to
6e814b9
Compare
Co-authored-by: Jay Conrod <[email protected]>
internal/browser/browser.go
Outdated
"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`, |
There was a problem hiding this comment.
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.
Add the pkg/browser module to handle cross platform opening of urls in the user's default browser.