o
    #1 ib                     @   s  d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ e eZG dd de	ZG dd de	ZdedefddZdedede fddZ!	d(dededede ddf
ddZ"e# Z$e$% 	d)deeedf ej&ddf defdd Z'e$% ej&dd!ddfdeedf deeedf ej&ddf defd"d#Z(e$% ej&dd$dej&dd%ddfdeedf deedf deeedf ej&ddf defd&d'Z)dS )*    N)Path)AnyListUnion)	BaseModel)	Annotated)	APIClient)get_app_config)is_logged_in)get_rich_toolkithandle_http_errors)"validate_environment_variable_name)model_validatec                   @   s   e Zd ZU eed< eed< dS )EnvironmentVariablenamevalueN)__name__
__module____qualname__str__annotations__ r   r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/fastapi_cloud_cli/commands/env.pyr      s   
 r   c                   @   s   e Zd ZU ee ed< dS )EnvironmentVariableResponsedataN)r   r   r   r   r   r   r   r   r   r   r      s   
 r   app_idreturnc                 C   sR   t  }|d|  d}|  tt| W  d    S 1 s"w   Y  d S )N/apps//environment-variables/)r   getraise_for_statusr   r   json)r   clientresponser   r   r   _get_environment_variables   s
   $r$   r   c                 C   sV   t  }|d|  d| }W d    n1 sw   Y  |jdkr%dS |  dS )Nr   r   i  FT)r   deletestatus_coder    )r   r   r"   r#   r   r   r   _delete_environment_variable$   s   
r'   Fr   	is_secretc                 C   sR   t  }|jd|  d|||dd}|  W d    d S 1 s"w   Y  d S )Nr   r   )r   r   r(   )r!   )r   postr    )r   r   r   r(   r"   r#   r   r   r   _set_environment_variable0   s   


"r*   pathz:A path to the folder containing the app you want to deploy)helpc              
   C   s6  t dd}t s|jddd td| pt }t|}|s/|d| d td|jd	dd
!}t	| t
|j}W d   n1 sKw   Y  W d   n1 sZw   Y  |jsp|d 	 W d   dS |d |  |jD ]}|d|j d q|W d   dS 1 sw   Y  dS )z5
    List the environment variables for the app.
    TZminimal<No credentials found. Use [blue]`fastapi login`[/] to login.authtag   !No app found in the folder [bold][/].!Fetching environment variables...	transientNNo environment variables found.zEnvironment variables:z[bold]z[/])r   r
   printtyperExitr   cwdr	   progressr   r$   r   r   
print_liner   )r+   toolkitZapp_path
app_configr=   environment_variablesenv_varr   r   r   list>   sB   






"rC   z.The name of the environment variable to deletec              
   C   s  t dd}t s|jddd td|pt }t|}|s/|d| d td| s|jd	dd
!}t	| t
|j}W d   n1 sMw   Y  W d   n1 s\w   Y  |jsr|d 	 W d   dS |jddd |jD d} | sJ nt| s|d|  d td|  |jddd
"}t	| t|j| }W d   n1 sw   Y  W d   n1 sw   Y  |s|d td|d|  d W d   dS 1 sw   Y  dS )z6
    Delete an environment variable from the app.
    Tr-   r.   r/   r0   r2   r3   r4   r5   r6   Nr8   z*Select the environment variable to delete:c                 S   s   g | ]	}|j |j d qS ))r   r   )r   ).0rB   r   r   r   
<listcomp>   s    zdelete.<locals>.<listcomp>)optionsz$The environment variable name [bold]z[/] is invalid.zDeleting environment variablezEnvironment variable not found.Environment variable [bold]z[/] deleted.)r   r
   r9   r:   r;   r   r<   r	   r=   r   r$   r   r   askr   r>   r'   )r   r+   r?   path_to_deployr@   r=   rA   Zdeletedr   r   r   r%   o   sr   




!





"r%   z+The name of the environment variable to setz,The value of the environment variable to setc              
   C   s0  t dd}t s|jddd td|pt }t|}|s/|d| d td| s6|d	} |s?|jd
dd}|j	ddd/}| dusMJ |dusSJ t
| t|j| | W d   n1 siw   Y  W d   n1 sxw   Y  |d|  d W d   dS 1 sw   Y  dS )z2
    Set an environment variable for the app.
    Tr-   r.   r/   r0   r2   r3   r4   z2Enter the name of the environment variable to set:z3Enter the value of the environment variable to set:)passwordzSetting environment variabler6   NrG   z[/] set.)r   r
   r9   r:   r;   r   r<   r	   inputr=   r   r*   r   )r   r   r+   r?   rI   r@   r=   r   r   r   set   sB   




	"rL   )F)N)*loggingpathlibr   typingr   r   r   r:   Zpydanticr   Ztyping_extensionsr   Zfastapi_cloud_cli.utils.apir   Zfastapi_cloud_cli.utils.appsr	   Zfastapi_cloud_cli.utils.authr
   Zfastapi_cloud_cli.utils.clir   r   Zfastapi_cloud_cli.utils.envr   Z'fastapi_cloud_cli.utils.pydantic_compatr   	getLoggerr   loggerr   r   r   r$   boolr'   r*   ZTyperZenv_appcommandZArgumentrC   r%   rL   r   r   r   r   <module>   s    


0	

P	

	
