Skip to content

Commit

Permalink
Fix default path for config and parent directories
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drGrove committed Oct 26, 2020
1 parent 9494d3b commit 23f0b9f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
19 changes: 8 additions & 11 deletions mtls/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import os
import pathlib
import sys
from configparser import ConfigParser
from datetime import datetime
Expand Down Expand Up @@ -104,7 +105,7 @@ def get_gpg_keys_for_email(email):
"--config",
"-c",
type=click.Path(),
default=os.path.join(HOME, "mtls/config.ini"),
default=os.path.join(HOME, "/mtls/config.ini"),
help=f"config file. [{HOME}/mtls/config.ini]",
)
@click.option("--gpg-password", type=str, hidden=True)
Expand Down Expand Up @@ -149,16 +150,12 @@ def config(ctx, key, value):
config_path = ctx.obj["config_path"]

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

if key not in ALLOWED_KEYS:
click.secho(AK_MSG, fg="red")
Expand Down
35 changes: 35 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,3 +1156,38 @@ def test_create_certificate(self):
if result.exception:
traceback.print_exception(*result.exc_info)
self.assertEqual(result.exit_code, 0, msg=result.exc_info)

class TestCliNoConfig(TestCliBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = {
"GNUPGHOME": cls.USER_GNUPGHOME.name,
"HOME": cls.HOME.name,
"XDG_CONFIG_HOME": f"{cls.HOME.name}/.config",
"USER": "test",
"HOST": str(platform.uname()[1]),
}
cls.runner = CliRunner(env=cls.env)
cls.config["DEFAULT"] = {
"name": "John Doe",
"email": "johndoe@example.com",
"fingerprint": cls.user.pgp_key.fingerprint,
"organization_name": "My Org",
}

def test_add_config_missing_file(self):
config_path = f"{self.env['XDG_CONFIG_HOME']}/mtls/config.ini"
result = self.runner.invoke(
cli,
[
"-c",
config_path,
"config",
"name",
"\"Test User\""
]
)
if result.exception:
traceback.print_exception(*result.exc_info)
self.assertEqual(result.exit_code, 0, msg=result.exc_info)

0 comments on commit 23f0b9f

Please sign in to comment.