Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
58 changes: 43 additions & 15 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
from opentelemetry.sdk.trace.id_generator import IdGenerator
from opentelemetry.sdk.trace.sampling import ParentBasedTraceIdRatio, Sampler
from prompt_toolkit import HTML, choice
from prompt_toolkit.styles import Style
from rich.console import Console
from rich.prompt import Confirm, Prompt
from typing_extensions import Self, Unpack
Expand Down Expand Up @@ -1457,22 +1459,48 @@ def use_existing_project(
project_name = None

if organization is None or project_name is None:
project_choices = {
str(index + 1): (item['organization_name'], item['project_name'])
for index, item in enumerate(filtered_projects)
}
project_choices_str = '\n'.join(
[f'{index}. {item[0]}/{item[1]}' for index, item in project_choices.items()]
)
selected_project_key = Prompt.ask(
f'Please select one of the following projects by number:\n{project_choices_str}\n',
choices=list(project_choices.keys()),
default='1',
)
project_info_tuple: tuple[str, str] = project_choices[selected_project_key]
organization = project_info_tuple[0]
project_name = project_info_tuple[1]
default_org = 'samuelcolvin'
# if possible, it would be better to sort orgs by when they were created, not alphabetically
org_options = sorted({project['organization_name'] for project in projects})
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't ask this if there's only one org.


def select_project(organization: str) -> str | None:
projects_in_organization = [
project for project in projects if project['organization_name'] == organization
]
project_options = sorted({project['project_name'] for project in projects_in_organization})
return choice(
message='Select the project to use',
options=[(project, project) for project in project_options],
style=Style.from_dict({'frame.border': '#884444', 'selected-option': 'bold'}),
)

if len(org_options) > 1:
default_org_projects = [
project['project_name'] for project in projects if project['organization_name'] == default_org
]
project_choice = choice(
message=HTML(
f'Select a project from the <b>{default_org}</b> organization, or select another organization.'
),
options=[(project, project) for project in default_org_projects]
+ [('__other_org__', 'Select project from another organization')],
style=Style.from_dict({'frame.border': '#884444', 'selected-option': 'bold'}),
)
if project_choice == '__other_org__':
organization = choice(
message='Select the organization to use', options=[(org, org) for org in org_options]
)
assert organization is not None
project_name = select_project(organization)
else:
organization = default_org
project_name = project_choice
else:
organization = org_options[0]
project_name = select_project(organization)

assert organization is not None
assert project_name is not None
return client.create_write_token(organization, project_name)

@classmethod
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies = [
"typing-extensions >= 4.1.0",
"tomli >= 2.0.1; python_version < '3.11'",
"executing >= 2.0.1",
"prompt-toolkit >= 3.0.52",
Copy link
Member

Choose a reason for hiding this comment

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

can we say >=3?

]

[project.optional-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading