Skip to content

Commit

Permalink
Add suppress-version-warning config option (#635)
Browse files Browse the repository at this point in the history
## 📝 Description

**What does this PR do and why is this change necessary?**

Adds `get_bool('value')` method for configuration options and adds a step in configuration to support `suppress-version-warning`.
Also adds support for `LINODE_CLI_SUPPRESS_VERSION_WARNING` environment variable.

## ✔️ How to Test

**What are the steps to reproduce the issue or verify the changes?**

1. Install the new version of the CLI
```bash
make install
```
2. Go through the configuration process
```bash
lin configure
```
3. The last question should ask you about suppressing API Version Warnings
4. Use the CLI and see if you view this warning

### Environment variable

1. Install the new CLI or delete the value from your config
2. Use the cli after setting the environment variable
```bash
export LINODE_CLI_SUPPRESS_VERSION_WARNING=true
lin linodes ls
unset $LINODE_CLI_SUPPRESS_VERSION_WARNING
lin linodes ls
```
3. Verify the output doesn't have the error.

**How do I run the relevant unit/integration tests?**

```bash
make testunit
```

resolves #582
  • Loading branch information
jriddle-linode authored Sep 3, 2024
1 parent 5e6003b commit c0a860c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
7 changes: 5 additions & 2 deletions linodecli/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import itertools
import json
import os
import sys
import time
from typing import Any, Iterable, List, Optional
Expand Down Expand Up @@ -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 "
Expand Down
27 changes: 27 additions & 0 deletions linodecli/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -265,6 +274,7 @@ def test_configure_no_default_terminal(self):
"foobar.linode.com",
"v4beta",
"https",
"n",
]
)

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions wiki/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c0a860c

Please sign in to comment.