Skip to content
Merged
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
52 changes: 50 additions & 2 deletions codeflash/cli_cmds/cmd_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from codeflash.code_utils.env_utils import check_formatter_installed, get_codeflash_api_key
from codeflash.code_utils.git_utils import get_git_remotes, get_repo_owner_and_name
from codeflash.code_utils.github_utils import get_github_secrets_page_url
from codeflash.code_utils.oauth_handler import perform_oauth_signin
from codeflash.code_utils.shell_utils import get_shell_rc_path, save_api_key_to_rc
from codeflash.either import is_successful
from codeflash.lsp.helpers import is_LSP_enabled
Expand Down Expand Up @@ -1166,10 +1167,13 @@ def convert(self, value: str, param: click.Parameter | None, ctx: click.Context

# Returns True if the user entered a new API key, False if they used an existing one
def prompt_api_key() -> bool:
"""Prompt user for API key via OAuth or manual entry."""
# Check for existing API key
try:
existing_api_key = get_codeflash_api_key()
except OSError:
existing_api_key = None

if existing_api_key:
display_key = f"{existing_api_key[:3]}****{existing_api_key[-4:]}"
api_key_panel = Panel(
Expand All @@ -1186,8 +1190,52 @@ def prompt_api_key() -> bool:
console.print()
return False

enter_api_key_and_save_to_rc()
ph("cli-new-api-key-entered")
# Prompt for authentication method
auth_choices = ["🔐 Login in with Codeflash", "🔑 Use Codeflash API key"]

questions = [
inquirer.List(
"auth_method",
message="How would you like to authenticate?",
choices=auth_choices,
default=auth_choices[0],
carousel=True,
)
]

answers = inquirer.prompt(questions, theme=CodeflashTheme())
if not answers:
apologize_and_exit()

method = answers["auth_method"]

if method == auth_choices[1]:
enter_api_key_and_save_to_rc()
ph("cli-new-api-key-entered")
return True

# Perform OAuth sign-in
api_key = perform_oauth_signin()

if not api_key:
apologize_and_exit()

# Save API key
shell_rc_path = get_shell_rc_path()
if not shell_rc_path.exists() and os.name == "nt":
shell_rc_path.touch()
click.echo(f"✅ Created {shell_rc_path}")

result = save_api_key_to_rc(api_key)
if is_successful(result):
click.echo(result.unwrap())
click.echo("✅ Signed in successfully and API key saved!")
else:
click.echo(result.failure())
click.pause()

os.environ["CODEFLASH_API_KEY"] = api_key
ph("cli-oauth-signin-completed")
return True


Expand Down
Loading
Loading