2021-04-18 02:27:05 +02:00
|
|
|
"""Config management."""
|
|
|
|
|
|
|
|
import json
|
2021-05-13 01:25:50 +02:00
|
|
|
import logging
|
2021-05-29 22:07:55 +02:00
|
|
|
from pathlib import Path
|
2021-04-18 02:27:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"connect_timeout": 10,
|
|
|
|
"text_width": 80,
|
2021-05-09 23:02:56 +02:00
|
|
|
"download_path": "",
|
2021-04-18 02:27:05 +02:00
|
|
|
"source_editor": ["vi"],
|
|
|
|
"command_editor": ["vi"],
|
2021-05-09 00:46:26 +02:00
|
|
|
"history_limit": 1000,
|
2021-05-09 01:39:33 +02:00
|
|
|
"external_commands": {},
|
2021-05-15 19:44:50 +02:00
|
|
|
"external_command_default": ["xdg-open"],
|
|
|
|
"home": "bebop:welcome",
|
2021-05-29 01:13:43 +02:00
|
|
|
"render_mode": "fancy",
|
2021-05-29 16:43:44 +02:00
|
|
|
"generate_client_cert_command": [
|
|
|
|
"openssl", "req",
|
|
|
|
"-newkey", "rsa:4096",
|
|
|
|
"-nodes",
|
|
|
|
"-keyform", "PEM",
|
|
|
|
"-keyout", "{key_path}",
|
2021-06-13 01:55:15 +02:00
|
|
|
"-utf8",
|
2021-05-29 16:43:44 +02:00
|
|
|
"-x509",
|
|
|
|
"-days", "28140", # https://www.youtube.com/watch?v=F9L4q-0Pi4E
|
|
|
|
"-outform", "PEM",
|
|
|
|
"-out", "{cert_path}",
|
|
|
|
"-subj", "/CN={common_name}",
|
|
|
|
],
|
2021-05-31 18:53:26 +02:00
|
|
|
"scroll_step": 3,
|
2021-06-03 16:45:35 +02:00
|
|
|
"persistent_history": False,
|
2021-06-04 16:09:00 +02:00
|
|
|
"enabled_plugins": [],
|
2021-04-18 02:27:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-29 01:13:43 +02:00
|
|
|
RENDER_MODES = ("fancy", "dumb")
|
|
|
|
|
2021-04-18 02:27:05 +02:00
|
|
|
|
2021-05-29 22:07:55 +02:00
|
|
|
def load_config(config_path: Path):
|
|
|
|
if not config_path.is_file():
|
2021-04-18 02:27:05 +02:00
|
|
|
create_default_config(config_path)
|
|
|
|
return DEFAULT_CONFIG
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(config_path, "rt") as config_file:
|
|
|
|
config = json.load(config_file)
|
|
|
|
except OSError as exc:
|
2021-05-29 22:07:55 +02:00
|
|
|
abs_path = config_path.absolute()
|
|
|
|
logging.error(f"Could not read config file {abs_path}: {exc}")
|
2021-04-18 02:27:05 +02:00
|
|
|
except ValueError as exc:
|
2021-05-29 22:07:55 +02:00
|
|
|
abs_path = config_path.absolute()
|
|
|
|
logging.error(f"Could not parse config file {abs_path}: {exc}")
|
2021-04-18 02:27:05 +02:00
|
|
|
else:
|
|
|
|
# Fill missing values with defaults.
|
|
|
|
for key, value in DEFAULT_CONFIG.items():
|
|
|
|
if key not in config:
|
|
|
|
config[key] = value
|
|
|
|
return config
|
|
|
|
return DEFAULT_CONFIG
|
|
|
|
|
|
|
|
|
2021-05-29 22:07:55 +02:00
|
|
|
def create_default_config(config_path: Path):
|
|
|
|
config_dir = config_path.parent
|
|
|
|
if not config_dir.is_dir():
|
|
|
|
try:
|
|
|
|
config_dir.mkdir(parents=True)
|
|
|
|
except OSError as exc:
|
|
|
|
logging.error(f"Could not create config dir {config_dir}: {exc}")
|
|
|
|
return
|
2021-04-18 02:27:05 +02:00
|
|
|
try:
|
|
|
|
with open(config_path, "wt") as config_file:
|
|
|
|
json.dump(DEFAULT_CONFIG, config_file, indent=2)
|
|
|
|
except OSError as exc:
|
2021-05-13 01:25:50 +02:00
|
|
|
logging.error(f"Could not create config file {config_path}: {exc}")
|