o
    iL                     @   s  d dl Z d dlmZ d dlmZ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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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eej&d%d&df def
d'd(Z'dS )+    N)Path)	AnnotatedAnyOptionalUnion)	BaseModel)	APIClient)get_app_config)Identity)get_rich_toolkithandle_http_errors)"validate_environment_variable_namec                   @   s&   e Zd ZU eed< dZee ed< dS )EnvironmentVariablenameNvalue)__name__
__module____qualname__str__annotations__r   r    r   r   R/home/ubuntu/.local/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   listr   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(   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_variable.   s   


"r+   pathz:A path to the folder containing the app you want to deploy)helpc              
   C   s>  t  }tdd}| s|jddd td| pt }t|}|s3|d| d td|j	d	dd
!}t
| t|j}W d   n1 sOw   Y  W d   n1 s^w   Y  |jst|d 	 W d   dS |d |  |jD ]}|d|j d qW 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   is_logged_inprinttyperExitr   cwdr	   progressr   r%   r   r   
print_liner   )r,   identitytoolkitapp_path
app_configr@   environment_variablesenv_varr   r   r   r   <   sD   






"r   z.The name of the environment variable to deletec           	   
   C   s  t  }tdd}| s|jddd td|pt }t|}|s3|d| d td| s|j	d	dd
!}t
| t|j}W d   n1 sQw   Y  W d   n1 s`w   Y  |jsv|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.   r0   r1   r2   r4   r5   r6   r7   r8   Nr:   z*Select the environment variable to delete:c                 S   s   g | ]	}|j |j d qS ))r   r   )r   ).0rG   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   r;   r<   r=   r>   r   r?   r	   r@   r   r%   r   r   askr   rA   r(   )	r   r,   rB   rC   path_to_deployrE   r@   rF   deletedr   r   r   r&   o   st   




 





"r&   z+The name of the environment variable to setz,The value of the environment variable to setsecretz--secretz'Mark the environment variable as secretc           	   
   C   s  t  }tdd}| s|jddd td|pt }t|}|s3|d| d td| sB|r=|	d	} n|	d
} |sS|rN|j	ddd}n|	d}|j
ddd0}| dusaJ |dusgJ t| t|j| || W d   n1 s~w   Y  W d   n1 sw   Y  |r|d|  d n|d|  d W d   dS W d   dS 1 sw   Y  dS )z2
    Set an environment variable for the app.
    Tr.   r0   r1   r2   r4   r5   r6   z$Enter the name of the secret to set:z2Enter the name of the environment variable to set:zEnter the secret value:)passwordz,Enter the value of the environment variable:zSetting environment variabler8   Nz"Secret environment variable [bold]z[/] set.rK   )r
   r   r;   r<   r=   r>   r   r?   r	   inputr@   r   r+   r   )	r   r   r,   rO   rB   rC   rM   rE   r@   r   r   r   set   sT   




	+"rR   )F)N)(loggingpathlibr   typingr   r   r   r   r=   pydanticr   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Argumentr   r&   OptionrR   r   r   r   r   <module>   s    


2	

Q	

	
