@@ -77,6 +77,7 @@ class ConfigReader(ConfigFileReader):
77
77
config_name = "standard"
78
78
config_searchpath = list (_CFG_SEARCH_PATHS )
79
79
config_section_schemas = [ConfigSchema .Default , ConfigSchema .Profile ]
80
+ config_warning_issued = False
80
81
81
82
@classmethod
82
83
def select_config_schema_for (cls , section_name ):
@@ -147,6 +148,19 @@ def has_default_file(cls):
147
148
148
149
return False
149
150
151
+ @classmethod
152
+ def config_already_warned (cls ):
153
+ """
154
+ Check if a configuration file warning has been issued.
155
+ This is required as configs are gathered at the root of the
156
+ command chain as well as for command verbs
157
+ """
158
+ if cls .config_warning_issued :
159
+ return True
160
+
161
+ cls .config_warning_issued = True
162
+ return False
163
+
150
164
@classmethod
151
165
def load_config (cls , opts , path = None , profile = None ):
152
166
"""Load a configuration file into an options object."""
@@ -161,8 +175,32 @@ def load_config(cls, opts, path=None, profile=None):
161
175
cls ._load_values_into_opts (opts , values )
162
176
163
177
if profile and profile != "default" :
164
- values = config .get ("profile:%s" % profile , {})
165
- cls ._load_values_into_opts (opts , values )
178
+ try :
179
+ values = config ["profile:%s" % profile ]
180
+ cls ._load_values_into_opts (opts , values )
181
+ except KeyError :
182
+ click .secho (
183
+ f"Warning: profile { profile } not found in config files { cls .config_files } " ,
184
+ fg = "yellow" ,
185
+ )
186
+
187
+ existing_config_paths = {
188
+ path : os .path .exists (path ) for path in cls .config_files
189
+ }
190
+ if not any (existing_config_paths .values ()) and not cls .config_already_warned ():
191
+ click .secho (
192
+ "Warning: No config files found in search paths. Tried the following:" ,
193
+ fg = "yellow" ,
194
+ )
195
+ for tested_path , exists in existing_config_paths .items ():
196
+ if exists :
197
+ click .secho (f"{ tested_path } - file exists" , fg = "green" )
198
+ else :
199
+ click .secho (f"{ tested_path } - file does not exist" , fg = "yellow" )
200
+ click .secho (
201
+ "You may need to run `cloudsmith login` to authenticate and create a config file." ,
202
+ fg = "yellow" ,
203
+ )
166
204
167
205
return values
168
206
@@ -206,6 +244,29 @@ class CredentialsReader(ConfigReader):
206
244
config_searchpath = list (_CFG_SEARCH_PATHS )
207
245
config_section_schemas = [CredentialsSchema .Default , CredentialsSchema .Profile ]
208
246
247
+ @classmethod
248
+ def load_config (cls , opts , path = None , profile = None ):
249
+ """
250
+ Load a credentials configuration file into an options object.
251
+ We overload the load_config command in CredentialsReader as
252
+ credentials files have their own specific default functionality.
253
+ """
254
+ if path and os .path .exists (path ):
255
+ if os .path .isdir (path ):
256
+ cls .config_searchpath .insert (0 , path )
257
+ else :
258
+ cls .config_files .insert (0 , path )
259
+
260
+ config = cls .read_config ()
261
+ values = config .get ("default" , {})
262
+ cls ._load_values_into_opts (opts , values )
263
+
264
+ if profile and profile != "default" :
265
+ values = config .get ("profile:%s" % profile , {})
266
+ cls ._load_values_into_opts (opts , values )
267
+
268
+ return values
269
+
209
270
210
271
class Options :
211
272
"""Options object that holds config for the application."""
0 commit comments