prettybat: Add checks to ensure that files exist

This commit is contained in:
Ethan P 2020-12-16 17:15:35 -08:00
parent 165fc69d34
commit b275282e36
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 32 additions and 0 deletions

27
lib/check.sh Normal file
View File

@ -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
}

View File

@ -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