-
Notifications
You must be signed in to change notification settings - Fork 173
Trigger auth command from prompt
#1423
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sys | ||
| import webbrowser | ||
| from urllib.parse import urlparse | ||
|
|
||
| from ..auth import DEFAULT_FILE, UserTokenCollection, poll_for_token, request_device_code | ||
| from ..config import REGIONS | ||
|
|
||
|
|
||
| def parse_auth(args: argparse.Namespace) -> None: | ||
| """Authenticate with Logfire. | ||
|
|
||
| This will authenticate your machine with Logfire and store the credentials. | ||
| """ | ||
| logfire_url: str | None = args.logfire_url | ||
|
|
||
| tokens_collection = UserTokenCollection() | ||
| logged_in = tokens_collection.is_logged_in(logfire_url) | ||
|
|
||
| if logged_in: | ||
| sys.stderr.writelines( | ||
| ( | ||
| f'You are already logged in. (Your credentials are stored in {DEFAULT_FILE})\n', | ||
| 'If you would like to log in using a different account, use the --region argument:\n', | ||
| 'logfire --region <region> auth\n', | ||
| ) | ||
| ) | ||
| return | ||
|
|
||
| sys.stderr.writelines( | ||
| ( | ||
| '\n', | ||
| 'Welcome to Logfire! 🔥\n', | ||
| 'Before you can send data to Logfire, we need to authenticate you.\n', | ||
| '\n', | ||
| ) | ||
| ) | ||
| if not logfire_url: | ||
| selected_region = -1 | ||
| while not (1 <= selected_region <= len(REGIONS)): | ||
| sys.stderr.write('Logfire is available in multiple data regions. Please select one:\n') | ||
| for i, (region_id, region_data) in enumerate(REGIONS.items(), start=1): | ||
| sys.stderr.write(f'{i}. {region_id.upper()} (GCP region: {region_data["gcp_region"]})\n') | ||
|
|
||
| try: | ||
| selected_region = int( | ||
| input(f'Selected region [{"/".join(str(i) for i in range(1, len(REGIONS) + 1))}]: ') | ||
| ) | ||
| except ValueError: | ||
| selected_region = -1 | ||
| logfire_url = list(REGIONS.values())[selected_region - 1]['base_url'] | ||
|
|
||
| device_code, frontend_auth_url = request_device_code(args._session, logfire_url) | ||
| frontend_host = urlparse(frontend_auth_url).netloc | ||
|
|
||
| # We are not using the `prompt` parameter from `input` here because we want to write to stderr. | ||
| sys.stderr.write(f'Press Enter to open {frontend_host} in your browser...\n') | ||
| input() | ||
|
Comment on lines
+58
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only lines changed here. |
||
|
|
||
| try: | ||
| webbrowser.open(frontend_auth_url, new=2) | ||
| except webbrowser.Error: | ||
| pass | ||
| sys.stderr.writelines( | ||
| ( | ||
| f"Please open {frontend_auth_url} in your browser to authenticate if it hasn't already.\n", | ||
| 'Waiting for you to authenticate with Logfire...\n', | ||
| ) | ||
| ) | ||
|
|
||
| tokens_collection.add_token(logfire_url, poll_for_token(args._session, device_code, logfire_url)) | ||
| sys.stderr.write('Successfully authenticated!\n') | ||
| sys.stderr.write(f'\nYour Logfire credentials are stored in {DEFAULT_FILE}\n') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 moved this parse method because I wanted to import from
prompt.py.