-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.sh
34 lines (29 loc) · 897 Bytes
/
include.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Used for file sourcing.
#This function should used for file sourcing instead of `. file.sh --source-only`
#
# @filepath Path to the file to be sourced
#
# Returns:
# 0 on a succesful import, 1 if the file can't be read and 2 if it can´t be found
function include()
{
local filepath="$1"
local fullpath
if [[ ! -r "$filepath" ]]; then
if [[ ! -e "$filepath" ]]; then
printf '%s\n' "File $filepath could not be found, check your file path."
return 2 # ENOENT
fi
printf '%s\n' "File $filepath could not be read, check your file permissions."
return 1 # EPERM
fi
fullpath="$(realpath "$filepath")"
if [[ -v KW_INCLUDES_SET ]]; then
[[ -v KW_INCLUDED_PATHS["$fullpath"] ]] && return 0
KW_INCLUDED_PATHS["$fullpath"]=1
else
declare -g KW_INCLUDES_SET=1
declare -gA KW_INCLUDED_PATHS=(["$fullpath"]=1)
fi
. "$filepath" --source-only
}