Coverage for src/clauth/commands/delete.py: 85%

52 statements  

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

1import typer 

2from rich.console import Console 

3from clauth.config import get_config_manager 

4from clauth.aws_utils import ( 

5 delete_aws_profile, 

6 delete_aws_credentials_profile, 

7 clear_sso_cache, 

8 remove_sso_session, 

9) 

10 

11console = Console() 

12 

13 

14def delete( 

15 confirm: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"), 

16): 

17 """ 

18 Deletes all CLAUTH configurations and associated AWS profile data. 

19 

20 This command provides a complete cleanup by: 

21 - Deleting the entire CLAUTH configuration directory. 

22 - Removing the associated AWS profile from `~/.aws/config`. 

23 - Clearing the AWS SSO token cache. 

24 - Removing any related SSO session configurations. 

25 """ 

26 # Load configuration to get profile name 

27 config_manager = get_config_manager() 

28 config = config_manager.load() 

29 profile = config.aws.profile 

30 

31 # Show what will be deleted 

32 console.print("\n[bold red]WARNING: DELETE OPERATION[/bold red]") 

33 console.print("This will permanently delete the following:") 

34 console.print(f" - AWS profile: [yellow]{profile}[/yellow]") 

35 console.print(" - AWS credentials profile (if it exists)") 

36 console.print(" - SSO token cache") 

37 console.print(" - SSO session configuration") 

38 console.print(" - [bold red]ENTIRE CLAUTH configuration directory[/bold red]") 

39 console.print() 

40 

41 # Confirmation 

42 if not confirm: 

43 if not typer.confirm("Are you sure you want to proceed with the deletion?"): 

44 console.print("[yellow]Delete operation cancelled.[/yellow]") 

45 raise typer.Exit(0) 

46 

47 success = True 

48 console.print("\n[bold blue]Starting delete operation...[/bold blue]") 

49 

50 # Step 1: Delete AWS profile and credentials 

51 console.print(f"\n[bold]Step 1: Deleting AWS profile '{profile}'[/bold]") 

52 if not delete_aws_profile(profile): 

53 success = False 

54 if not delete_aws_credentials_profile(profile): 

55 success = False 

56 

57 # Step 2: Clear SSO token cache 

58 console.print("\n[bold]Step 2: Clearing SSO token cache[/bold]") 

59 if not clear_sso_cache(profile): 

60 success = False 

61 

62 # Step 3: Remove SSO session configuration 

63 console.print("\n[bold]Step 3: Removing SSO session configuration[/bold]") 

64 if not remove_sso_session("claude-auth"): 

65 success = False 

66 if not remove_sso_session(config.aws.session_name): 

67 success = False 

68 

69 # Step 4: Delete CLAUTH configuration directory 

70 console.print(f"\n[bold]Step 4: Deleting CLAUTH configuration[/bold]") 

71 try: 

72 import shutil 

73 if config_manager.config_dir.exists(): 

74 shutil.rmtree(config_manager.config_dir) 

75 console.print( 

76 f"[green]SUCCESS: Completely removed config directory: {config_manager.config_dir}[/green]" 

77 ) 

78 else: 

79 console.print("[yellow]Config directory already doesn't exist.[/yellow]") 

80 except Exception as e: 

81 console.print(f"[red]ERROR: Failed to delete CLAUTH configuration: {e}[/red]") 

82 success = False 

83 

84 # Final status 

85 console.print() 

86 if success: 

87 console.print("[bold green]SUCCESS: Deletion completed successfully![/bold green]") 

88 else: 

89 console.print("[bold red]WARNING: Deletion completed with some errors.[/bold red]") 

90 console.print("Check the messages above for details.") 

91 raise typer.Exit(1)