o
    !h                     @   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 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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_namec                   @   s   e Zd ZU eed< eed< dS )EnvironmentVariablenamevalueN)__name__
__module____qualname__str__annotations__ r   r   `/var/www/html/pro-man-master/venv/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| W  d    S 1 s"w   Y  d S )N/apps//environment-variables/)r   getraise_for_statusr   model_validate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'   r   c                 C   sN   t  }|jd|  d||id}|  W d    d S 1 s w   Y  d S )Nr   r   )r!   )r   patchr   )r   r   r   r"   r#   r   r   r   _set_environment_variable/   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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*   toolkitapp_path
app_configr=   environment_variablesenv_varr   r   r   list;   sB   






"rD   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   ).0rC   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_deployrA   r=   rB   deletedr   r   r   r%   l   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   NrH   z[/] set.)r   r
   r9   r:   r;   r   r<   r	   inputr=   r   r)   r   )r   r   r*   r?   rJ   rA   r=   r   r   r   set   sB   




	"rN   )N)(loggingpathlibr   typingr   r   r   r:   pydanticr   typing_extensionsr   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.appsr	   fastapi_cloud_cli.utils.authr
   fastapi_cloud_cli.utils.clir   r   fastapi_cloud_cli.utils.envr   	getLoggerr   loggerr   r   r   r$   boolr'   r)   Typerenv_appcommandArgumentrD   r%   rN   r   r   r   r   <module>   s    
	
0	

P	

	
