Detailed backup message with custom exit codes

This commit is contained in:
Ferit Yiğit BALABAN 2022-03-22 23:52:52 +03:00
parent e4c96bab71
commit eec0bfc3de
No known key found for this signature in database
GPG Key ID: BB8494BA65A90842

View File

@ -12,7 +12,7 @@ from termcolor import colored, cprint
DOTFILES_REPOSITORY = '$HOME/repos/dotfiles' DOTFILES_REPOSITORY = '$HOME/repos/dotfiles'
REMOTE_REPOSITORY = 'https://github.com/fybalaban/dotfiles' REMOTE_REPOSITORY = 'https://github.com/fybalaban/dotfiles'
LOCAL_CONFIG = '$HOME/.config' LOCAL_CONFIG = '$HOME/.config'
VER = 'v1.4' VER = 'v1.5'
help_message = f''' help_message = f'''
dotman {VER} dotfiles manager by fyb dotman {VER} dotfiles manager by fyb
@ -45,7 +45,7 @@ def proc(command, cwd=''):
r = run(command, shell=True, capture_output=True, text=True) r = run(command, shell=True, capture_output=True, text=True)
else: else:
r = run(command, shell=True, capture_output=True, text=True, cwd=cwd) r = run(command, shell=True, capture_output=True, text=True, cwd=cwd)
return r.returncode, str(r.stdout) return r.returncode, str(r.stdout) + str(r.stderr)
def prclr(text, color): def prclr(text, color):
@ -85,20 +85,14 @@ def special_copy(source, dest):
copy(directory, dest) copy(directory, dest)
def git_commit(): def commit_then_push():
flag_commit = True _, _ = proc('/usr/bin/git add .', DOTFILES_REPOSITORY)
code, output = proc('/usr/bin/git add .', DOTFILES_REPOSITORY)
flag_commit = flag_commit or 'nothing to commit' in output
if flag_commit:
date = dt.now().strftime('%d.%m.%Y %H.%M') date = dt.now().strftime('%d.%m.%Y %H.%M')
proc(f'/usr/bin/git commit -m "dotman {date}"', DOTFILES_REPOSITORY) code, output = proc(f'/usr/bin/git commit -m "dotman {date}"', DOTFILES_REPOSITORY)
return flag_commit if 'nothing to commit' not in output:
def push_remote():
code, output = proc('/usr/bin/git push', DOTFILES_REPOSITORY) code, output = proc('/usr/bin/git push', DOTFILES_REPOSITORY)
return code == 0, code return 0 if code == 0 else 2
return 1
def main(): def main():
@ -156,10 +150,14 @@ def main():
special_copy(LOCAL_CONFIG, DOTFILES_REPOSITORY) special_copy(LOCAL_CONFIG, DOTFILES_REPOSITORY)
print(colored('Step 2: ', 'magenta'), f'Create a commit and push to remote repo', print(colored('Step 2: ', 'magenta'), f'Create a commit and push to remote repo',
colored(f'"{REMOTE_REPOSITORY}"', 'yellow')) colored(f'"{REMOTE_REPOSITORY}"', 'yellow'))
git_commit() stat = commit_then_push()
push_remote() if stat == 0:
cprint('Backup completed. Have a nice day!', 'green') cprint('Backup completed. Have a nice day!', 'green')
exit(0) elif stat == 1:
cprint('There was nothing to commit, aborting.', 'red')
elif stat == 2:
cprint('Couldn\'t push local to remote, aborting.', 'red')
exit(stat)
elif ans.lower() == 'd' or ans.lower() == 'deploy': elif ans.lower() == 'd' or ans.lower() == 'deploy':
print(colored('Step 1:', 'magenta'), ' Copy from local repo to local config') print(colored('Step 1:', 'magenta'), ' Copy from local repo to local config')
special_copy(DOTFILES_REPOSITORY, LOCAL_CONFIG) special_copy(DOTFILES_REPOSITORY, LOCAL_CONFIG)
@ -168,9 +166,7 @@ def main():
elif flag_backup and not flag_deploy: elif flag_backup and not flag_deploy:
if local_repo_exists: if local_repo_exists:
special_copy(LOCAL_CONFIG, DOTFILES_REPOSITORY) special_copy(LOCAL_CONFIG, DOTFILES_REPOSITORY)
git_commit() exit(commit_then_push())
push_remote()
exit(0)
else: else:
print(colored('[CRITICAL]', 'red'), 'Local repository couldn\'t be located. Aborting...') print(colored('[CRITICAL]', 'red'), 'Local repository couldn\'t be located. Aborting...')
exit(128) exit(128)