Coverage for src/clauth/launcher.py: 90%

31 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-09-28 14:37 -0400

1# Copyright (c) 2025 Mahmood Khordoo 

2# 

3# This software is licensed under the MIT License. 

4# See the LICENSE file in the root directory for details. 

5 

6""" 

7CLAUTH Application Launcher. 

8 

9This module contains the core logic for launching the Claude Code CLI 

10with the correct environment configuration. 

11""" 

12 

13import os 

14import subprocess 

15import typer 

16from clauth.config import get_config_manager 

17from clauth.aws_utils import user_is_authenticated 

18from clauth.helpers import handle_authentication_failure, get_app_path, clear_screen, ExecutableNotFoundError 

19 

20 

21def launch_claude_cli(): 

22 """Launch Claude Code with proper environment variables from saved configuration.""" 

23 # Load configuration 

24 config_manager = get_config_manager() 

25 config = config_manager.load() 

26 

27 # Check if user is authenticated 

28 if not user_is_authenticated(profile=config.aws.profile): 

29 if not handle_authentication_failure(config.aws.profile): 

30 raise typer.Exit(1) 

31 

32 # Check if model settings are configured 

33 if not config.models.default_model_arn or not config.models.fast_model_arn: 

34 typer.secho("Model configuration missing. Run 'clauth init' for full setup.", fg=typer.colors.RED) 

35 raise typer.Exit(1) 

36 

37 # Set up environment variables 

38 env = os.environ.copy() 

39 env.update({ 

40 "AWS_PROFILE": config.aws.profile, 

41 "AWS_REGION": config.aws.region, 

42 "CLAUDE_CODE_USE_BEDROCK": "1", 

43 "ANTHROPIC_MODEL": config.models.default_model_arn, 

44 "ANTHROPIC_SMALL_FAST_MODEL_AWS_REGION": config.models.fast_model_arn, 

45 }) 

46 

47 # Launch Claude Code 

48 typer.secho("Launching Claude Code with Bedrock configuration...", fg=typer.colors.BLUE) 

49 try: 

50 claude_path = get_app_path(config.cli.claude_cli_name) 

51 clear_screen() 

52 subprocess.run([claude_path], env=env, check=True) 

53 except Exception as e: 

54 if "ExecutableNotFoundError" in str(type(e)): 

55 typer.secho(f"Launch failed: {e}", fg=typer.colors.RED) 

56 typer.secho("Please install Claude Code CLI and ensure it's in your PATH.", fg=typer.colors.YELLOW) 

57 else: 

58 typer.secho(f"Configuration error: {e}", fg=typer.colors.RED) 

59 raise typer.Exit(1) 

60 except subprocess.CalledProcessError as e: 

61 typer.secho(f"Failed to launch Claude Code. Exit code: {e.returncode}", fg=typer.colors.RED) 

62 raise typer.Exit(1)