Skip to content

Commit fd2d2b8

Browse files
authored
Merge pull request #928 from codeflash-ai/feat/cli/login
[Feat] CLI Login
2 parents c6ab05f + 147fb31 commit fd2d2b8

File tree

2 files changed

+841
-2
lines changed

2 files changed

+841
-2
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from codeflash.code_utils.env_utils import check_formatter_installed, get_codeflash_api_key
3232
from codeflash.code_utils.git_utils import get_git_remotes, get_repo_owner_and_name
3333
from codeflash.code_utils.github_utils import get_github_secrets_page_url
34+
from codeflash.code_utils.oauth_handler import perform_oauth_signin
3435
from codeflash.code_utils.shell_utils import get_shell_rc_path, save_api_key_to_rc
3536
from codeflash.either import is_successful
3637
from codeflash.lsp.helpers import is_LSP_enabled
@@ -1166,10 +1167,13 @@ def convert(self, value: str, param: click.Parameter | None, ctx: click.Context
11661167

11671168
# Returns True if the user entered a new API key, False if they used an existing one
11681169
def prompt_api_key() -> bool:
1170+
"""Prompt user for API key via OAuth or manual entry."""
1171+
# Check for existing API key
11691172
try:
11701173
existing_api_key = get_codeflash_api_key()
11711174
except OSError:
11721175
existing_api_key = None
1176+
11731177
if existing_api_key:
11741178
display_key = f"{existing_api_key[:3]}****{existing_api_key[-4:]}"
11751179
api_key_panel = Panel(
@@ -1186,8 +1190,52 @@ def prompt_api_key() -> bool:
11861190
console.print()
11871191
return False
11881192

1189-
enter_api_key_and_save_to_rc()
1190-
ph("cli-new-api-key-entered")
1193+
# Prompt for authentication method
1194+
auth_choices = ["🔐 Login in with Codeflash", "🔑 Use Codeflash API key"]
1195+
1196+
questions = [
1197+
inquirer.List(
1198+
"auth_method",
1199+
message="How would you like to authenticate?",
1200+
choices=auth_choices,
1201+
default=auth_choices[0],
1202+
carousel=True,
1203+
)
1204+
]
1205+
1206+
answers = inquirer.prompt(questions, theme=CodeflashTheme())
1207+
if not answers:
1208+
apologize_and_exit()
1209+
1210+
method = answers["auth_method"]
1211+
1212+
if method == auth_choices[1]:
1213+
enter_api_key_and_save_to_rc()
1214+
ph("cli-new-api-key-entered")
1215+
return True
1216+
1217+
# Perform OAuth sign-in
1218+
api_key = perform_oauth_signin()
1219+
1220+
if not api_key:
1221+
apologize_and_exit()
1222+
1223+
# Save API key
1224+
shell_rc_path = get_shell_rc_path()
1225+
if not shell_rc_path.exists() and os.name == "nt":
1226+
shell_rc_path.touch()
1227+
click.echo(f"✅ Created {shell_rc_path}")
1228+
1229+
result = save_api_key_to_rc(api_key)
1230+
if is_successful(result):
1231+
click.echo(result.unwrap())
1232+
click.echo("✅ Signed in successfully and API key saved!")
1233+
else:
1234+
click.echo(result.failure())
1235+
click.pause()
1236+
1237+
os.environ["CODEFLASH_API_KEY"] = api_key
1238+
ph("cli-oauth-signin-completed")
11911239
return True
11921240

11931241

0 commit comments

Comments
 (0)