captaincore/lib/arguments
2020-02-04 19:20:33 -05:00

80 lines
No EOL
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
COLOR_BLACK="\033[30m"
COLOR_RED="\033[31m"
COLOR_GREEN="\033[32m"
COLOR_YELLOW="\033[33m"
COLOR_BLUE="\033[34m"
COLOR_MAGENTA="\033[35m"
COLOR_CYAN="\033[36m"
COLOR_LIGHT_GRAY="\033[37m"
COLOR_DARK_GRAY="\033[38m"
COLOR_NORMAL="\033[39m"
# Loop through arguments and separate regular arguments from flags (--flag)
for var in "$@"; do
# If starts with "--" then assign to $flags array
if [[ $var == "--"* ]]; then
count=1+${#flags[*]}
processed_var=$var
# If contains a value then wrap in quotes
if [[ $var == *"="* ]]; then
flag_value=$( echo $var | perl -n -e '/.+?=(.+)/&& print $1' ) # extract value
flag_name=$( echo $var | perl -n -e '/(.+?)=.+/&& print $1' ) # extract name
flag_value="${flag_value%\"}" # Remove first double quote if found
flag_value="${flag_value#\"}" # Remove last double quote if found
processed_var=${flag_name}=\"${flag_value}\"
fi
flags[$count]=$processed_var
continue
fi
if [[ $var == "@"* ]]; then
# Remove @ from target
targets_current=$( echo $var | cut -c 2- )
targets[$count]="targets=$targets_current"
continue
fi
# Assign to $arguments array
count=1+${#arguments[*]}
arguments[$count]=$var
done
# Loop through flags and defines variables. Example "--email=my-email@my-site.com" becomes $email
for i in "${!flags[@]}"; do
# Replace "-" with "_" and remove leading "--"
flag_name=$( echo ${flags[$i]} | cut -c 3- )
# Defines variables for flags with data
if [[ $flag_name == *"="* ]]; then
flag_value=$( echo $flag_name | perl -n -e '/.+?=\"(.+)\"/&& print $1' ) # extract value
flag_name=$( echo $flag_name | perl -n -e '/(.+?)=.+/&& print $1' ) # extract name
flag_name=${flag_name//-/_}
flag_value="${flag_value%\"}" # Remove first double quote if found
flag_value="${flag_value#\"}" # Remove last double quote if found
declare "$flag_name"="$flag_value" # assigns to $flag_flagname
continue
fi
# Defines variables for boolean flags
flag_name=${flag_name//-/_}
declare "$flag_name"=true
done
# Load configurations from config.json
while read config; do
if [[ "$config" == "Error:"* ]]; then
continue
fi
declare "$config"
done <<< "$(php ${root_path}lib/local-scripts/configs.php fetch --captain_id=$captain_id)"
# Pass Captain ID as environment variable
export CAPTAIN_ID=$captain_id