3131 is_flag = True ,
3232 help = "Retrieve a user API token after successful authentication." ,
3333)
34+ @click .option (
35+ "-f" ,
36+ "--force" ,
37+ default = False ,
38+ is_flag = True ,
39+ help = "Force create a user API token after successful authentication." ,
40+ )
3441@decorators .common_cli_config_options
3542@decorators .common_cli_output_options
3643@decorators .initialise_api
3744@click .pass_context
38- def authenticate (ctx , opts , owner , token ):
45+ def authenticate (ctx , opts , owner , token , force ):
3946 """Authenticate to Cloudsmith using the org's SAML setup."""
4047 owner = owner [0 ].strip ("'[]'" )
4148 api_host = opts .api_config .host
@@ -75,18 +82,22 @@ def authenticate(ctx, opts, owner, token):
7582 try :
7683 api_token = user .create_user_token_saml ()
7784 click .echo (f"New token value: { click .style (api_token .key , fg = 'magenta' )} " )
78- create , has_errors = create_config_files (ctx , opts , api_key = api_token .key )
79- new_config_messaging (has_errors , opts , create , api_key = api_token .key )
80- return
85+
86+ if not token :
87+ create , has_errors = create_config_files (
88+ ctx , opts , api_key = api_token .key
89+ )
90+ new_config_messaging (has_errors , opts , create , api_key = api_token .key )
8191 except exceptions .ApiException as exc :
8292 if exc .status == 400 :
83- if "User has already created an API key" in exc .detail :
84- click .confirm (
85- "User already has a token. Would you like to recreate it?" ,
86- abort = True ,
87- )
88- else :
89- raise
93+ if not force :
94+ if "User has already created an API key" in exc .detail :
95+ click .confirm (
96+ "User already has a token. Would you like to recreate it?" ,
97+ abort = True ,
98+ )
99+ else :
100+ raise
90101
91102 context_msg = "Failed to refresh the token!"
92103 with handle_api_exceptions (ctx , opts = opts , context_msg = context_msg ):
@@ -98,15 +109,23 @@ def authenticate(ctx, opts, owner, token):
98109 f"Created: { click .style (t .created , fg = 'green' )} , "
99110 f"slug_perm: { click .style (t .slug_perm , fg = 'cyan' )} "
100111 )
101- token_slug = click .prompt (
102- "Please enter the slug_perm of the token you would like to refresh"
103- )
104112
105- click .echo (f"Refreshing token { token_slug } ... " , nl = False )
113+ if not force :
114+ token_slug = click .prompt (
115+ "Please enter the slug_perm of the token you would like to refresh"
116+ )
117+ click .echo (f"Refreshing token { token_slug } ... " , nl = False )
118+ else :
119+ # Use the first available slug_perm for simplicity
120+ token_slug = api_tokens [0 ].slug_perm
121+ click .echo (f"Refreshing token { token_slug } ... " , nl = False )
122+
106123 with handle_api_exceptions (ctx , opts = opts , context_msg = context_msg ):
107124 with maybe_spinner (opts ):
108125 new_token = user .refresh_user_token (token_slug )
109126 click .secho ("OK" , fg = "green" )
110127 click .echo (f"New token value: { click .style (new_token .key , fg = 'magenta' )} " )
111- create , has_errors = create_config_files (ctx , opts , api_key = new_token .key )
112- new_config_messaging (has_errors , opts , create , api_key = new_token .key )
128+
129+ if not force :
130+ create , has_errors = create_config_files (ctx , opts , api_key = new_token .key )
131+ new_config_messaging (has_errors , opts , create , api_key = new_token .key )
0 commit comments