Skip to content

Commit 67c82e1

Browse files
committed
add --no-warn flag, consolidate decorators
1 parent e681b34 commit 67c82e1

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

cloudsmith_cli/cli/commands/repos.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def repositories(ctx, opts): # pylink: disable=unused-argument
7575
@decorators.common_cli_output_options
7676
@decorators.common_api_auth_options
7777
@decorators.initialise_api
78-
@decorators.verify_authenticated
7978
@click.argument(
8079
"owner_repo",
8180
metavar="OWNER/REPO",

cloudsmith_cli/cli/config.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def config_already_warned(cls):
162162
return False
163163

164164
@classmethod
165-
def load_config(cls, opts, path=None, profile=None):
165+
def load_config(cls, opts, path=None, profile=None, no_warn=False):
166166
"""Load a configuration file into an options object."""
167167
if path and os.path.exists(path):
168168
if os.path.isdir(path):
@@ -174,12 +174,14 @@ def load_config(cls, opts, path=None, profile=None):
174174
values = config.get("default", {})
175175
cls._load_values_into_opts(opts, values)
176176

177+
warn = not no_warn and not cls.config_already_warned()
178+
177179
if profile and profile != "default":
178180
try:
179181
values = config["profile:%s" % profile]
180182
cls._load_values_into_opts(opts, values)
181183
except KeyError:
182-
if not cls.config_already_warned():
184+
if warn:
183185
click.secho(
184186
f"Warning: profile {profile} not found in config files {cls.config_files}",
185187
fg="yellow",
@@ -188,7 +190,7 @@ def load_config(cls, opts, path=None, profile=None):
188190
existing_config_paths = {
189191
path: os.path.exists(path) for path in cls.config_files
190192
}
191-
if not any(existing_config_paths.values()) and not cls.config_already_warned():
193+
if not any(existing_config_paths.values()) and warn:
192194
click.secho(
193195
"Warning: No config files found in search paths. Tried the following:",
194196
fg="yellow",
@@ -246,7 +248,7 @@ class CredentialsReader(ConfigReader):
246248
config_section_schemas = [CredentialsSchema.Default, CredentialsSchema.Profile]
247249

248250
@classmethod
249-
def load_config(cls, opts, path=None, profile=None):
251+
def load_config(cls, opts, path=None, profile=None, no_warn=False):
250252
"""
251253
Load a credentials configuration file into an options object.
252254
We overload the load_config command in CredentialsReader as
@@ -269,6 +271,7 @@ def load_config(cls, opts, path=None, profile=None):
269271
return values
270272

271273

274+
# pylint: disable=too-many-public-methods
272275
class Options:
273276
"""Options object that holds config for the application."""
274277

@@ -289,15 +292,15 @@ def get_creds_reader():
289292
"""Get the credentials config reader class."""
290293
return CredentialsReader
291294

292-
def load_config_file(self, path, profile=None):
295+
def load_config_file(self, path, profile=None, no_warn=False):
293296
"""Load the standard config file."""
294297
config_cls = self.get_config_reader()
295-
return config_cls.load_config(self, path, profile=profile)
298+
return config_cls.load_config(self, path, profile=profile, no_warn=no_warn)
296299

297-
def load_creds_file(self, path, profile=None):
300+
def load_creds_file(self, path, profile=None, no_warn=False):
298301
"""Load the credentials config file."""
299302
config_cls = self.get_creds_reader()
300-
return config_cls.load_config(self, path, profile=profile)
303+
return config_cls.load_config(self, path, profile=profile, no_warn=no_warn)
301304

302305
@property
303306
def api_config(self):
@@ -330,6 +333,16 @@ def api_host(self, value):
330333
"""Set value for API host."""
331334
self._set_option("api_host", value)
332335

336+
@property
337+
def no_warn(self):
338+
"""Get value for API host."""
339+
return self._get_option("no_warn")
340+
341+
@no_warn.setter
342+
def no_warn(self, value):
343+
"""Set value for API host."""
344+
self._set_option("no_warn", value)
345+
333346
@property
334347
def api_key(self):
335348
"""Get value for API key."""

cloudsmith_cli/cli/decorators.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def common_cli_config_options(f):
8787
envvar="CLOUDSMITH_PROFILE",
8888
help="The name of the profile to use for configuration.",
8989
)
90+
@click.option(
91+
"--no-warn",
92+
is_flag=True,
93+
default=None,
94+
help="Don't warn on misconfiguration issues",
95+
)
9096
@click.pass_context
9197
@functools.wraps(f)
9298
def wrapper(ctx, *args, **kwargs):
@@ -95,8 +101,11 @@ def wrapper(ctx, *args, **kwargs):
95101
profile = kwargs.pop("profile")
96102
config_file = kwargs.pop("config_file")
97103
creds_file = kwargs.pop("credentials_file")
98-
opts.load_config_file(path=config_file, profile=profile)
99-
opts.load_creds_file(path=creds_file, profile=profile)
104+
no_warn = kwargs.pop("no_warn")
105+
if no_warn:
106+
opts.no_warn = no_warn
107+
opts.load_config_file(path=config_file, profile=profile, no_warn=no_warn)
108+
opts.load_creds_file(path=creds_file, profile=profile, no_warn=no_warn)
100109
kwargs["opts"] = opts
101110
return ctx.invoke(f, *args, **kwargs)
102111

@@ -306,27 +315,8 @@ def call_print_rate_limit_info_with_opts(rate_info):
306315
error_retry_cb=opts.error_retry_cb,
307316
)
308317

309-
kwargs["opts"] = opts
310-
return ctx.invoke(f, *args, **kwargs)
311-
312-
return wrapper
313-
314-
315-
def verify_authenticated(f):
316-
"""Verify that the user is authenticated and warn if not."""
317-
318-
@click.option(
319-
"--no-warn",
320-
default=False,
321-
is_flag=True,
322-
help="Don't warn on misconfiguration issues",
323-
)
324-
@click.pass_context
325-
@functools.wraps(f)
326-
def wrapper(ctx, *args, **kwargs):
327318
cloudsmith_host = kwargs["opts"].opts["api_config"].host
328-
print(kwargs["opts"].opts["api_config"].__dict__)
329-
no_warn = kwargs.pop("no_warn")
319+
no_warn = opts.no_warn
330320
is_auth, _, _, _ = get_user_brief()
331321
if not is_auth and not no_warn:
332322
click.secho(
@@ -339,10 +329,8 @@ def wrapper(ctx, *args, **kwargs):
339329
f"You're currently attempting to connect to Cloudsmith instance {cloudsmith_host}",
340330
fg="yellow",
341331
)
342-
no_warn = True
332+
opts.no_warn = True
343333

344-
opts = config.get_or_create_options(ctx)
345-
opts.no_warn = no_warn
346334
kwargs["opts"] = opts
347335
return ctx.invoke(f, *args, **kwargs)
348336

0 commit comments

Comments
 (0)