o
    ;i}                     @   s  d dl Z d dlZd dl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Zd dlZd dlmZ d dlmZm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 d dlmZ d dlm Z  d dl!m"Z" ej#ddddZ$e$j%dddej&edfde
e' de(fddZ)e$j%dddej&ejdddeejdd dejdd!dejdd"d#dfd$e'd%e
e*e'  de
e' d&e
e d'e
e d(e(fd)dZ+e$j%d*d+dej&ed,dfedd-d.deed/d0e'd1e(d2e(de
e' fd3d*Z,d4e'fd5d6Z-dS )7    N)datetime)Path)NamedTemporaryFile)Optional)Syntax)ArgumentOption)synchronizer)timestamp_to_localized_str)
ENV_OPTION
YES_OPTIONdisplay_table)_Client)
ensure_env)OutputManager)_Secret)api_pb2secretzManage secrets.T)namehelpno_args_is_helplistzList your published secrets.)r   Fenvjsonc              	      s  t t I d H  g dtdtf fdd}|t  I d H }	 |r*n|d jj	j
I d H }q' fddD }g }t|D ]%\}}| I d H }||jt|j
 ||j|jrht|j|nd	g qGrud
 dnd}	g d}
t|
||d|	 d d S )Ncreated_beforereturnc                    sN   d}t j|| d}t j|d} j|I d H }|j t|j|k S )Nd   )max_objectsr   )environment_name
pagination)r   ListPaginationSecretListRequeststub
SecretListextenditemslen)r   max_page_sizer   reqrespclientr   r%    D/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/secret.pyretrieve_page*   s   zlist_.<locals>.retrieve_pageTc                    s"   g | ]}t j|j |jd dqS )T)is_another_app)r   _new_hydrated	secret_idmetadata).0item)r+   r,   r-   
<listcomp>8   s   " zlist_.<locals>.<listcomp>-z in environment '' )Namez
Created atz
Created byzLast used atSecrets)title)r   r   from_envfloatboolr   now	timestampr3   creation_info
created_atzipinfoappendr   r
   
created_bylast_used_atr   )r   r   r.   finishedsecretsrowsobj	resp_datarE   env_partcolumn_namesr,   r*   r-   list_   s2   	rP   createzCreate a new secret.z Space-separated KEY=VALUE items.)defaultr   z)Path to a .env file to load secrets from.z)Path to a JSON file to load secrets from.z--forcez*Overwrite the secret if it already exists.secret_name	keyvaluesfrom_dotenv	from_jsonforcec                    s  t |}i }|p
g D ]}d|v r&|dd\}}	|	dkr!t|}	|	||< qtd|rn| s9td| zddlm}
 W n tyL   tdw z	|	|
| W n t
ym } ztd	| d
| d }~ww |r| s|td| z|d}|	t| W d    n1 sw   Y  W n t
y } ztd| d
| d }~ww |std| D ]"\}}t|tr|std| dt|tstd| dq|rtj| ||dI d H  n
tj| |I d H  t }|rddd | D nd}d|  d| d}t|dkr dnd}|d|  d| ddd d | D   |d! |t|d" d S )#N=   r7   a/  Each item should be of the form <KEY>=VALUE. To enter secrets using your $EDITOR, use `<KEY>=-`. To
enter secrets from environment variables, use `<KEY>="$ENV_VAR"`.

E.g.

modal secret create my-credentials username=john password=-
modal secret create my-credentials username=john password="$PASSWORD"
zCould not read .env file at r   )dotenv_valueszfNeed the `python-dotenv` package installed. You can install it by running `pip install python-dotenv`.zCould not parse .env file at z: zCould not read JSON file at rzCould not parse JSON file at z4You need to specify at least one key for your secretzInvalid key: 'r8   zNon-string value for secret ')	overwritez
    c                 s   s    | ]	}d | dV  qdS )zos.getenv("z")Nr,   )r4   r   r,   r,   r-   	<genexpr>   s    zcreate.<locals>.<genexpr>z...z0
@app.function(secrets=[modal.Secret.from_name("z")])
def some_function():
    
sr9   zCreated a new secret 'z' with the key z, c                 s   s    | ]}t |V  qd S )N)repr)r4   kr,   r,   r-   r]      s    z
Use it in your Modal app:
python)r   splitget_text_from_editorclick
UsageErroris_filedotenvrZ   ImportErrorupdate	Exceptionopenr   loadr%   
isinstancestrr   _create_deployedobjectsrQ   r   getjoinkeysr&   printr   )rS   rT   r   rU   rV   rW   env_dictargkeyvaluerZ   efrb   voutputenv_var_codeexample_codeplural_sr,   r,   r-   rQ   K   s   



"
deletezDelete a named Secret.z6Name of the modal.Secret to be deleted. Case sensitivez--allow-missingz(Don't error if the Secret doesn't exist.)allow_missingyesr   r   r   r   c                   sB   t |}|stjd|  dddd tjj| ||dI d H  d S )Nz>Are you sure you want to irrevocably delete the modal.Secret 'z'?FT)rR   abort)r   r   )r   typerconfirmr   rr   r   )r   r   r   r   r,   r,   r-   r      s   	
r   c                 C   s   t ddddC}t dkr(tdd}td| d	|  d
 t||jg}n
td t|j}|dkr:t	d|
d | W  d    S 1 sMw   Y  d S )Nzw+secret_bufferz.txt)prefixsuffixWindowsEDITORviz-Pressing enter will open an external editor (z) for editing 'z'...zTPressing enter will open an external editor to allow you to edit the secret value...r   zvSomething went wrong with the external editor. Try again, or use '--' as the value to pass input through stdin instead)r   platformsystemosgetenvinput
subprocesscallr   
ValueErrorseekread)ry   
bufferfileeditorstatus_coder,   r,   r-   re      s   
$re   ).r   r   r   r   r   pathlibr   tempfiler   typingr   rf   r   rich.syntaxr   r   r   modal._utils.async_utilsr	   modal._utils.time_utilsr
   modal.cli.utilsr   r   r   modal.clientr   modal.environmentsr   modal.outputr   modal.secretr   modal_protor   Typer
secret_clicommandcreate_blockingrp   r?   rP   r   rQ   r   re   r,   r,   r,   r-   <module>   sz    -
X