
    tni                         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 j        e          Zd	efd
ZdS )    N)Path)AnyOption)	APIClient)	AppConfigget_app_configwrite_app_config)Identity)get_rich_toolkithandle_http_errorsreturnc            
         t                      } t                      5 }|                                 sT|                    d           |                                 |                    dd           t          j        d          t          j                    }t          |          rT|                    d           |                                 |                    dd           t          j        d          |
                    dd	           |                                 |                    d
          5 }t          |d          5  t                      5 }|                    d          }|                                 |                                d         }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |s)|                    d           t          j        d          |                                 |                    ddd |D                       }|                                 |                    d          5 }t          |d          5  t                      5 }|                    dd|d         i          }|                                 |                                d         }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |sT|                    d           |                                 |                    dd           t          j        d          |                                 |                    ddd |D                       }	|                                 t%          |	d         |d                    }
t'          ||
           |                    d!|	d"          d#           t(                              d$|	d          d%|d                     ddd           dS # 1 swxY w 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.)messagez/teams/dataNz5[error]No teams found. Please create a team first.[/]zSelect the team:teamc                 \    g | ])}t          |d          |d         |d          dd          *S )nameid)r   r   r   valuer   ).0ts     \/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/fastapi_cloud_cli/commands/link.py
<listcomp>zlink.<locals>.<listcomp>F   L        &	1T7AfI4V4VWWXX      )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                 \    g | ])}t          |d          |d         |d          dd          *S )slugr   )r   r'   r   r   )r   as     r   r   zlink.<locals>.<listcomp>g   r    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_linkr1   clientresponse
teams_datar   	apps_datar%   
app_configs              r   linkr@      ss    zzH			 _Jw$$&& 		 MMD      MMC     *Q--xzz,'' 		 MMG      MM_     *Q--3CCC122 	9h#"Q   9 9 [[ 9F%zz)44H--///!)!8J9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 9 9 9 9 9 9 9	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9  	 MMG   *Q--{{ #    
 
 	011 	8X#"P   8 8 [[ 8F%zz(ItDz;RzSSH--/// ( 7I8 8 8 8 8 8 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8  		 MM7      MMS     *Q--kk% "    
 
 	c$idDDD
z222JFJJJ	
 	
 	
 	Hs4yHHDJHHIII_J _J _J _J _J _J _J _J _J _J _J _J _J _J _J _J _J _Js   DQ	<GGAF-	!G-F11G4F15G8GGGGGQ	G	Q	"G	#BQ	0LLAK+	L+K//L2K/3L6LLL	L
LQ	L	Q	 L	!DQ		QQ)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__r6   r@    r!   r   <module>rL      s                  $ $ $ $ $ $ 1 1 1 1 1 1 T T T T T T T T T T 1 1 1 1 1 1 L L L L L L L L		8	$	$eJc eJ eJ eJ eJ eJ eJr!   