diff --git a/linodecli/api_request.py b/linodecli/api_request.py index d3bf909f1..a0e63137e 100644 --- a/linodecli/api_request.py +++ b/linodecli/api_request.py @@ -4,6 +4,7 @@ import itertools import json +import os import sys import time from typing import Any, Iterable, List, Optional @@ -373,8 +374,10 @@ def _attempt_warn_old_version(ctx, result): "with --suppress-warnings", file=sys.stderr, ) - - if new_version_exists: + suppress_version_warning = ctx.config.get_bool("suppress-version-warning") or os.getenv( + "LINODE_CLI_SUPPRESS_VERSION_WARNING" + ) + if new_version_exists and not suppress_version_warning: print( f"The API responded with version {spec_version}, which is newer than " f"the CLI's version of {ctx.spec_version}. Please update the CLI to get " diff --git a/linodecli/configuration/config.py b/linodecli/configuration/config.py index 48226a148..88c557bd4 100644 --- a/linodecli/configuration/config.py +++ b/linodecli/configuration/config.py @@ -186,6 +186,30 @@ def get_value(self, key: str) -> Optional[Any]: return self.config.get(username, key) + def get_bool(self, key: str) -> bool: + """ + Retrieves and returns an existing config boolean for the current user. This + is intended for plugins to use instead of having to deal with figuring out + who the current user is when accessing their config. + + .. warning:: + Plugins _MUST NOT_ set values for the user's config except through + ``plugin_set_value`` below. + + :param key: The key to look up. + :type key: str + + :returns: The boolean for that key, or False if the key doesn't exist for the + current user. + :rtype: any + """ + username = self.username or self.default_username() + + if not self.config.has_option(username, key): + return False + + return self.config.getboolean(username, key) + # plugin methods - these are intended for plugins to utilize to store their # own persistent config information def plugin_set_value(self, key: str, value: Any): @@ -449,6 +473,9 @@ def configure( if _bool_input("Configure a custom API target?", default=False): self._configure_api_target(config) + if _bool_input("Suppress API Version Warnings?", default=False): + config["suppress-version-warning"] = "true" + # save off the new configuration if username != "DEFAULT" and not self.config.has_section(username): self.config.add_section(username) diff --git a/tests/unit/test_configuration.py b/tests/unit/test_configuration.py index ff85944cc..2c43a078c 100644 --- a/tests/unit/test_configuration.py +++ b/tests/unit/test_configuration.py @@ -38,6 +38,7 @@ class TestConfiguration: plugin-testplugin-testkey = plugin-test-value authorized_users = cli-dev mysql_engine = mysql/8.0.26 +suppress-version-warning = true [cli-dev2] token = {test_token}2 @@ -156,6 +157,14 @@ def test_get_value(self): assert conf.get_value("notakey") == None assert conf.get_value("region") == "us-east" + def test_get_bool(self): + """ + Test CLIConfig.get_bool({key}) + """ + conf = self._build_test_config() + assert conf.get_bool("notakey") == False + assert conf.get_bool("suppress-version-warning") == True + def test_plugin_set_value(self): """ Test CLIConfig.plugin_set_value({key}, {value}) @@ -265,6 +274,7 @@ def test_configure_no_default_terminal(self): "foobar.linode.com", "v4beta", "https", + "n", ] ) @@ -319,7 +329,7 @@ def test_configure_default_terminal(self): """ conf = configuration.CLIConfig(self.base_url, skip_config=True) - answers = iter(["1", "1", "1", "1", "1", "1", "n"]) + answers = iter(["1", "1", "1", "1", "1", "1", "n", "n"]) def mock_input(prompt): if not prompt: diff --git a/wiki/Configuration.md b/wiki/Configuration.md index 879dc6d18..ed595da48 100644 --- a/wiki/Configuration.md +++ b/wiki/Configuration.md @@ -38,6 +38,9 @@ without having a configuration file, which is desirable in some situations. You may also specify the path to a custom Certificate Authority file using the `LINODE_CLI_CA` environment variable. +If you wish to hide the API Version warning you can use the `LINODE_CLI_SUPPRESS_VERSION_WARNING` +environment variable. + ## Configurable API URL In some cases you may want to run linode-cli against a non-default Linode API URL.