11import os
2+ import toml
23from dataclasses import MISSING
4+ from pathlib import Path
35
46import click
57import dotenv
68
7- from beaker_kernel .lib .config import locate_envfile , config
9+ from beaker_kernel .lib .config import locate_envfile , locate_config , config , ConfigClass
810from beaker_kernel .lib .autodiscovery import LIB_LOCATIONS
911
1012
@@ -24,11 +26,11 @@ def find():
2426 """
2527 Locate the configuration file for currently in use
2628 """
27- envfile = locate_envfile ()
28- if os .path .isfile (envfile ):
29- click .echo (f"Configuration location: { envfile } \n " )
29+ config_file = locate_config ()
30+ if os .path .isfile (config_file ):
31+ click .echo (f"Configuration location: { config_file } \n " )
3032 else :
31- click .echo (f"No configuration file located.\n Default location is: { envfile } \n " )
33+ click .echo (f"No configuration file located.\n Default location is: { config_file } \n " )
3234
3335
3436@config_group .command ()
@@ -37,28 +39,29 @@ def show(sensitive):
3739 """
3840 Displays the current Beaker configuration
3941 """
40- envfile : str = locate_envfile ()
41- if os . path . isfile ( envfile ):
42- dotenv_config = dotenv . dotenv_values ( envfile )
42+ config_file : Path | None = locate_config ()
43+ if config_file and config_file . exists ( ):
44+ config_data = toml . loads ( config_file . read_text () )
4345 else :
44- dotenv_config = {}
46+ config_data = {}
4547 has_sensitive = False
4648
4749 click .echo (f"Current Beaker configuration:\n " )
4850 for field_name , field in config .__dataclass_fields__ .items ():
49- env_var : str = field .metadata .get ("env_var " , None )
51+ config_var : str = field .metadata .get ("config_var " , None )
5052 description : str = field .metadata .get ("description" , "(No description provided)" )
5153 is_sensitive : bool = field .metadata .get ("sensitive" , False )
5254 aliases : list [str ] = field .metadata .get ("aliases" , None ) or []
5355
5456 value = getattr (config , field_name )
5557
5658 exists_in_config_file = False
57- env_vars_to_check = [env_var , * aliases ]
58- for env_var in env_vars_to_check :
59- if env_var in dotenv_config :
59+
60+ vars_to_check = [field_name , * aliases ]
61+ for config_name in vars_to_check :
62+ if config_name in config_data :
6063 exists_in_config_file = True
61- dotenv_config .pop (env_var )
64+ config_data .pop (config_var )
6265 break
6366
6467 if is_sensitive and not sensitive :
@@ -73,13 +76,20 @@ def show(sensitive):
7376 click .echo (f"# { field_name } : { description } " )
7477 click .echo (f"{ field_name } ={ display_value } " , nl = False )
7578 if not exists_in_config_file :
76- click .echo (" # default not defined in config file" , nl = False )
79+ click .echo (" # ( default) - Not defined in config file" , nl = False )
7780 click .echo ("\n " )
7881
79- if dotenv_config :
80- click .echo ("\n The following environment variables are defined in the configuration file but not defined in the "
81- "Beaker configuration:" )
82- for var_name , value in dotenv_config .items ():
82+ tools_enabled = config_data .pop ('tools_enabled' , {})
83+ if tools_enabled :
84+ click .echo ("The following tools' have are enabled if true or disabled if false:" )
85+ for tool , enabled in tools_enabled .items ():
86+ click .echo (f" { tool } - { enabled } " )
87+ click .echo ("" )
88+
89+ if config_data :
90+ click .echo ("\n The following config variables are defined in the configuration file but are not defined "
91+ "in the Beaker configuration class:" )
92+ for var_name , value in config_data .items ():
8393 if sensitive :
8494 click .echo (f" { var_name } ={ value } " )
8595 else :
@@ -97,22 +107,21 @@ def update(envfile):
97107 Iterate through available options, prompting for new or updated values.
98108 """
99109 updates = {}
100- if envfile is None :
101- envfile = locate_envfile ()
102- envfile_exists = os .path .isfile (envfile )
103- if envfile_exists :
104- dotenv_config = dotenv .dotenv_values (envfile )
110+
111+ config_file = locate_config ()
112+ if config_file and config_file .exists ():
113+ config_data = toml .loads (config_file .read_text ())
105114 else :
106- dotenv_config = {}
115+ config_data = {}
107116
108117 for field_name , field in config .__dataclass_fields__ .items ():
109- env_var = field .metadata .get ("env_var " , None )
118+ config_var = field .metadata .get ("config_var " , None )
110119 description = field .metadata .get ("description" , "(No description provided)" )
111120 is_sensitive = field .metadata .get ("sensitive" , False )
112- if not env_var :
113- click . echo ( f"Skipping config option ' { field_name } as no environment variable has been defined." )
114- if env_var in dotenv_config :
115- default = dotenv_config .get (env_var )
121+ save_default_value = field . metadata . get ( "save_default_value" , False )
122+
123+ if config_var in config_data :
124+ default = config_data .get (config_var )
116125 else :
117126 if field .default is not MISSING :
118127 default = field .default
@@ -131,20 +140,21 @@ def update(envfile):
131140Description: { description }
132141
133142{ field_name } """
134- value = click .prompt (prompt , default = default_display ,)
135- if value == SENSITIVE_STR_REPR :
136- value = default
137- if value != dotenv_config .get (env_var , MISSING ):
138- updates [env_var ] = value
143+ value = click .prompt (prompt , default = default_display )
144+ was_value_entered = (value != default_display )
145+ is_value_different = (value != config_data .get (config_var , MISSING ))
146+ if was_value_entered and is_value_different :
147+ updates [config_var ] = value
148+ elif save_default_value and is_value_different :
149+ updates [config_var ] = default
139150
140151 if updates :
141152 click .echo ("Updated values:" )
142153 click .echo (f"\n " .join (f" { varname } ={ value } " for varname , value in updates .items ()))
143- approval = click .prompt (f"Save above values to file { envfile } ?" , default = "y" , type = bool )
154+ approval = click .prompt (f"Save above values to file { config_file } ?" , default = "y" , type = bool )
144155 if isinstance (approval , bool ) and approval :
145156 click .echo ("Saving..." )
146- for varname , value in updates .items ():
147- dotenv .set_key (envfile , varname , value , quote_mode = "never" )
157+ config .update (updates , config_file )
148158 click .echo ("Done." )
149159 else :
150160 click .echo ("Aborting without saving." )
0 commit comments