mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-06-29 22:16:13 +02:00
Add --config-file argument to use alternate config file at runtime (#1290)
* added new CLI argument option --config-file * pass argument and fetch alt config file if specified * argparse argument setting update * argument alias --cf added * documentation update - usage of CLI argument * fixed name-clash + unit tests * feature test added * #1170-alternate-config-file: Auto stash before rebase of "refs/heads/#1170-alternate-config-file" * Update docs/advanced.md Co-authored-by: Jonathan Wren <jonathan@nowandwren.com> * BDD tests added * Begin migrating/rewording --cf tests in pytest-bdd. Uses current directory instead of deep directory structure, but requires a given for each config file referenced * Fix issue where specifying a config-file that needs to be upgraded ended up upgrading the user config file instead * Uncomment and rework remaining tests for pytest-bdd instead of behave * Fix copytree for Python 3.7 (which doesn't support dirs_exist_ok) * Minor fixes to alternative config examples * Remove behave tests (behave is no longer in use) * Move config file unit test to unit test dir and use pytext path fixture instead of current directory to find test data * Use explicit "given the config exists" for copying config files instead of shoehorning in "given we use the config" twice * Change when/when to when/and * Clarify scenarios and fix indentation * Confirm primary config file isn't modified when encrypting/decrypting a journal in an alternate config file * Remove try/except on copytree since I'm no longer using the same Co-authored-by: Jonathan Wren <jonathan@nowandwren.com> Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
parent
5057c290c1
commit
ae009099ed
10 changed files with 209 additions and 15 deletions
25
jrnl/args.py
25
jrnl/args.py
|
@ -337,6 +337,31 @@ def parse_args(args=[]):
|
|||
""",
|
||||
)
|
||||
|
||||
alternate_config = parser.add_argument_group(
|
||||
"Specifies alternate config to be used",
|
||||
textwrap.dedent("Applies alternate config for current session"),
|
||||
)
|
||||
|
||||
alternate_config.add_argument(
|
||||
"--config-file",
|
||||
dest="config_file_path",
|
||||
type=str,
|
||||
default="",
|
||||
help="""
|
||||
Overrides default (created when first installed) config file for this command only.
|
||||
|
||||
Examples: \n
|
||||
\t - Use a work config file for this jrnl entry, call: \n
|
||||
\t jrnl --config-file /home/user1/work_config.yaml
|
||||
\t - Use a personal config file stored on a thumb drive: \n
|
||||
\t jrnl --config-file /media/user1/my-thumb-drive/personal_config.yaml
|
||||
""",
|
||||
)
|
||||
|
||||
alternate_config.add_argument(
|
||||
"--cf", dest="config_file_path", type=str, default="", help=argparse.SUPPRESS
|
||||
)
|
||||
|
||||
# Handle '-123' as a shortcut for '-n 123'
|
||||
num = re.compile(r"^-(\d+)$")
|
||||
args = [num.sub(r"-n \1", arg) for arg in args]
|
||||
|
|
|
@ -47,9 +47,14 @@ def make_yaml_valid_dict(input: list) -> dict:
|
|||
return runtime_modifications
|
||||
|
||||
|
||||
def save_config(config):
|
||||
def save_config(config, alt_config_path=None):
|
||||
"""Supply alt_config_path if using an alternate config through --config-file."""
|
||||
config["version"] = __version__
|
||||
with open(get_config_path(), "w", encoding=YAML_FILE_ENCODING) as f:
|
||||
with open(
|
||||
alt_config_path if alt_config_path else get_config_path(),
|
||||
"w",
|
||||
encoding=YAML_FILE_ENCODING,
|
||||
) as f:
|
||||
yaml.safe_dump(
|
||||
config,
|
||||
f,
|
||||
|
|
|
@ -19,32 +19,53 @@ from .prompt import yesno
|
|||
from .upgrade import is_old_version
|
||||
|
||||
|
||||
def upgrade_config(config):
|
||||
def upgrade_config(config_data, alt_config_path=None):
|
||||
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
|
||||
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
||||
versions."""
|
||||
versions.
|
||||
Supply alt_config_path if using an alternate config through --config-file."""
|
||||
default_config = get_default_config()
|
||||
missing_keys = set(default_config).difference(config)
|
||||
missing_keys = set(default_config).difference(config_data)
|
||||
if missing_keys:
|
||||
for key in missing_keys:
|
||||
config[key] = default_config[key]
|
||||
save_config(config)
|
||||
config_data[key] = default_config[key]
|
||||
save_config(config_data, alt_config_path)
|
||||
config_path = alt_config_path if alt_config_path else get_config_path()
|
||||
print(
|
||||
f"[Configuration updated to newest version at {get_config_path()}]",
|
||||
f"[Configuration updated to newest version at {config_path}]",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
|
||||
def load_or_install_jrnl():
|
||||
"""
|
||||
If jrnl is already installed, loads and returns a config object.
|
||||
Else, perform various prompts to install jrnl.
|
||||
"""
|
||||
def find_default_config():
|
||||
config_path = (
|
||||
get_config_path()
|
||||
if os.path.exists(get_config_path())
|
||||
else os.path.join(os.path.expanduser("~"), ".jrnl_config")
|
||||
)
|
||||
return config_path
|
||||
|
||||
|
||||
def find_alt_config(alt_config):
|
||||
if os.path.exists(alt_config):
|
||||
return alt_config
|
||||
else:
|
||||
print(
|
||||
"Alternate configuration file not found at path specified.", file=sys.stderr
|
||||
)
|
||||
print("Exiting.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def load_or_install_jrnl(alt_config_path):
|
||||
"""
|
||||
If jrnl is already installed, loads and returns a default config object.
|
||||
If alternate config is specified via --config-file flag, it will be used.
|
||||
Else, perform various prompts to install jrnl.
|
||||
"""
|
||||
config_path = (
|
||||
find_alt_config(alt_config_path) if alt_config_path else find_default_config()
|
||||
)
|
||||
|
||||
if os.path.exists(config_path):
|
||||
logging.debug("Reading configuration from file %s", config_path)
|
||||
|
@ -68,7 +89,7 @@ def load_or_install_jrnl():
|
|||
print("Exiting.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
upgrade_config(config)
|
||||
upgrade_config(config, alt_config_path)
|
||||
verify_config_colors(config)
|
||||
|
||||
else:
|
||||
|
|
|
@ -36,7 +36,7 @@ def run(args):
|
|||
|
||||
# Load the config, and extract journal name
|
||||
try:
|
||||
config = install.load_or_install_jrnl()
|
||||
config = install.load_or_install_jrnl(args.config_file_path)
|
||||
original_config = config.copy()
|
||||
|
||||
# Apply config overrides
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue