Skip to content

Commit c82fa34

Browse files
authored
Do not require project name on logfire projects use command (#177)
1 parent 4e94b68 commit c82fa34

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

logfire/_internal/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ def parse_auth(args: argparse.Namespace) -> None:
270270

271271
console.print()
272272
console.print(f'Your Logfire credentials are stored in [bold]{DEFAULT_FILE}[/]')
273-
# TODO(Marcelo): Add a message to inform which commands can be used.
274273

275274

276275
def parse_list_projects(args: argparse.Namespace) -> None:
@@ -326,6 +325,7 @@ def parse_use_project(args: argparse.Namespace) -> None:
326325
project_name = args.project_name
327326
organization = args.org
328327
console = Console(file=sys.stderr)
328+
329329
projects = LogfireCredentials.get_user_projects(session=args._session, logfire_api_url=logfire_url)
330330
project_info = LogfireCredentials.use_existing_project(
331331
session=args._session,
@@ -438,7 +438,7 @@ def _main(args: list[str] | None = None) -> None:
438438
cmd_projects_new.set_defaults(func=parse_create_new_project)
439439

440440
cmd_projects_use = projects_subparsers.add_parser('use', help='use a project')
441-
cmd_projects_use.add_argument('project_name', help='project name')
441+
cmd_projects_use.add_argument('project_name', nargs='?', help='project name')
442442
cmd_projects_use.add_argument('--org', help='project organization')
443443
cmd_projects_use.add_argument('--data-dir', default='.logfire')
444444
cmd_projects_use.set_defaults(func=parse_use_project)

logfire/_internal/config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def use_existing_project(
913913
project_name: Name of project that has to be used.
914914
915915
Returns:
916-
The configured project informations.
916+
The configured project information.
917917
918918
Raises:
919919
LogfireConfigError: If there was an error configuring the project.
@@ -1142,10 +1142,7 @@ def initialize_project(
11421142

11431143
projects = cls.get_user_projects(session=session, logfire_api_url=logfire_api_url)
11441144
if projects:
1145-
use_existing_projects = Confirm.ask(
1146-
'Do you want to use one of your existing projects? ',
1147-
default=True,
1148-
)
1145+
use_existing_projects = Confirm.ask('Do you want to use one of your existing projects? ', default=True)
11491146
if use_existing_projects: # pragma: no branch
11501147
credentials = cls.use_existing_project(
11511148
session=session, logfire_api_url=logfire_api_url, projects=projects

tests/test_cli.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,59 @@ def test_projects_use(tmp_dir_cwd: Path, default_credentials: Path) -> None:
823823
}
824824

825825

826+
def test_projects_use_without_project_name(tmp_dir_cwd: Path, default_credentials: Path) -> None:
827+
with ExitStack() as stack:
828+
stack.enter_context(patch('logfire._internal.config.LogfireCredentials._get_user_token', return_value=''))
829+
console = stack.enter_context(patch('logfire._internal.cli.Console'))
830+
prompt_mock = stack.enter_context(patch('rich.prompt.Prompt.ask', side_effect=['1']))
831+
832+
m = requests_mock.Mocker()
833+
stack.enter_context(m)
834+
m.get(
835+
'https://logfire-api.pydantic.dev/v1/projects/',
836+
json=[
837+
{'organization_name': 'fake_org', 'project_name': 'myproject'},
838+
{'organization_name': 'fake_org', 'project_name': 'otherproject'},
839+
],
840+
)
841+
create_project_response = {
842+
'json': {
843+
'project_name': 'myproject',
844+
'token': 'fake_token',
845+
'project_url': 'fake_project_url',
846+
}
847+
}
848+
m.post(
849+
'https://logfire-api.pydantic.dev/v1/organizations/fake_org/projects/myproject/write-tokens/',
850+
[create_project_response],
851+
)
852+
853+
main(['projects', 'use'])
854+
855+
assert prompt_mock.mock_calls == [
856+
call(
857+
(
858+
'Please select one of the following projects by number:\n'
859+
'1. fake_org/myproject\n'
860+
'2. fake_org/otherproject\n'
861+
),
862+
choices=['1', '2'],
863+
default='1',
864+
)
865+
]
866+
867+
console_calls = [re.sub(r'^call(\(\).)?', '', str(call)) for call in console.mock_calls]
868+
assert console_calls == [
869+
IsStr(regex=r'^\(file=.*'),
870+
"print('Project configured successfully. You will be able to view it at: fake_project_url')",
871+
]
872+
873+
assert json.loads((tmp_dir_cwd / '.logfire/logfire_credentials.json').read_text()) == {
874+
**create_project_response['json'],
875+
'logfire_api_url': 'https://logfire-api.pydantic.dev',
876+
}
877+
878+
826879
def test_projects_use_multiple(tmp_dir_cwd: Path, default_credentials: Path) -> None:
827880
with ExitStack() as stack:
828881
stack.enter_context(patch('logfire._internal.config.LogfireCredentials._get_user_token', return_value=''))

0 commit comments

Comments
 (0)