-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy
executable file
·119 lines (105 loc) · 2.26 KB
/
easy
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
DATE=$(which date)
GDATE=$(which gdate 2>/dev/null)
if [ -x "$GDATE" ]; then
DATE=$GDATE
fi
function LOG_INFO {
>&2 echo "$($DATE +"%F %T.%N") - [INFO ] - $*"
}
if [[ -z "$(which easy)" ]]; then
LOG_INFO "easy is not installed!"
exit 1
fi
export EASY_DIR=$(dirname $(realpath $(which easy)))
if [[ ! -d "${EASY_DIR}" ]]; then
LOG_INFO "EASY_DIR=${EASY_DIR} is not valid!"
exit 1
fi
function easy_verify_dir {
if [[ -z "$2" ]]; then
LOG_INFO "$1 is not set!"
exit 1
fi
if [[ ! -d "$2" ]]; then
LOG_INFO "$2 does not exist or is not a directory..."
exit 1
fi
}
easy_verify_dir EASY_LETSENCRYPT_DIR $EASY_LETSENCRYPT_DIR
easy_verify_dir EASY_DOMAINS_DIR $EASY_DOMAINS_DIR
D="${EASY_DIR}/commands"
function __private_easy_usage {
LOG_INFO "Invalid command: $1"
LOG_INFO "Available switches are:"
>&2 printf '\t%s\n' "--version"
LOG_INFO "Available commands are:"
cd $D
for f in *; do
>&2 printf '\t%s\n' "${f%.*}"
done
}
function __private_easy_version {
cat ${EASY_DIR}/package.json \
| grep \"version\"\: \
| cut -d ',' -f 1 \
| cut -d ':' -f 2 \
| cut -d ' ' -f 2 \
| cut -d '"' -f 2
}
function easy {
[[ "--version" == "$1" ]] && __private_easy_version && return 0
C="$1"
# no command provided
if [[ -z "$C" ]]; then
__private_easy_usage $C
return 1
fi
# Check if it is a valid command
C_OK=""
# Look for ruby implementation first
C_RB="$D/$C.rb"
if [[ -f "$C_RB" ]]; then
C_OK="$C_RB"
ruby $C_RB $@
return $?
fi
# Look for python impl
C_PY="$D/$C.py"
if [[ -f "$C_PY" ]]; then
C_OK="$C_PY"
python $C_PY $@
return $?
fi
if [[ -z "$C_OK" ]]; then
# Look for shell implementation
C_SH="$D/$C.sh"
if [[ -f "$C_SH" ]]; then
C_OK="$C_SH"
source ${C_SH}
# Check whether the command
# exists as an internal
# function...
#
# NOTE: Internal commands use
# underscores rather than
# hyphens, hence the name
# conversion as the first
# step here.
C2=$(echo "$C" | tr '-' '_')
# Execute the requested command
if [ -n "$C_OK" ]; then
# It's available as
# a shell function
__easy_command_"$C2" $@
return $?
fi
fi
fi
# couldn't find the command
if [[ -z "$C_OK" ]]; then
__private_easy_usage $C
return 1
fi
}
easy $@