diff --git a/shallow_backup/__main__.py b/shallow_backup/__main__.py index ba7e1c0b..779d5b74 100644 --- a/shallow_backup/__main__.py +++ b/shallow_backup/__main__.py @@ -88,7 +88,7 @@ "--reinstall-packages", is_flag=True, default=False, help="Reinstall packages." ) @click.option("--remote", default=None, help="Set remote URL for the git repo.") -@click.option("--show", is_flag=True, default=False, help="Display config file.") +@click.option("--edit", is_flag=True, default=False, help="Open config file in $EDITOR.") @click.option( "--version", "-v", @@ -115,7 +115,7 @@ def cli( reinstall_fonts, reinstall_packages, remote, - show, + edit, version, ): """ @@ -130,7 +130,7 @@ def cli( check_insecure_config_permissions() # Process CLI args - admin_action = any([add_dot, delete_config, destroy_backup, show, version]) + admin_action = any([add_dot, delete_config, destroy_backup, edit, version]) has_cli_arg = any( [ no_new_backup_path_prompt, @@ -171,8 +171,8 @@ def cli( elif destroy_backup: backup_home_path = expand_to_abs_path(get_config()["backup_path"]) destroy_backup_dir(backup_home_path) - elif show: - show_config() + elif edit: + edit_config() elif add_dot: new_config = add_dot_path_to_config(backup_config, add_dot) write_config(new_config) @@ -297,8 +297,8 @@ def cli( elif target == "all": reinstall_all_sb(dotfiles_path, packages_path, fonts_path, configs_path) elif target == "config": - if action.startswith("show"): - show_config() + if action.startswith("edit"): + edit_config() elif action.startswith("add"): add_to_config_prompt() elif action.startswith("remove"): diff --git a/shallow_backup/config.py b/shallow_backup/config.py index 0838b397..7240f642 100644 --- a/shallow_backup/config.py +++ b/shallow_backup/config.py @@ -7,6 +7,7 @@ from .compatibility import * from .utils import safe_mkdir, strip_home from .constants import ProjInfo +from functools import lru_cache def get_xdg_config_path() -> str: @@ -14,6 +15,7 @@ def get_xdg_config_path() -> str: return environ.get("XDG_CONFIG_HOME") or path.join(path.expanduser("~"), ".config") +@lru_cache(maxsize=1) def get_config_path() -> str: """ Detects if in testing or prod env, and returns the right config path. @@ -140,37 +142,10 @@ def add_dot_path_to_config(backup_config: dict, file_path: str) -> dict: return backup_config -def show_config(): +def edit_config(): """ - Print the config. Colorize section titles and indent contents. + Open the config in the default editor """ - print_section_header("SHALLOW BACKUP CONFIG", Fore.RED) - for section, contents in get_config().items(): - # Print backup path on same line - if section == "backup_path": - print_path_red("Backup Path:", contents) - elif section == "config_mapping": - print_red_bold("\nConfigs:") - for config_path, dest in contents.items(): - print(f" {config_path} -> {dest}") - # Print section header and contents. (Dotfiles) - elif section == "dotfiles": - print_path_red( - "\nDotfiles:", - "(Backup and Reinstall conditions will be shown if they exist)", - ) - for dotfile, options in contents.items(): - backup_condition = options.get("backup_condition", "") - reinstall_condition = options.get("reinstall_condition", "") - if backup_condition or reinstall_condition: - print(f" {dotfile} ->") - print(f'\t\tbackup_condition: "{backup_condition}"') - print(f'\t\treinstall_condition: "{reinstall_condition}"') - else: - print(f" {dotfile}") - elif section == "lowest_supported_version": - print_path_red(f"{section.replace('_', ' ').capitalize()}:", contents) - else: - print_red_bold(f"\n{section.replace('-', ' ').capitalize()}: ") - for item in contents: - print(f" {item}") + config_path = get_config_path() + editor = os.environ.get("EDITOR", "vim") + os.system(f"{editor} {config_path}") diff --git a/shallow_backup/prompts.py b/shallow_backup/prompts.py index c00c8198..7c8a0560 100644 --- a/shallow_backup/prompts.py +++ b/shallow_backup/prompts.py @@ -184,7 +184,7 @@ def main_menu_prompt(): " Reinstall packages", " Add path to config", " Remove path from config", - " Show config", + " Edit config", " Destroy backup", ], ), @@ -193,7 +193,8 @@ def main_menu_prompt(): answers = inquirer.prompt(questions) if answers: - return answers.get("choice").strip().lower() - else: - # KeyboardInterrupts - sys.exit(1) + choice = answers.get("choice") + if choice: + return choice.strip().lower() + + raise Exception("ERR: Invalid choice.")