Skip to content

Commit 7f0afdd

Browse files
committed
Improve cli command
1 parent ea6796e commit 7f0afdd

File tree

9 files changed

+639
-758
lines changed

9 files changed

+639
-758
lines changed

bin/lemper-cli.sh

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,57 @@
2020
set -e -o pipefail
2121

2222
# Version control.
23-
PROG_NAME=$(basename "$0")
24-
PROG_VER="2.x.x"
23+
export PROG_NAME && PROG_NAME=$(basename "$0")
24+
export PROG_VERSION && PROG_VERSION="2.x.x"
2525

2626
# Test mode.
2727
DRYRUN=false
2828

29+
# Make sure only root can run this script.
30+
function requires_root() {
31+
if [[ "$(id -u)" -ne 0 ]]; then
32+
if ! hash sudo 2>/dev/null; then
33+
echo "${PROG_NAME} command must be run as 'root' or with sudo."
34+
exit 1
35+
else
36+
#echo "Switching to root user to run this script."
37+
sudo -E "$0" "$@"
38+
exit 0
39+
fi
40+
fi
41+
}
42+
43+
requires_root "$@"
44+
45+
# Export LEMPer Stack configuration.
46+
if [[ -f "/etc/lemper/lemper.conf" ]]; then
47+
# Clean environemnt first.
48+
# shellcheck source=/etc/lemper/lemper.conf
49+
# shellcheck disable=SC2046
50+
unset $(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs)
51+
52+
# shellcheck source=/etc/lemper/lemper.conf
53+
# shellcheck disable=SC1094
54+
# shellcheck disable=SC1091
55+
source <(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
56+
else
57+
echo "LEMPer Stack configuration required, but the file doesn't exist."
58+
echo "It should be created during installation process and placed under '/etc/lemper/lemper.conf'."
59+
exit 1
60+
fi
61+
62+
# Set default variables.
63+
LEMPER_USERNAME=${LEMPER_USERNAME:-"lemper"}
64+
LEMPER_PASSWORD=${LEMPER_PASSWORD:-""}
65+
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}
66+
67+
# Set CLI plugins directory.
68+
CLI_PLUGINS_DIR="/etc/lemper/cli-plugins"
69+
2970
# Color decorator.
30-
RED=91
31-
GREEN=92
32-
YELLOW=93
71+
RED=31
72+
GREEN=32
73+
YELLOW=33
3374

3475
##
3576
# Helper Functions.
@@ -108,54 +149,13 @@ function run() {
108149
fi
109150
}
110151

111-
# Make sure only root can run this script.
112-
function requires_root() {
113-
if [[ "$(id -u)" -ne 0 ]]; then
114-
if ! hash sudo 2>/dev/null; then
115-
echo "${PROG_NAME} command must be run as 'root' or with sudo."
116-
exit 1
117-
else
118-
#echo "Switching to root user to run this script."
119-
sudo -E "$0" "$@"
120-
exit 0
121-
fi
122-
fi
123-
}
124-
125-
requires_root "$@"
126-
127-
# Export LEMPer Stack configuration.
128-
if [[ -f "/etc/lemper/lemper.conf" ]]; then
129-
# Clean environemnt first.
130-
# shellcheck source=/etc/lemper/lemper.conf
131-
# shellcheck disable=SC2046
132-
unset $(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs)
133-
134-
# shellcheck source=/etc/lemper/lemper.conf
135-
# shellcheck disable=SC1094
136-
# shellcheck disable=SC1091
137-
source <(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
138-
else
139-
echo "LEMPer Stack configuration required, but the file doesn't exist."
140-
echo "It should be created during installation process and placed under '/etc/lemper/lemper.conf'."
141-
exit 1
142-
fi
143-
144-
# Set default variables.
145-
LEMPER_USERNAME=${LEMPER_USERNAME:-"lemper"}
146-
LEMPER_PASSWORD=${LEMPER_PASSWORD:-""}
147-
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}
148-
149-
# Set CLI plugins directory.
150-
CLI_PLUGINS_DIR="/etc/lemper/cli-plugins"
151-
152152
##
153153
# Show usage
154154
# output to STDERR.
155155
##
156156
function cmd_help() {
157157
cat <<- EOL
158-
${PROG_NAME} ${PROG_VER}
158+
${PROG_NAME} ${PROG_VERSION}
159159
Command line management tool for LEMPer Stack.
160160
161161
Usage: ${PROG_NAME} [--version] [--help]
@@ -178,7 +178,7 @@ EOL
178178
# Show version.
179179
##
180180
function cmd_version() {
181-
echo "${PROG_NAME} version ${PROG_VER}"
181+
echo "${PROG_NAME} version ${PROG_VERSION}"
182182
}
183183

184184
##
@@ -204,10 +204,9 @@ function init_lemper_cli() {
204204
# Source the plugin executable file.
205205
# shellcheck disable=SC1090
206206
. "${CLI_PLUGINS_DIR}/lemper-${CMD}" "$@"
207-
exit 0
208207
else
209-
echo "${PROG_NAME}: '${CMD}' is not ${PROG_NAME} command"
210-
echo "See '${PROG_NAME} --help' for more information"
208+
echo "${PROG_NAME}: '${CMD}' is not valid command."
209+
echo "See '${PROG_NAME} --help' for more information."
211210
exit 1
212211
fi
213212
;;

lib/lemper-account.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
# | Authors: Edi Septriyanto <[email protected]> |
1616
# +-------------------------------------------------------------------------+
1717

18+
# Version control.
19+
#CMD_PARENT="${PROG_NAME}"
20+
#CMD_NAME="account"
21+
22+
# Make sure only root can access and not direct access.
23+
if [[ "$(type -t requires_root)" != "function" ]]; then
24+
echo "Direct access to this script is not permitted."
25+
exit 1
26+
fi
27+
1828
# Create default system account.
1929
function create_account() {
2030
export USERNAME=${1:-"lemper"}

lib/lemper-adduser.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
# +-------------------------------------------------------------------------+
1717

1818
# Version control.
19-
#PROG_NAME=$(basename "$0")
20-
#PROG_VER="2.x.x"
21-
#CMD_PARENT="lemper-cli"
19+
#CMD_PARENT="${PROG_NAME}"
2220
#CMD_NAME="adduser"
2321

2422
# Make sure only root can access and not direct access.

0 commit comments

Comments
 (0)