Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --edit argument #338

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions shallow_backup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
"--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 +117,7 @@ def cli(
reinstall_fonts,
reinstall_packages,
remote,
show,
edit,
version,
):
"""
Expand All @@ -130,7 +132,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 +173,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 +299,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
45 changes: 12 additions & 33 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@
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.
:return: Path to config.
"""
test_config_path = environ.get("SHALLOW_BACKUP_TEST_CONFIG_PATH", None)
legacy_config = path.join(get_xdg_config_path(), "shallow-backup.conf")
new_config_path = path.join(get_xdg_config_path(), "shallow-backup.json")
if test_config_path:
return test_config_path
elif path.exists(legacy_config):
return legacy_config
else:
return path.join(get_xdg_config_path(), "shallow-backup.conf")
return new_config_path


def get_config() -> dict:
Expand Down Expand Up @@ -140,37 +146,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.")
Loading