@@ -36,7 +36,12 @@ def activate(profile_name, client=None):
3636 client_registry = ClientRegistry ()
3737 config_manager = ConfigManager ()
3838
39+ activate_this_client : bool = client is None
40+
3941 if client :
42+ if client == ClientRegistry .get_active_client ():
43+ activate_this_client = True
44+
4045 console .print (f"[bold cyan]Activating profile '{ profile_name } ' in client '{ client } '...[/]" )
4146 client_manager = ClientRegistry .get_client_manager (client )
4247 if client_manager is None :
@@ -54,9 +59,11 @@ def activate(profile_name, client=None):
5459 console .print (f"[bold red]Error:[/] Client '{ client } ' not found." )
5560 return
5661 success = client_manager .activate_profile (profile_name , config_manager .get_router_config ())
57- if success :
62+ if success and activate_this_client :
5863 client_registry .set_active_profile (profile_name )
5964 console .print (f"\n [green]Profile '{ profile_name } ' activated successfully.[/]\n " )
65+ elif success :
66+ console .print (f"\n [green]Profile '{ profile_name } ' activated successfully for client '{ client } '.[/]\n " )
6067 else :
6168 console .print (f"[bold red]Error:[/] Failed to activate profile '{ profile_name } '." )
6269
@@ -69,15 +76,20 @@ def deactivate(client=None):
6976
7077 Unsets the active profile.
7178 """
79+ deactivate_this_client : bool = client is None
80+
7281 # Set the active profile
7382 active_profile = ClientRegistry .get_active_profile ()
74- if active_profile is None :
83+ if deactivate_this_client and active_profile is None :
7584 console .print ("[bold yellow]No active profile found.[/]\n " )
7685 return
7786 console .print (f"\n [green]Deactivating profile '{ active_profile } '...[/]" )
7887 client_registry = ClientRegistry ()
7988
8089 if client :
90+ if client == ClientRegistry .get_active_client ():
91+ deactivate_this_client = True
92+
8193 console .print (f"[bold cyan]Deactivating profile '{ active_profile } ' in client '{ client } '...[/]" )
8294 client_manager = ClientRegistry .get_client_manager (client )
8395 if client_manager is None :
@@ -95,9 +107,11 @@ def deactivate(client=None):
95107 console .print (f"[bold red]Error:[/] Client '{ client } ' not found." )
96108 return
97109 success = client_manager .deactivate_profile ()
98- if success :
110+ if success and deactivate_this_client :
99111 client_registry .set_active_profile (None )
100112 console .print (f"\n [yellow]Profile '{ active_profile } ' deactivated successfully.[/]\n " )
113+ elif success :
114+ console .print (f"\n [yellow]Profile '{ active_profile } ' deactivated successfully for client '{ client } '.[/]\n " )
101115 else :
102116 console .print (f"[bold red]Error:[/] Failed to deactivate profile '{ active_profile } ' in client '{ client } '." )
103117
@@ -197,6 +211,22 @@ def remove(profile_name):
197211 if not profile_config_manager .delete_profile (profile_name ):
198212 console .print (f"[bold red]Error:[/] Profile '{ profile_name } ' not found." )
199213 return
214+ # Check whether any client is associated with the deleted profile
215+ clients = ClientRegistry .get_supported_clients ()
216+ for client in clients :
217+ client_manager = ClientRegistry .get_client_manager (client )
218+ if client_manager :
219+ profile_this_client_associated = client_manager .get_associated_profile ()
220+ if profile_this_client_associated == profile_name :
221+ # Deactivate the profile in this client
222+ client_manager .deactivate_profile ()
223+ console .print (f"\n [green]Profile '{ profile_name } ' deactivated successfully for client '{ client } '.[/]\n " )
224+
225+ # fresh the active_profile
226+ activated_profile = ClientRegistry .get_active_profile ()
227+ if activated_profile == profile_name :
228+ ClientRegistry .set_active_profile (None )
229+
200230 console .print (f"\n [green]Profile '{ profile_name } ' deleted successfully.[/]\n " )
201231
202232
@@ -212,4 +242,22 @@ def rename(profile_name):
212242 if not profile_config_manager .rename_profile (profile_name , new_profile_name ):
213243 console .print (f"[bold red]Error:[/] Profile '{ profile_name } ' not found." )
214244 return
245+ # Check whether any client is associated with the profile to be renamed
246+ clients = ClientRegistry .get_supported_clients ()
247+ config_manager = ConfigManager ()
248+ for client in clients :
249+ client_manager = ClientRegistry .get_client_manager (client )
250+ if client_manager :
251+ profile_this_client_associated = client_manager .get_associated_profile ()
252+ if profile_this_client_associated == profile_name :
253+ # fresh the config
254+ client_manager .deactivate_profile ()
255+ client_manager .activate_profile (new_profile_name , config_manager .get_router_config ())
256+ console .print (f"\n [green]Profile '{ profile_name } ' deactivated successfully for client '{ client } '.[/]\n " )
257+
258+ # fresh the active_profile
259+ activated_profile = ClientRegistry .get_active_profile ()
260+ if activated_profile == profile_name :
261+ ClientRegistry .set_active_profile (new_profile_name )
262+
215263 console .print (f"\n [green]Profile '{ profile_name } ' renamed to '{ new_profile_name } ' successfully.[/]\n " )
0 commit comments