Skip to content

Commit 8969d60

Browse files
authored
Add CLI option for adding paths to config (#248)
Close #247
1 parent 573d632 commit 8969d60

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

shallow_backup/__main__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# custom help options
1414
@click.command(context_settings=dict(help_option_names=['-h', '-help', '--help']))
15+
@click.option('--add', default=None, help="Add a dotfile or dotfolder by path.")
1516
@click.option('-all', is_flag=True, default=False, help="Full back up.")
1617
@click.option('-configs', is_flag=True, default=False, help="Back up app config files.")
1718
@click.option('-delete_config', is_flag=True, default=False, help="Delete config file.")
@@ -31,7 +32,7 @@
3132
@click.option('-separate_dotfiles_repo', is_flag=True, default=False, help="Use if you are trying to maintain a separate dotfiles repo and running into issue #229.")
3233
@click.option('-show', is_flag=True, default=False, help="Display config file.")
3334
@click.option('--version', '-v', is_flag=True, default=False, help='Display version and author info.')
34-
def cli(all, configs, delete_config, destroy_backup, dotfiles, fonts, new_path,
35+
def cli(add, all, configs, delete_config, destroy_backup, dotfiles, fonts, new_path,
3536
no_splash, old_path, packages, reinstall_all, reinstall_configs,
3637
reinstall_dots, reinstall_fonts, reinstall_packages, remote,
3738
separate_dotfiles_repo, show, version):
@@ -45,13 +46,16 @@ def cli(all, configs, delete_config, destroy_backup, dotfiles, fonts, new_path,
4546
upgrade_from_pre_v3()
4647

4748
# Process CLI args
48-
admin_action = any([version, delete_config, destroy_backup, show])
49+
admin_action = any([add, delete_config, destroy_backup, show, version])
4950
has_cli_arg = any([old_path, all, dotfiles, packages, fonts, configs,
5051
reinstall_dots, reinstall_fonts, reinstall_all,
5152
reinstall_configs, reinstall_packages])
5253
skip_prompt = any([all, dotfiles, configs, packages, fonts, reinstall_packages, reinstall_configs, reinstall_dots,
5354
reinstall_fonts])
5455

56+
safe_create_config()
57+
backup_config = get_config()
58+
5559
# Perform administrative action and exit.
5660
if admin_action:
5761
if version:
@@ -63,13 +67,14 @@ def cli(all, configs, delete_config, destroy_backup, dotfiles, fonts, new_path,
6367
destroy_backup_dir(backup_home_path)
6468
elif show:
6569
show_config()
70+
elif add:
71+
new_config = add_path(backup_config, add)
72+
write_config(new_config)
6673
sys.exit()
6774

6875
# Start CLI
6976
if not no_splash:
7077
splash_screen()
71-
safe_create_config()
72-
backup_config = get_config()
7378

7479
# User entered a new path, so update the config
7580
if new_path:

shallow_backup/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ def delete_config_file():
111111
print_red_bold("ERROR: No config file found.")
112112

113113

114+
def add_path(backup_config: dict, filePath: str) -> dict:
115+
"""
116+
Add a path to the config under the correct heading (dotfiles / dotfolders).
117+
Exits if the filepath parameter is invalid.
118+
:backup_config: dict representing current config
119+
:add: str relative or absolute path of file to add to config
120+
:return new backup config
121+
"""
122+
def strip_home(full_path):
123+
"""
124+
Removes the path to $HOME from the front of the absolute path.
125+
"""
126+
return full_path[len(os.path.expanduser("~")) + 1:]
127+
128+
absPath = path.abspath(filePath)
129+
if not path.exists(absPath):
130+
print_path_red("Invalid file path:", absPath)
131+
sys.exit(1)
132+
elif path.isdir(absPath):
133+
backup_config["dotfolders"] += [strip_home(absPath)]
134+
else: # Otherwise it's a dotfile
135+
backup_config["dotfiles"] += [strip_home(absPath)]
136+
return backup_config
137+
138+
114139
def show_config():
115140
"""
116141
Print the config. Colorize section titles and indent contents.

0 commit comments

Comments
 (0)