Skip to content

Commit

Permalink
Add --edit argument
Browse files Browse the repository at this point in the history
Fix #336
  • Loading branch information
alichtman committed Nov 16, 2023
1 parent 206543c commit 711abc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
14 changes: 7 additions & 7 deletions shallow_backup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -115,7 +115,7 @@ def cli(
reinstall_fonts,
reinstall_packages,
remote,
show,
edit,
version,
):
"""
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"):
Expand Down
39 changes: 7 additions & 32 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
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:
"""Returns path to $XDG_CONFIG_HOME, or ~/.config, if it doesn't exist."""
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.
Expand Down Expand Up @@ -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}")
11 changes: 6 additions & 5 deletions shallow_backup/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def main_menu_prompt():
" Reinstall packages",
" Add path to config",
" Remove path from config",
" Show config",
" Edit config",
" Destroy backup",
],
),
Expand All @@ -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.")

0 comments on commit 711abc6

Please sign in to comment.