Skip to content

Commit

Permalink
Improve cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
joglomedia committed Jan 21, 2025
1 parent ea6796e commit 7f0afdd
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 758 deletions.
101 changes: 50 additions & 51 deletions bin/lemper-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,57 @@
set -e -o pipefail

# Version control.
PROG_NAME=$(basename "$0")
PROG_VER="2.x.x"
export PROG_NAME && PROG_NAME=$(basename "$0")
export PROG_VERSION && PROG_VERSION="2.x.x"

# Test mode.
DRYRUN=false

# Make sure only root can run this script.
function requires_root() {
if [[ "$(id -u)" -ne 0 ]]; then
if ! hash sudo 2>/dev/null; then
echo "${PROG_NAME} command must be run as 'root' or with sudo."
exit 1
else
#echo "Switching to root user to run this script."
sudo -E "$0" "$@"
exit 0
fi
fi
}

requires_root "$@"

# Export LEMPer Stack configuration.
if [[ -f "/etc/lemper/lemper.conf" ]]; then
# Clean environemnt first.
# shellcheck source=/etc/lemper/lemper.conf
# shellcheck disable=SC2046
unset $(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs)

# shellcheck source=/etc/lemper/lemper.conf
# shellcheck disable=SC1094
# shellcheck disable=SC1091
source <(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
else
echo "LEMPer Stack configuration required, but the file doesn't exist."
echo "It should be created during installation process and placed under '/etc/lemper/lemper.conf'."
exit 1
fi

# Set default variables.
LEMPER_USERNAME=${LEMPER_USERNAME:-"lemper"}
LEMPER_PASSWORD=${LEMPER_PASSWORD:-""}
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}

# Set CLI plugins directory.
CLI_PLUGINS_DIR="/etc/lemper/cli-plugins"

# Color decorator.
RED=91
GREEN=92
YELLOW=93
RED=31
GREEN=32
YELLOW=33

##
# Helper Functions.
Expand Down Expand Up @@ -108,54 +149,13 @@ function run() {
fi
}

# Make sure only root can run this script.
function requires_root() {
if [[ "$(id -u)" -ne 0 ]]; then
if ! hash sudo 2>/dev/null; then
echo "${PROG_NAME} command must be run as 'root' or with sudo."
exit 1
else
#echo "Switching to root user to run this script."
sudo -E "$0" "$@"
exit 0
fi
fi
}

requires_root "$@"

# Export LEMPer Stack configuration.
if [[ -f "/etc/lemper/lemper.conf" ]]; then
# Clean environemnt first.
# shellcheck source=/etc/lemper/lemper.conf
# shellcheck disable=SC2046
unset $(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs)

# shellcheck source=/etc/lemper/lemper.conf
# shellcheck disable=SC1094
# shellcheck disable=SC1091
source <(grep -v '^#' /etc/lemper/lemper.conf | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
else
echo "LEMPer Stack configuration required, but the file doesn't exist."
echo "It should be created during installation process and placed under '/etc/lemper/lemper.conf'."
exit 1
fi

# Set default variables.
LEMPER_USERNAME=${LEMPER_USERNAME:-"lemper"}
LEMPER_PASSWORD=${LEMPER_PASSWORD:-""}
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}

# Set CLI plugins directory.
CLI_PLUGINS_DIR="/etc/lemper/cli-plugins"

##
# Show usage
# output to STDERR.
##
function cmd_help() {
cat <<- EOL
${PROG_NAME} ${PROG_VER}
${PROG_NAME} ${PROG_VERSION}
Command line management tool for LEMPer Stack.
Usage: ${PROG_NAME} [--version] [--help]
Expand All @@ -178,7 +178,7 @@ EOL
# Show version.
##
function cmd_version() {
echo "${PROG_NAME} version ${PROG_VER}"
echo "${PROG_NAME} version ${PROG_VERSION}"
}

##
Expand All @@ -204,10 +204,9 @@ function init_lemper_cli() {
# Source the plugin executable file.
# shellcheck disable=SC1090
. "${CLI_PLUGINS_DIR}/lemper-${CMD}" "$@"
exit 0
else
echo "${PROG_NAME}: '${CMD}' is not ${PROG_NAME} command"
echo "See '${PROG_NAME} --help' for more information"
echo "${PROG_NAME}: '${CMD}' is not valid command."
echo "See '${PROG_NAME} --help' for more information."
exit 1
fi
;;
Expand Down
10 changes: 10 additions & 0 deletions lib/lemper-account.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
# | Authors: Edi Septriyanto <[email protected]> |
# +-------------------------------------------------------------------------+

# Version control.
#CMD_PARENT="${PROG_NAME}"
#CMD_NAME="account"

# Make sure only root can access and not direct access.
if [[ "$(type -t requires_root)" != "function" ]]; then
echo "Direct access to this script is not permitted."
exit 1
fi

# Create default system account.
function create_account() {
export USERNAME=${1:-"lemper"}
Expand Down
4 changes: 1 addition & 3 deletions lib/lemper-adduser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
# +-------------------------------------------------------------------------+

# Version control.
#PROG_NAME=$(basename "$0")
#PROG_VER="2.x.x"
#CMD_PARENT="lemper-cli"
#CMD_PARENT="${PROG_NAME}"
#CMD_NAME="adduser"

# Make sure only root can access and not direct access.
Expand Down
Loading

0 comments on commit 7f0afdd

Please sign in to comment.