-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.lib.sh
66 lines (55 loc) · 1.54 KB
/
env.lib.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# wrapper of md5
function env._md5 {
if which md5 >/dev/zero; then
md5 "$@" | awk '{print $NF}'
elif which md5sum >/dev/zero; then
md5sum "$@" | awk '{print $1}'
fi
}
# wrapper of xargs | md5
function env._xargs-md5 {
if which md5 >/dev/zero; then
xargs md5
elif which md5sum >/dev/zero; then
xargs md5sum
fi
}
# generate environment for each files and folder based on ther name and contents
function env.fenv {
DIR=${1:-"${DIR:-"."}"}
for file in $(find $DIR -type f); do
name=$(echo -n "$file" | env._md5)
content=$(env._md5 "$file")
export "__FILE_$name"="$content"
done
for folder in $(find $DIR -type d); do
name=$(echo -n "$folder" | env._md5)
content="$(find "$folder" -type f | env._xargs-md5 | env._md5)"
export "__FOLDER_$name"="$content"
done
}
# list name of the environment of each file/folder that will be generated by `env.fenv`
function env.fenvname {
DIR=${1:-"${DIR:-"."}"}
for file in $(find $DIR -type f); do
echo "$file => __FILE_"$(echo -n "$file" | env._md5)
done
for folder in $(find $DIR -type d); do
echo "$folder => __FOLDER_"$(echo -n "$folder" | env._md5)
done
}
# load environment variables from files
function env.dotenv {
if [ $# -eq 0 ]; then
set -- "${@:1:1}" ".env"
fi
for file in "$@"; do
if [ ! -f "$file" ]; then
continue
fi
set -o allexport
source "$file"
set +o allexport
done
}