
                             S r SSKrSSKrSSKrSSKJr  SSKJr  SSKrSSK	r	SSK
r
SSKrSSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr   " S S\R0                  5      rg)zThe BigQuery init CLI command.    N)Optional)flags)client_project)bigquery_command)bq_cached_client)utils)utils_flags)utils_formatting)bq_id_utils)
bq_logging)bq_processor_utilsc                      ^  \ rS rSrSrS\S\R                  4U 4S jjrS\	4S jr
S\\   4S jrS\\   4S	 jrS
rU =r$ )Init   z7Create a .bigqueryrc file and set up OAuth credentials.namefvc                 h   > [         [        U ]  X5        SU l        [        R
                  " SSSUS9  g )NFdelete_credentialszTIf specified, the credentials file associated with this .bigqueryrc file is deleted.)flag_values)superr   __init__surface_in_shellr   DEFINE_boolean)selfr   r   	__class__s      $platform/bq/frontend/command_init.pyr   Init.__init__!   s6    	$t(!D		    returnc                     g)z/Init never needs to call itself before running.F )r   s    r   
_NeedsInitInit._NeedsInit,   s    r   c                    [         R                  " 5         [        R                  R                  =(       d    [        R
                  R                  n[        R                  R                  U5      (       d  [        SU< S35        g S[        R                  " SU< S35      :w  a  [        SU< S35        g[        R                  " U5        g! [         a  n[        S	U< S
U< 35         SnAgSnAff = f)z$Deletes this user's credential file.zCredential file z does not exist.r   yzDelete credential file ? (y/N) zNOT deleting 
, exiting.Error removing : N   )bq_utilsProcessBigqueryrcbq_auth_flagsSERVICE_ACCOUNT_CREDENTIAL_FILEvalueCREDENTIAL_FILEospathexistsprintfrontend_utilsPromptYNremoveOSError)r   filenamees      r   DeleteCredentialsInit.DeleteCredentials0   s     55;; 	/((..  77>>(##H>?		''19
; 
 	X78ii x34s   /C 7C 
C5C00C5c           	      
   Sn[         R                  " SR                  [        R                  " USS95      SS/[
        R                  S9  [        R                  R                  n[        R                  " 5         [        R                  " [        R                  R                  5        U R                  (       a  U R!                  5       $ [        R"                  " 5       n[%        5         [%        S5        [%        S	5        [%        5         [&        R(                  R+                  U5      (       a  [%        S
5        [%        SU< S35        [%        S5        [%        S5        [%        5         S[,        R.                  " SU< S35      :w  a  [%        SU< S35        g[%        5          [&        R0                  " U5        [%        S5        [%        S5        [%        5         [4        R6                  R9                  5       nS[:        R<                  R                  0nU(       a  [%        SU-  5        [%        5         X&S'   GOq[>        R@                  " URB                  SS9n[%        S 5        [%        5         U(       d-  [%        S!5        [%        S"5        [%        S#5        [%        5         GO	[%        S$5        [D        RF                  " 5       nURI                  S%5        [J        RL                  " U[N        RP                  RR                  5        [U        U5       HB  u  p[J        RV                  " U
5      nURY                  S%U	S-   05        UR[                  U5        MD     UR]                  5         [_        U5      S:X  aC  [`        Rb                  " US   5      n[%        S&U< S'35        [%        5         URd                  US'   O[%        S(5        [%        S)5        [%        S*5        [%        5         Sn[g        U[h        5      (       dy  [,        Rj                  " S+[_        U5      < S,35      n U(       a"  S[i        U5      s=::  a  [_        U5      ::  a  O  O[i        U=(       d    S5      n[g        U[h        5      (       d  My  [%        5         U(       a*  [`        Rb                  " X}S-
     5      nURd                  US'    [o        US-5       nURq                  5        H  u  nn[%        U< S.U< 3US/9  M     SSS5        [%        S15        [%        5         g! [2         a  n[%        SU< SU< 35         SnAgSnAff = f! [l         a     Nf = f! , (       d  f       N^= f! [r         a  n[%        S0U< SU< 35         SnAgSnAff = f)2z3Authenticate and create a default .bigqueryrc file.zBQ CLI will soon require all users to log in using `gcloud auth login`. `bq init` will no longer handle authentication after January 1, 2024.

P   )widthredbold)colorattrsfilez;Welcome to BigQuery! This script will walk you through the z<process of initializing your .bigqueryrc configuration file.z **** NOTE! ****z*An existing .bigqueryrc file was found at .z>Are you sure you want to continue and overwrite your existing zconfiguration?r%   z
Overwrite r&   zNOT overwriting r'   r   r(   r)   Nr*   z9First, we need to set up your credentials if they do not zalready exist.credential_filez%Setting project_id %s as the default.
project_idi  )	apiclientmax_resultszCCredential creation complete. Now we will select a default project.z.No projects found for this user. Please go to z#  https://console.cloud.google.com/zand create a project.zList of projects:#z Found only one project, setting z as the default.z6Found multiple projects. Please enter a selection for z3which should be the default, or leave blank to not zset a default.zEnter a selection (1 - z): wz = )rE   zError writing z:BigQuery configuration complete! Type "bq" to get started.):	termcolorcprintjointextwrapwrapsysstdoutbq_flags
PROJECT_IDr/   r+   r,   r   ConfigureLoggingAPILOGr   r;   GetBigqueryRcFilenamer4   r1   r2   r3   r5   r6   r7   r8   r   ClientGetr-   r0   r   list_projectsrI   r	   get_formatter_from_flags	AddColumnr
   configure_formatterr   ApiClientHelperProjectReference	enumerateformat_project_infoupdateAddDictPrintlenr   ConstructObjectReference	projectId
isinstanceintPromptWithDefault
ValueErroropenitemsIOError)r   messageproject_id_flag
bigqueryrcr:   cliententriesprojects	formatterindexprojectresultproject_referenceresponsercfileflagr/   s                    r   RunWithArgsInit.RunWithArgsE   s4   	3 
 		(--r23hZZ	 ))//O  5 56##%%//1J	G	
GH	
HI	G	ww~~j!!zKLLMg	''*(NO	O
<=g
		*
 

EF	
	G
 $$((*F -"?"?"E"EFG3oEFg-l--$$$h 
O g>?34%&!"88:	C ,,{22CC	
 (1NE#77@&
--eai(
)


F
# 2 	x=A0IIqk
 "% '"3"="=',

H
I
E
F
 
!
'(x--%7725h-AHc(m!Ds8}!Dx}1- x-- ' 2 K KA&! %6$?$?GL!
C F"==?KD%
T5)
7 + ! 

FG	Go  Q78H   !   j!45sf   "S? (T) T) )U
 5,T9!U
 ?
T&	T!!T&)
T65T69
UU
 U
 

U1U,,U1)r   )__name__
__module____qualname____firstlineno____doc__strr   
FlagValuesr   boolr"   r   rj   r;   r~   __static_attributes____classcell__)r   s   @r   r   r      sS    ?	3 	E$4$4 	$ # *8C=  r   r   )r   r1   rR   rP   typingr   abslr   rM   r-   rT   r+   clientsr   frontendr   r   r   r5   r	   r
   r   r   r   BigqueryCmdr   r!   r   r   <module>r      sS    $ 	 
        " % % ,   %   $f'' fr   