From c16ef9f747d09f777dad390697991edf62fcfae5 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 24 Oct 2020 17:28:13 -0700 Subject: [PATCH] Check if template directory exists before continuing with script --- bin/cptemplate | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/bin/cptemplate b/bin/cptemplate index 0d9ceb0..c1d5c74 100755 --- a/bin/cptemplate +++ b/bin/cptemplate @@ -26,6 +26,12 @@ from sys import platform # ----- Paths ----- DEFAULT_TEMPLATE_DIR = Path.home() / "Templates" +# ----- Error Messages ----- +TEMPLATE_DIR_DOESNT_EXIST = "Warning: template dir does not exist, exiting." + +# ----- Exit Codes ----- +E_TEMP_DIR_NON_EXIST = 1 + # ========== Functions========== # ========== Main Script ========== if __name__ == "__main__": @@ -38,7 +44,6 @@ if __name__ == "__main__": "--template-dir", dest="dir", type=str, - default=DEFAULT_TEMPLATerror.E_DIR, help="choose a template directory (default: ~/Templates)", ) parser.add_argument( @@ -52,20 +57,30 @@ if __name__ == "__main__": args = parser.parse_args() - files = search.find_files(directory=args.dir) + template_dir = DEFAULT_TEMPLATE_DIR if args.dir is None else Path(args.dir) + + # Check if default template directory is non-existent + if not template_dir.exists(): + print(TEMPLATE_DIR_DOESNT_EXIST) + exit(E_TEMP_DIR_NON_EXIST) + + files = search.find_files(directory=template_dir) try: selected_file = fzf.select_file_with_fzf(files) except KeyboardInterrupt: exit(error.E_INTERRUPT) + except fzf.FZFError as f: + print(f) + exit(error.E_INTERRUPT) dest_file = Path(args.dest) 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") else: dest_file.write_bytes(selected_file.read_bytes()) - # force_overwrite true: overwrite - # force_overwrite false: print error message