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