Skip to content

Commit 268d689

Browse files
authored
new: Allow specifying a custom configuration path through environment variable (#652)
1 parent e7ed7a4 commit 268d689

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

linodecli/configuration/config.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ def update(
290290
):
291291
print(f"User {username} is not configured.")
292292
sys.exit(ExitCodes.USERNAME_ERROR)
293-
if not self.config.has_section(username) or allowed_defaults is None:
293+
if (
294+
not self.config.has_section(username)
295+
and self.config.default_section is None
296+
) or allowed_defaults is None:
294297
return namespace
295298

296299
warn_dict = {}
@@ -335,12 +338,6 @@ def write_config(self):
335338
to save values they've set, and is used internally to update the config
336339
on disk when a new user if configured.
337340
"""
338-
339-
# Create the config path isf necessary
340-
config_path = f"{os.path.expanduser('~')}/.config"
341-
if not os.path.exists(config_path):
342-
os.makedirs(config_path)
343-
344341
with open(_get_config_path(), "w", encoding="utf-8") as f:
345342
self.config.write(f)
346343

linodecli/configuration/helpers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"XDG_CONFIG_HOME", f"{os.path.expanduser('~')}/.config"
1616
)
1717

18+
ENV_CONFIG_FILE_PATH = "LINODE_CLI_CONFIG"
19+
1820
# this is a list of browser that _should_ work for web-based auth. This is mostly
1921
# intended to exclude lynx and other terminal browsers which could be opened, but
2022
# won't work.
@@ -38,11 +40,23 @@ def _get_config_path() -> str:
3840
:returns: The path to the local config file.
3941
:rtype: str
4042
"""
43+
custom_path = os.getenv(ENV_CONFIG_FILE_PATH, None)
44+
45+
if custom_path is not None:
46+
custom_path = os.path.expanduser(custom_path)
47+
if not os.path.exists(custom_path):
48+
os.makedirs(os.path.dirname(custom_path), exist_ok=True)
49+
return custom_path
50+
4151
path = f"{LEGACY_CONFIG_DIR}/{LEGACY_CONFIG_NAME}"
4252
if os.path.exists(path):
4353
return path
4454

45-
return f"{CONFIG_DIR}/{CONFIG_NAME}"
55+
path = f"{CONFIG_DIR}/{CONFIG_NAME}"
56+
if not os.path.exists(path):
57+
os.makedirs(CONFIG_DIR, exist_ok=True)
58+
59+
return path
4660

4761

4862
def _get_config(load: bool = True):

linodecli/help_pages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"(e.g. 'v4beta')",
3131
"LINODE_CLI_API_SCHEME": "Overrides the target scheme used for API requests. "
3232
"(e.g. 'https')",
33+
"LINODE_CLI_CONFIG": "Overrides the default configuration file path. "
34+
"(e.g '~/.linode/my-cli-config')",
3335
}
3436

3537
HELP_TOPICS = {

tests/unit/test_configuration.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,26 @@ def test_bool_input_default(self, monkeypatch):
609609
output = stdout_buf.getvalue()
610610
assert "foo [y/N]: " in output
611611
assert result
612+
613+
def test_custom_config_path(self, monkeypatch, tmp_path):
614+
"""
615+
Test use a custom configuration path
616+
"""
617+
conf = self._build_test_config()
618+
custom_path = tmp_path / "test-cli-config"
619+
620+
with (
621+
patch.dict(
622+
os.environ,
623+
{"LINODE_CLI_CONFIG": custom_path.absolute().as_posix()},
624+
),
625+
):
626+
conf.write_config()
627+
628+
configs = custom_path.read_text().splitlines()
629+
expected_configs = self.mock_config_file.splitlines()
630+
631+
assert len(configs) == len(expected_configs) + 1
632+
633+
for i, _ in enumerate(expected_configs):
634+
assert expected_configs[i] == configs[i]

wiki/Configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ environment variable.
4141
If you wish to hide the API Version warning you can use the `LINODE_CLI_SUPPRESS_VERSION_WARNING`
4242
environment variable.
4343

44+
You may also specify a custom configuration path using the `LINODE_CLI_CONFIG` environment variable
45+
to replace the default path `~/.config/linode-cli`.
46+
4447
## Configurable API URL
4548

4649
In some cases you may want to run linode-cli against a non-default Linode API URL.

wiki/development/Development - Overview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ configure the following:
5151
- Overrides for the target API URL (hostname, version, scheme, etc.)
5252

5353
This command serves as an interactive prompt and outputs a configuration file to `~/.config/linode-cli`.
54-
This file is in a simple INI format and can be easily modified manually by users.
54+
This file is in a simple INI format and can be easily modified manually by users.
55+
You may also specify a custom configuration file path using the `LINODE_CLI_CONFIG` environment variable.
5556

5657
Additionally, multiple users can be created for the CLI which can be designated when running commands using the `--as-user` argument
5758
or using the `default-user` config variable.

0 commit comments

Comments
 (0)