22import time
33from pathlib import Path
44from typing import Optional
5+ from uuid import uuid4
56
67import questionary
78import requests
1516from codecarbon import __app_name__ , __version__
1617from codecarbon .cli .cli_utils import (
1718 create_new_config_file ,
19+ get_api_enabled ,
1820 get_api_endpoint ,
1921 get_config ,
2022 get_existing_local_exp_id ,
@@ -177,13 +179,29 @@ def config():
177179 else :
178180 file_path = create_new_config_file ()
179181
182+ api_enabled = get_api_enabled ()
183+ api_enabled = typer .prompt (
184+ f"API is currently { 'enabled' if api_enabled == '1' else 'disabled' } . Press enter to continue or change to 0/1" ,
185+ type = str ,
186+ default = "0" ,
187+ )
188+ overwrite_local_config ("api_enabled" , api_enabled , path = file_path )
189+
190+ if api_enabled == "0" :
191+ overwrite_local_config ("experiment_id" , str (uuid4 ()), path = file_path )
192+ return
193+
180194 api_endpoint = get_api_endpoint (file_path )
181195 api_endpoint = typer .prompt (
182- f"Current API endpoint is { api_endpoint } . Press enter to continue or input other url" ,
196+ f"Current API endpoint is at { api_endpoint } . Press enter to continue or input other url" ,
183197 type = str ,
184198 default = api_endpoint ,
185199 )
186200 overwrite_local_config ("api_endpoint" , api_endpoint , path = file_path )
201+
202+ print (f"Logging into the auth server at { AUTH_SERVER_URL } " )
203+ fief_auth .authorize ()
204+
187205 api = ApiClient (endpoint_url = api_endpoint )
188206 api .set_access_token (_get_access_token ())
189207 organizations = api .get_list_organizations ()
@@ -305,8 +323,8 @@ def monitor(
305323 int , typer .Argument (help = "Number of measures between API calls." )
306324 ] = 30 ,
307325 api : Annotated [
308- bool , typer .Option (help = "Choose to call Code Carbon API or not" )
309- ] = True ,
326+ Optional [ bool ] , typer .Option (help = "Choose to call Code Carbon API or not" )
327+ ] = None ,
310328):
311329 """Monitor your machine's carbon emissions.
312330
@@ -315,6 +333,8 @@ def monitor(
315333 api_call_interval (Annotated[int, typer.Argument, optional): Number of measures before calling API. Defaults to 30.
316334 api (Annotated[bool, typer.Option, optional): Choose to call Code Carbon API or not. Defaults to True.
317335 """
336+ if api is None :
337+ api = get_api_enabled () == "1"
318338 experiment_id = get_existing_local_exp_id ()
319339 if api and experiment_id is None :
320340 print ("ERROR: No experiment id, call 'codecarbon init' first." , err = True )
0 commit comments