From b275282e36272708b46aaec98d74c465092bc293 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 16 Dec 2020 17:15:35 -0800 Subject: [PATCH] prettybat: Add checks to ensure that files exist --- lib/check.sh | 27 +++++++++++++++++++++++++++ src/prettybat.sh | 5 +++++ 2 files changed, 32 insertions(+) create mode 100644 lib/check.sh diff --git a/lib/check.sh b/lib/check.sh new file mode 100644 index 0000000..a6edd69 --- /dev/null +++ b/lib/check.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# ----------------------------------------------------------------------------- +# bat-extras | Copyright (C) 2020 eth-p | MIT License +# +# Repository: https://github.com/eth-p/bat-extras +# Issues: https://github.com/eth-p/bat-extras/issues +# ----------------------------------------------------------------------------- + +# Checks that the file or directory exists. +# Arguments: +# 1 -- The path to check. +check_exists() { + [[ -e "$1" ]] && return 0 + + print_error "%s: No such file or directory" "$1" + return 1 +} + +# Checks that the file is a file. +# Arguments: +# 1 -- The path to check. +check_is_file() { + [[ -f "$1" ]] && return 0 + + print_error "%s: Not a file" "$1" + return 1 +} diff --git a/src/prettybat.sh b/src/prettybat.sh index a03281d..49d44cd 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -13,6 +13,7 @@ source "${LIB}/opt_hook_version.sh" source "${LIB}/str.sh" source "${LIB}/print.sh" source "${LIB}/version.sh" +source "${LIB}/check.sh" # ----------------------------------------------------------------------------- # Init: # ----------------------------------------------------------------------------- @@ -178,6 +179,10 @@ process_file() { local fext="$ext" local lang="${ext:1}" local formatter + + # Check that the file exists, and is a file. + check_exists "$file" || return 1 + check_is_file "$file" || return 1 # Determine the formatter. if [[ -n "$OPT_LANGUAGE" ]]; then