From b2925bfd2352ad43df918fd32e1caa2b1973c906 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 24 Oct 2020 17:49:19 -0700 Subject: [PATCH] cptemplate,error.py: Add handling for when target file already exists --- bin/cptemplate | 11 +++++++---- file_scripts/error.py | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bin/cptemplate b/bin/cptemplate index c1d5c74..6eeb6d7 100755 --- a/bin/cptemplate +++ b/bin/cptemplate @@ -78,9 +78,12 @@ if __name__ == "__main__": try: dest_file.touch(mode=0o600, exist_ok=False) - except FileExistsError: - # force_overwrite true: overwrite - # force_overwrite false: print error message - raise NotImplementedError("Implement if file already exists") + except FileExistsError as e: + if args.force_overwrite: + dest_file.touch(mode=0o600, exist_ok=True) + dest_file.write_bytes(selected_file.read_bytes()) + else: + print(e) + exit(error.E_FILE_EXISTS) else: dest_file.write_bytes(selected_file.read_bytes()) diff --git a/file_scripts/error.py b/file_scripts/error.py index e440349..65fea2b 100644 --- a/file_scripts/error.py +++ b/file_scripts/error.py @@ -12,6 +12,7 @@ E_NOFILESELECTED = 2 E_NOEDITORFOUND = 3 E_NO_RESULTS = 4 E_USER_RESPONSE_NO = 5 +E_FILE_EXISTS = 6 # ----- Messages ----- NO_FILE_SELECTED_MESSAGE = "No file was selected."