Skip to content

Commit 08b55d9

Browse files
committed
feat: Add the ability to create a profile from a data file
#13 Branch: Profiles-13 Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
1 parent 10cbf7f commit 08b55d9

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

cforge/commands/settings/profiles.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
# Standard
1111
from datetime import datetime
12+
from pathlib import Path
1213
from typing import Optional
14+
import json
1315
import secrets
1416
import string
1517

@@ -171,7 +173,9 @@ def profiles_switch(
171173
raise typer.Exit(1)
172174

173175

174-
def profiles_create() -> None:
176+
def profiles_create(
177+
data_file: Optional[Path] = typer.Argument(None, help="JSON file containing prompt data (interactive mode if not provided)"),
178+
) -> None:
175179
"""Create a new profile interactively.
176180
177181
Walks the user through creating a new profile by prompting for all required
@@ -197,8 +201,14 @@ def profiles_create() -> None:
197201
"last_used": None,
198202
}
199203

200-
# Prompt for profile data using the schema
201-
profile_data = prompt_for_schema(AuthProfile, prefilled=prefilled)
204+
if data_file:
205+
if not data_file.exists():
206+
console.print(f"[red]File not found: {data_file}[/red]")
207+
raise typer.Exit(1)
208+
profile_data = json.loads(data_file.read_text())
209+
profile_data.update(prefilled)
210+
else:
211+
profile_data = prompt_for_schema(AuthProfile, prefilled=prefilled)
202212

203213
# Create the AuthProfile instance
204214
new_profile = AuthProfile.model_validate(profile_data)

tests/commands/settings/test_profiles.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
# Standard
1111
from datetime import datetime
12+
from pathlib import Path
1213
from unittest.mock import Mock, patch
14+
import json
15+
import tempfile
1316

1417
# Third-Party
1518
import pytest
@@ -658,3 +661,30 @@ def test_profiles_create_with_existing_store(self, mock_console, mock_settings)
658661
assert len(updated_store.profiles) == 2
659662
assert "existing-profile" in updated_store.profiles
660663
assert "new-profile-id" in updated_store.profiles
664+
665+
def test_profiles_create_data_file(self, mock_console, mock_settings) -> None:
666+
"""Test successfully creating a new profile using a data file."""
667+
with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console):
668+
with patch("cforge.commands.settings.profiles.typer.confirm", return_value=False):
669+
data_file_content = {
670+
"name": "Test Profile",
671+
"email": "test@example.com",
672+
"api_url": "https://api.test.com",
673+
"is_active": False,
674+
}
675+
with tempfile.NamedTemporaryFile("w") as data_file:
676+
data_file.write(json.dumps(data_file_content))
677+
data_file.flush()
678+
profiles_create(Path(data_file.name))
679+
680+
# Verify both profiles exist in the store
681+
updated_store = load_profile_store()
682+
assert updated_store is not None
683+
assert len(updated_store.profiles) == 1
684+
profile_id = list(updated_store.profiles.keys())[0]
685+
profile = list(updated_store.profiles.values())[0]
686+
assert profile.id == profile_id
687+
assert profile.name == data_file_content["name"]
688+
assert profile.email == data_file_content["email"]
689+
assert profile.api_url == data_file_content["api_url"]
690+
assert profile.is_active == data_file_content["is_active"]

0 commit comments

Comments
 (0)