o
    i                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZmZ e eZd	efd
dZdS )    N)Path)AnyOption)	APIClient)	AppConfigget_app_configwrite_app_config)Identity)get_rich_toolkithandle_http_errorsreturnc                  C   s  t  } t b}|  s!|d |  |jddd tdt }t	|r>|d |  |jddd td|j
dd	d |  |d
@}t|dd* t }|d}|  | d }W d   n1 ssw   Y  W d   n1 sw   Y  W d   n1 sw   Y  |s|d td|  |jdddd |D d}|  |dF}t|dd0 t }|jdd|d id}|  | d }W d   n1 sw   Y  W d   n1 sw   Y  W d   n	1 sw   Y  |s#|d |  |jddd td|  |jddd d |D d}	|  t|	d |d d!}
t||
 |d"|	d#  d$ td%|	d  d&|d   W d   dS 1 smw   Y  dS )'zB
    Link a local directory to an existing FastAPI Cloud app.
    z2[error]You need to be logged in to link an app.[/]z1Run [bold]fastapi cloud login[/] to authenticate.tip)tag   z5[error]This directory is already linked to an app.[/]zMRun [bold]fastapi cloud unlink[/] first to remove the existing configuration.zLink to FastAPI CloudFastAPIzFetching teams...z-Error fetching teams. Please try again later.)default_messagez/teams/dataNz5[error]No teams found. Please create a team first.[/]zSelect the team:teamc                 S   ,   g | ]}t |d  |d |d  ddqS )nameid)r   r   r   valuer   ).0t r   S/home/ubuntu/.local/lib/python3.10/site-packages/fastapi_cloud_cli/commands/link.py
<listcomp>G       zlink.<locals>.<listcomp>)r   optionszFetching apps...z,Error fetching apps. Please try again later.z/apps/team_idr   )paramsz%[error]No apps found in this team.[/]zARun [bold]fastapi cloud deploy[/] to create and deploy a new app.zSelect the app to link:appc                 S   r   )slugr   )r   r$   r   r   )r   ar   r   r   r   h   r   )app_idr!   z!Successfully linked to app [bold]r$   u   [/bold]! 🔗zLinked to app: z
 in team: )r
   r   is_logged_inprint
print_linetyperExitr   cwdr   print_titleprogressr   r   getraise_for_statusjsonaskr   r	   loggerdebug)identitytoolkitpath_to_linkr.   clientresponse
teams_datar   	apps_datar#   
app_configr   r   r   link   s   





		
	
 $r=   )loggingpathlibr   typingr   r*   rich_toolkit.menur   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.appsr   r   r	   fastapi_cloud_cli.utils.authr
   fastapi_cloud_cli.utils.clir   r   	getLogger__name__r3   r=   r   r   r   r   <module>   s    
