mirror of
https://github.com/eth-p/bat-extras.git
synced 2024-12-04 21:40:44 +01:00
prettybat: Add checks to ensure that files exist
This commit is contained in:
parent
165fc69d34
commit
b275282e36
27
lib/check.sh
Normal file
27
lib/check.sh
Normal 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
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user