Skip to content

Commit 23f0b9f

Browse files
committed
Fix default path for config and parent directories
The default path was missing a / causing issues for calls without a -c option. When using the `mtls config` option and the config file or any parent directories don't exist they should be created.
1 parent 9494d3b commit 23f0b9f

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

mtls/cli.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22
import os
3+
import pathlib
34
import sys
45
from configparser import ConfigParser
56
from datetime import datetime
@@ -104,7 +105,7 @@ def get_gpg_keys_for_email(email):
104105
"--config",
105106
"-c",
106107
type=click.Path(),
107-
default=os.path.join(HOME, "mtls/config.ini"),
108+
default=os.path.join(HOME, "/mtls/config.ini"),
108109
help=f"config file. [{HOME}/mtls/config.ini]",
109110
)
110111
@click.option("--gpg-password", type=str, hidden=True)
@@ -149,16 +150,12 @@ def config(ctx, key, value):
149150
config_path = ctx.obj["config_path"]
150151

151152
if not os.path.exists(config_path):
152-
if config_path != f"{HOME}/mtls/config.ini":
153-
click.secho(
154-
f"Config file not found, please run `mtls -c {config_path} init`",
155-
fg="red",
156-
)
157-
else:
158-
click.secho(
159-
"Config file not found, please run `mtls init`", fg="red"
160-
)
161-
sys.exit(1)
153+
config = ConfigParser()
154+
config["DEFAULT"] = {}
155+
config_dir = pathlib.Path(config_path).parent
156+
config_dir.mkdir(parents=True, exist_ok=True)
157+
with open(config_path, "w") as config_file:
158+
config.write(config_file)
162159

163160
if key not in ALLOWED_KEYS:
164161
click.secho(AK_MSG, fg="red")

test/test_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,3 +1156,38 @@ def test_create_certificate(self):
11561156
if result.exception:
11571157
traceback.print_exception(*result.exc_info)
11581158
self.assertEqual(result.exit_code, 0, msg=result.exc_info)
1159+
1160+
class TestCliNoConfig(TestCliBase):
1161+
@classmethod
1162+
def setUpClass(cls):
1163+
super().setUpClass()
1164+
cls.env = {
1165+
"GNUPGHOME": cls.USER_GNUPGHOME.name,
1166+
"HOME": cls.HOME.name,
1167+
"XDG_CONFIG_HOME": f"{cls.HOME.name}/.config",
1168+
"USER": "test",
1169+
"HOST": str(platform.uname()[1]),
1170+
}
1171+
cls.runner = CliRunner(env=cls.env)
1172+
cls.config["DEFAULT"] = {
1173+
"name": "John Doe",
1174+
"email": "johndoe@example.com",
1175+
"fingerprint": cls.user.pgp_key.fingerprint,
1176+
"organization_name": "My Org",
1177+
}
1178+
1179+
def test_add_config_missing_file(self):
1180+
config_path = f"{self.env['XDG_CONFIG_HOME']}/mtls/config.ini"
1181+
result = self.runner.invoke(
1182+
cli,
1183+
[
1184+
"-c",
1185+
config_path,
1186+
"config",
1187+
"name",
1188+
"\"Test User\""
1189+
]
1190+
)
1191+
if result.exception:
1192+
traceback.print_exception(*result.exc_info)
1193+
self.assertEqual(result.exit_code, 0, msg=result.exc_info)

0 commit comments

Comments
 (0)