88from .main import main
99
1010
11- def handle_duplicate_token_error (exc , ctx , opts , save_config , force ):
11+ def handle_duplicate_token_error (exc , ctx , opts , save_config , force , json ):
1212 """
1313 Handle the case where user already has a token.
1414
@@ -26,7 +26,7 @@ def handle_duplicate_token_error(exc, ctx, opts, save_config, force):
2626 ):
2727 return None
2828 return refresh_existing_token_interactive (
29- ctx , opts , save_config = save_config , force = force
29+ ctx , opts , save_config = save_config , force = force , json = json
3030 )
3131 raise exc
3232
@@ -137,7 +137,7 @@ def print_tokens(tokens):
137137
138138
139139def refresh_existing_token_interactive (
140- ctx , opts , token_slug = None , save_config = False , force = False
140+ ctx , opts , token_slug = None , save_config = False , force = False , json = False
141141):
142142 """Refresh an existing API token with interactive token selection, or create new if none exist."""
143143 context_msg = "Failed to refresh the token!"
@@ -159,8 +159,9 @@ def refresh_existing_token_interactive(
159159 click .echo ("No existing tokens found. Creating a new token instead..." )
160160 return _create (ctx , opts , save_config = save_config , force = force )
161161
162- click .echo ("Current tokens:" )
163- print_tokens (api_tokens )
162+ if not json :
163+ click .echo ("Current tokens:" )
164+ print_tokens (api_tokens )
164165
165166 if not force :
166167 token_slug = click .prompt (
@@ -169,9 +170,11 @@ def refresh_existing_token_interactive(
169170 else :
170171 # Use the first available slug_perm when force is enabled
171172 token_slug = api_tokens [0 ].slug_perm
172- click .echo (f"Using token { token_slug } (first available)" )
173+ if not json :
174+ click .echo (f"Using token { token_slug } (first available)" )
173175
174- click .echo (f"Refreshing token { token_slug } ... " , nl = False )
176+ if not json :
177+ click .echo (f"Refreshing token { token_slug } ... " , nl = False )
175178 try :
176179 with handle_api_exceptions (ctx , opts = opts , context_msg = context_msg ):
177180 with maybe_spinner (opts ):
@@ -186,8 +189,9 @@ def refresh_existing_token_interactive(
186189 if maybe_print_as_json (opts , new_token ):
187190 return
188191
189- click .secho ("OK" , fg = "green" )
190- click .echo (f"New token value: { click .style (new_token .key , fg = 'magenta' )} " )
192+ if not json :
193+ click .secho ("OK" , fg = "green" )
194+ click .echo (f"New token value: { click .style (new_token .key , fg = 'magenta' )} " )
191195
192196 return new_token
193197 except exceptions .ApiException as exc :
@@ -210,7 +214,10 @@ def _create(ctx, opts, save_config=False, force=False, json=False):
210214 return
211215 if maybe_print_as_json (opts , new_token ):
212216 return
213- click .echo (f"New token value: { click .style (new_token .key , fg = 'magenta' )} " )
217+ if not json :
218+ click .echo (
219+ f"New token value: { click .style (new_token .key , fg = 'magenta' )} "
220+ )
214221 return new_token
215222
216223 return new_token
@@ -223,6 +230,6 @@ def _create(ctx, opts, save_config=False, force=False, json=False):
223230 opts .output = "json"
224231 if exc .status == 400 :
225232 new_token = handle_duplicate_token_error (
226- exc , ctx , opts , save_config = save_config , force = force
233+ exc , ctx , opts , save_config = save_config , force = force , json = json
227234 )
228235 return new_token
0 commit comments