Skip to content

Commit

Permalink
Make backup and reinstall condition keys optional (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman authored Dec 11, 2021
1 parent 5547929 commit f2688df
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 147 deletions.
107 changes: 11 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Editing the file in a text editor will give you more control and be faster.
#### Conditional Backup and Reinstallation
Every dotfile has two subkeys: `backup_condition` and `reinstall_condition`. Both of these accept expressions that will be evaluated in `bash`. An empty string (`""`) is the default value, and is considered to be `True`. If the return value of the expression is `0`, this is considered `True`. Otherwise, it is `False`. This lets you do simple things like preventing backup with:
Every dotfile has two (optional) subkeys: `backup_condition` and `reinstall_condition`. Both of these accept expressions that will be evaluated in `bash`. An empty string (`""`) is the default value, and is considered to be `True`. If the return value of the expression is `0`, this is considered `True`. Otherwise, it is `False`. This lets you do simple things like preventing backup with:
```javascript
// Because `$ false` returns 1
Expand All @@ -167,109 +167,24 @@ And also more complicated things like only backing up certain files if an enviro
"backup_condition": "[[ -n \"$ENV_VAR\" ]]"
```
My config (as of `v5.0.0a`) looks like this, and is used to back up my [dotfiles](https://www.github.com/alichtman/dotfiles):
Here's an example config based on my [dotfiles](https://www.github.com/alichtman/dotfiles):
```json
{
"backup_path": "~/shallow-backup",
"lowest_supported_version": "5.0.0a",
"dotfiles": {
".config/agignore": {
"reinstall_condition": "",
"backup_condition": ""
"backup_condition": "uname -a | grep Darwin",
"reinstall_conditon": "uname -a | grep Darwin"
},
".config/crontab": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/fzf-notes": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/git/config": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/git/gitignore_global": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/jrnl/jrnl.yaml": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/kitty": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/nvim": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/pycodestyle": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/pylintrc": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/python": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/radare2/radare2rc": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/ranger": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/shallow-backup.conf": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/starship.toml": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/tmux": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/tuir/tuir.cfg": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/zathura/zathurarc": {
"reinstall_condition": "",
"backup_condition": ""
},
".config/zsh": {
"reinstall_condition": "",
"backup_condition": ""
},
".ctags": {
"reinstall_condition": "",
"backup_condition": ""
},
".ghc/ghci.conf": {
"reinstall_condition": "",
"backup_condition": ""
},
".pypirc": {
"reinstall_condition": "",
"backup_condition": ""
},
".ssh": {
"reinstall_condition": "",
"backup_condition": ""
},
".zshenv": {
"reinstall_condition": "",
"backup_condition": ""
}
".config/git/gitignore_global": { },
".config/jrnl/jrnl.yaml": { },
".config/kitty": { },
".config/nvim": { },
".config/pycodestyle": { },
...
".zshenv": { }
},
"root-gitignore": [
".DS_Store",
Expand Down
2 changes: 1 addition & 1 deletion shallow_backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def backup_dotfiles(backup_dest_path, dry_run=False, home_path=os.path.expanduse
dot_path_pairs = []
for dotfile_path_from_config, options in config.items():
# Evaluate condition, if specified. Skip if the command doesn't return true.
condition_success = evaluate_condition(condition=options["backup_condition"],
condition_success = evaluate_condition(condition=options.get("backup_condition", ""),
backup_or_reinstall="backup",
dotfile_path=dotfile_path_from_config)
if not condition_success:
Expand Down
63 changes: 15 additions & 48 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,16 @@ def get_default_config() -> dict:
"backup_condition": "",
"reinstall_condition": "",
},
".bashrc": {
"backup_condition": "",
"reinstall_condition": "",
},
".config/git": {
"backup_condition": "",
"reinstall_condition": "",
},
".config/nvim/init.vim": {
"backup_condition": "",
"reinstall_condition": "",
},
".config/tmux": {
"backup_condition": "",
"reinstall_condition": "",
},
".config/zsh": {
"backup_condition": "",
"reinstall_condition": "",
},
".profile": {
"backup_condition": "",
"reinstall_condition": "",
},
".pypirc": {
"backup_condition": "",
"reinstall_condition": "",
},
".ssh": {
"backup_condition": "",
"reinstall_condition": "",
},
".zshenv": {
"backup_condition": "",
"reinstall_condition": "",
},
f"{strip_home(get_config_path())}": {
"backup_condition": "",
"reinstall_condition": "",
},
".bashrc": {},
".config/git": {},
".config/nvim/init.vim": {},
".config/tmux": {},
".config/zsh": {},
".profile": {},
".pypirc": {},
".ssh": {},
".zshenv": {},
f"{strip_home(get_config_path())}": {},
},
"root-gitignore": [
"dotfiles/.ssh",
Expand Down Expand Up @@ -151,10 +121,7 @@ def add_dot_path_to_config(backup_config: dict, file_path: str) -> dict:
else:
stripped_home_path = strip_home(abs_path)
print_path_blue("Added:", stripped_home_path)
backup_config["dotfiles"][stripped_home_path] = {
"reinstall_condition": "",
"backup_condition": ""
}
backup_config["dotfiles"][stripped_home_path] = {}
return backup_config


Expand All @@ -169,15 +136,15 @@ def show_config():
print_path_red("Backup Path:", contents)
elif section == "config_mapping":
print_red_bold("\nConfigs:")
for path, dest in contents.items():
print(f" {path} -> {dest}")
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['backup_condition']
reinstall_condition = options['reinstall_condition']
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}\"")
Expand Down
2 changes: 1 addition & 1 deletion shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def reinstall_dots_sb(dots_path: str, home_path: str = os.path.expanduser("~"),
dotfiles_to_reinstall = []
for dotfile_path_from_config, options in config.items():
# Evaluate condition, if specified. Skip if the command doesn't return true.
condition_success = evaluate_condition(condition=options["reinstall_condition"],
condition_success = evaluate_condition(condition=options.get("reinstall_condition", ""),
backup_or_reinstall="reinstall",
dotfile_path=dotfile_path_from_config)
if not condition_success:
Expand Down
1 change: 0 additions & 1 deletion shallow_backup/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ def check_if_config_upgrade_needed():
print_red("2. Manually upgrade the config.")
print_red_bold("Please downgrade to a version of shallow-backup before v5.0.0a if you do not want to upgrade your config.")
sys.exit()

0 comments on commit f2688df

Please sign in to comment.