7
7
import click
8
8
from click_configfile import ConfigFileReader , Param , SectionSchema , matches_section
9
9
10
+ from cloudsmith_cli .cli .warnings import ConfigLoadWarning , ProfileNotFoundWarning
11
+
10
12
from ..core .utils import get_data_path , read_file
11
13
from . import utils , validators
12
14
@@ -162,8 +164,9 @@ def config_already_warned(cls):
162
164
return False
163
165
164
166
@classmethod
165
- def load_config (cls , opts , path = None , profile = None , no_warn = False ):
167
+ def load_config (cls , opts , path = None , warnings = None , profile = None ):
166
168
"""Load a configuration file into an options object."""
169
+
167
170
if path and os .path .exists (path ):
168
171
if os .path .isdir (path ):
169
172
cls .config_searchpath .insert (0 , path )
@@ -174,36 +177,22 @@ def load_config(cls, opts, path=None, profile=None, no_warn=False):
174
177
values = config .get ("default" , {})
175
178
cls ._load_values_into_opts (opts , values )
176
179
177
- warn = not no_warn and not cls .config_already_warned ()
178
-
179
- if profile and profile != "default" and warn :
180
+ if profile and profile != "default" :
180
181
try :
181
182
values = config ["profile:%s" % profile ]
182
183
cls ._load_values_into_opts (opts , values )
183
184
except KeyError :
184
- if warn :
185
- click .secho (
186
- f"Warning: profile { profile } not found in config files { cls .config_files } " ,
187
- fg = "yellow" ,
188
- )
185
+ warning = ProfileNotFoundWarning (path = path , profile = profile )
186
+ warnings .append (warning )
189
187
190
188
existing_config_paths = {
191
189
path : os .path .exists (path ) for path in cls .config_files
192
190
}
193
- if not any (existing_config_paths .values ()) and warn :
194
- click .secho (
195
- "Warning: No config files found in search paths. Tried the following:" ,
196
- fg = "yellow" ,
197
- )
198
- for tested_path , exists in existing_config_paths .items ():
199
- if exists :
200
- click .secho (f"{ tested_path } - file exists" , fg = "green" )
201
- else :
202
- click .secho (f"{ tested_path } - file does not exist" , fg = "yellow" )
203
- click .secho (
204
- "You may need to run `cloudsmith login` to authenticate and create a config file." ,
205
- fg = "yellow" ,
191
+ if not any (list (existing_config_paths .values ())):
192
+ config_load_warning = ConfigLoadWarning (
193
+ paths = existing_config_paths ,
206
194
)
195
+ warnings .append (config_load_warning )
207
196
208
197
return values
209
198
@@ -248,7 +237,7 @@ class CredentialsReader(ConfigReader):
248
237
config_section_schemas = [CredentialsSchema .Default , CredentialsSchema .Profile ]
249
238
250
239
@classmethod
251
- def load_config (cls , opts , path = None , profile = None , no_warn = False ):
240
+ def load_config (cls , opts , path = None , warnings = None , profile = None ):
252
241
"""
253
242
Load a credentials configuration file into an options object.
254
243
We overload the load_config command in CredentialsReader as
@@ -292,15 +281,17 @@ def get_creds_reader():
292
281
"""Get the credentials config reader class."""
293
282
return CredentialsReader
294
283
295
- def load_config_file (self , path , profile = None , no_warn = False ):
284
+ def load_config_file (self , path , warnings = None , profile = None ):
296
285
"""Load the standard config file."""
286
+ print ("load_config_file" )
297
287
config_cls = self .get_config_reader ()
298
- return config_cls .load_config (self , path , profile = profile , no_warn = no_warn )
288
+ return config_cls .load_config (self , path , warnings = warnings , profile = profile )
299
289
300
- def load_creds_file (self , path , profile = None , no_warn = False ):
290
+ def load_creds_file (self , path , warnings = None , profile = None ):
301
291
"""Load the credentials config file."""
292
+ print ("load_creds_file" )
302
293
config_cls = self .get_creds_reader ()
303
- return config_cls .load_config (self , path , profile = profile , no_warn = no_warn )
294
+ return config_cls .load_config (self , path , warnings = warnings , profile = profile )
304
295
305
296
@property
306
297
def api_config (self ):
0 commit comments