Skip to content

Commit

Permalink
Feature - Improved App Selection (#31)
Browse files Browse the repository at this point in the history
* Improved app selection to now show bot app and service type along with selection before installation

* Optimised import and export function and fixed bug issues when operating these functions

* Optimised data display and fixed issue when switching context

* Optimised and feature improved install application display menu

* Updated CLI options and added service option to app install choice

* Updated default logic option to include newly added field

* Fixed return key stuck in loop when nothing to install
  • Loading branch information
XternA authored Sep 16, 2024
1 parent c4d1350 commit ec06c20
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 290 deletions.
6 changes: 4 additions & 2 deletions apps.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"HONEYGAIN_EMAIL",
"HONEYGAIN_PASSWORD"
],
"is_enabled": true
"is_enabled": true,
"service_enabled": true
},
{
"name": "PEER2PROFIT",
Expand Down Expand Up @@ -168,6 +169,7 @@
"BEARSHARE_EMAIL",
"BEARSHARE_PASSWORD"
],
"is_enabled": true
"is_enabled": true,
"service_enabled": true
}
]
32 changes: 32 additions & 0 deletions compose/compose.service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
honeygain-pot:
container_name: honeygain-pot
image: xterna/honeygain-pot
restart: always
environment:
- EMAIL=${HONEYGAIN_EMAIL:-}
- PASSWORD=${HONEYGAIN_PASSWORD:-}
profiles:
- ${HONEYGAIN_SERVICE:-DISABLED}
dns:
- 1.1.1.1
- 8.8.8.8
cpus: $ALT_MIN_CPU_LIMIT
mem_limit: $RAM_LIMIT
mem_reservation: $RAM_RESERVE

bearshare-pot:
container_name: bearshare-pot
image: xterna/bearshare-pot
restart: always
environment:
- EMAIL=${BEARSHARE_EMAIL:-}
- PASSWORD=${BEARSHARE_PASSWORD:-}
profiles:
- ${BEARSHARE_SERVICE:-DISABLED}
dns:
- 1.1.1.1
- 8.8.8.8
cpus: $ALT_MIN_CPU_LIMIT
mem_limit: $RAM_LIMIT
mem_reservation: $RAM_RESERVE
16 changes: 0 additions & 16 deletions compose/compose.single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ services:
mem_limit: $RAM_LIMIT
mem_reservation: $RAM_RESERVE

honeygain-pot:
container_name: honeygain-pot
image: xterna/honeygain-pot
restart: always
environment:
- EMAIL=${HONEYGAIN_EMAIL:-}
- PASSWORD=${HONEYGAIN_PASSWORD:-}
profiles:
- ${HONEYGAIN:-DISABLED}
dns:
- 1.1.1.1
- 8.8.8.8
cpus: $ALT_MIN_CPU_LIMIT
mem_limit: $RAM_LIMIT
mem_reservation: $RAM_RESERVE

pawns:
container_name: pawns
image: iproyal/pawns-cli
Expand Down
219 changes: 132 additions & 87 deletions scripts/app-selection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,156 @@ display_banner() {
echo "${GREEN}----------------------------------------${NC}\n"
}

display_table() {
json_content=$(cat "$JSON_FILE")
app_data=$(echo "$json_content" | jq -r '.[] | "\(.name) \(.is_enabled)"')

# Table header
printf "%-4s %-21s %-8s\n" "No." "App Name" "Status"
printf "%-4s %-21s %-8s\n" "---" "--------------------" "--------"

counter=1
echo "$app_data" | while IFS=$'\n' read -r line; do
name=$(echo "$line" | cut -d' ' -f1)
is_enabled=$(echo "$line" | cut -d' ' -f2)
choose_application_type() {
if [ "$1" = "service" ]; then
field="service_enabled"
app_type="Service"
action="service"
switch_type="applications"
shortcut="a"
else
field="is_enabled"
app_type="App"
action="application"
switch_type="services"
shortcut="s"
fi

if [ "$is_enabled" = "true" ]; then
status="${GREEN}Enabled${NC}"
else
status="${RED}Disabled${NC}"
fi
display_table_choice "$field" "$app_type" "$action" "$shortcut" "$switch_type"
}

# Content
printf "%-4s %-21s %b\n" "$counter" "$name" "$status"
counter=$((counter + 1))
display_table_choice() {
local field_name="$1"
local header="$2"
local application="$3"
local shortcut=$4
local switch_type=$5

while true; do
display_banner
json_content=$(cat "$JSON_FILE")
app_data=$(echo "$json_content" | jq -r ".[] | select(has(\"$field_name\")) | \"\(.name) \(.${field_name})\"")

echo "${RED}Disabled${NC} ${application}s will not be deployed.\n"

printf "%-4s %-21s %-8s\n" "No." "$header Name" "Status"
printf "%-4s %-21s %-8s\n" "---" "--------------------" "--------"

counter=1
echo "$app_data" | awk -v counter="$counter" -v GREEN="$GREEN" -v RED="$RED" -v NC="$NC" '
{
if ($2 == "true") {
status = GREEN "Enabled" NC
} else {
status = RED "Disabled" NC
}
printf "%-4s %-21s %s\n", counter, $1, status
counter++
}'

echo "\nOptions:"
echo " ${GREEN}e${NC} = ${GREEN}enable all${NC}"
echo " ${RED}d${NC} = ${RED}disable all${NC}"
echo " ${YELLOW}${shortcut}${NC} = ${YELLOW}select ${switch_type}${NC}"
echo " ${BLUE}0${NC} = ${BLUE}exit${NC}"

printf "\nSelect to ${GREEN}enable${NC} | ${RED}disable${NC} $application (1-%s): " "$(echo "$app_data" | wc -l | xargs)"
read -r choice

case $choice in
[1-9]*)
# Enable or disable specific application
if ! [ "$choice" -ge 1 ] || ! [ "$choice" -le "$(echo "$app_data" | wc -l)" ]; then
echo "\nInvalid input! Please enter a number between 1 and $(echo "$app_data" | wc -l)."
printf "\nPress Enter to continue..."; read input
else
# Update entry
temp_file=$(mktemp)
chosen_app=$(echo "$app_data" | sed -n "${choice}p" | cut -d' ' -f1)
jq --indent 4 --arg chosen_app "$chosen_app" --arg field_name "$field_name" '. |= map(if .name == $chosen_app then .[$field_name] |= not else . end)' "$JSON_FILE" > "$temp_file"
mv "$temp_file" "$JSON_FILE"
fi
;;
e)
# Enable all
temp_file=$(mktemp)
jq --indent 4 --arg field_name "$field_name" 'map(if has($field_name) then .[$field_name] = true else . end)' "$JSON_FILE" > "$temp_file"
mv "$temp_file" "$JSON_FILE"
;;
d)
# Disable all
temp_file=$(mktemp)
jq --indent 4 --arg field_name "$field_name" 'map(if has($field_name) then .[$field_name] = false else . end)' "$JSON_FILE" > "$temp_file"
mv "$temp_file" "$JSON_FILE"
;;
a|s)
if [ "$choice" = "s" ]; then
choose_application_type service
else
choose_application_type
fi
return
;;
0)
export_selection
exit 0
;;
*)
echo "\nInvalid option! Please select a valid option."
printf "\nPress Enter to continue..."; read input
;;
esac
done
}

export_selection() {
json_content=$(cat "$JSON_FILE")
app_data=$(echo "$json_content" | jq -r '.[] | "\(.name) \(.is_enabled | if . == true then "ENABLED" else "DISABLED" end)"')

if [ -f "$ENV_DEPLOY_FILE" ]; then
rm -f "$ENV_DEPLOY_FILE"
fi
app_data=$(echo "$json_content" | jq -r '.[] | "\(.name) \(.is_enabled | if . == true then "ENABLED" else "DISABLED" end) \(.service_enabled)"')

echo "$app_data" | while IFS=$'\n' read -r line; do
name=$(echo "$line" | cut -d' ' -f1)
is_enabled=$(echo "$line" | cut -d' ' -f2)
> "$ENV_DEPLOY_FILE" # Empty file

echo "$app_data" | while IFS=' ' read -r name is_enabled service_enabled; do
echo "$name=$is_enabled" >> "$ENV_DEPLOY_FILE"

if [ "$service_enabled" != "null" ]; then
if [ "$service_enabled" = "true" ]; then
service_enabled="ENABLED"
else
service_enabled="DISABLED"
fi
echo "${name}_SERVICE=$service_enabled" >> "$ENV_DEPLOY_FILE"
fi
done
}

import_selection() {
if [ -f "$ENV_DEPLOY_FILE" ]; then
while IFS='=' read -r name is_enabled; do
if [ "$is_enabled" = "ENABLED" ]; then
is_enabled="true"
else
is_enabled="false"
fi
[ -f "$ENV_DEPLOY_FILE" ] || return

json_content=$(jq --arg name "$name" '.[] | select(.name == $name)' "$JSON_FILE")
if [ ! -z "$json_content" ]; then
updated_json_content=$(jq --indent 4 --arg name "$name" --arg is_enabled "$is_enabled" '. |= map(if .name == $name then .is_enabled = ($is_enabled | fromjson) else . end)' "$JSON_FILE")
echo "$updated_json_content" > "$JSON_FILE"
fi
done < "$ENV_DEPLOY_FILE"
fi
jq_filter="map("
while IFS='=' read -r name is_enabled; do
app_enabled="false"
[ "$is_enabled" = "ENABLED" ] && app_enabled="true"

if [ "${name#*_SERVICE}" != "$name" ]; then
jq_filter="$jq_filter if .name == \"${name%_SERVICE}\" then .service_enabled = $app_enabled else . end |"
else
jq_filter="$jq_filter if .name == \"$name\" then .is_enabled = $app_enabled else . end |"
fi
done < "$ENV_DEPLOY_FILE"

jq_filter="${jq_filter%|}" # Remove trailing pipe
jq --indent 4 "$jq_filter)" "$JSON_FILE" > "$JSON_FILE.tmp"
mv "$JSON_FILE.tmp" "$JSON_FILE"
}

parse_cmd_arg() {
if [ "$1" = "--default" ]; then
updated_json_content=$(jq --indent 4 '. |= map(.is_enabled = true)' "$JSON_FILE")
updated_json_content=$(jq --indent 4 '
map(
.is_enabled = true |
if has("service_enabled") then .service_enabled = true else . end
)
' "$JSON_FILE")
echo "$updated_json_content" > "$JSON_FILE"
export_selection
exit 0
Expand Down Expand Up @@ -103,48 +191,5 @@ parse_cmd_arg() {
parse_cmd_arg "$@"

while true; do
display_banner
echo "${RED}Disabled${NC} applications will not be deployed.\n"
display_table

echo "\nOptions:"
echo " ${GREEN}e${NC} = ${GREEN}enable all${NC}"
echo " ${RED}d${NC} = ${RED}disable all${NC}"
echo " ${BLUE}0${NC} = ${BLUE}exit${NC}"

printf "\nSelect to ${GREEN}enable${NC} | ${RED}disable${NC} application (1-%s): " "$(echo "$app_data" | wc -l | xargs)"
read -r choice

case $choice in
[1-9]*)
# Enable or disable specific application
if ! [ "$choice" -ge 1 ] || ! [ "$choice" -le "$(echo "$app_data" | wc -l)" ]; then
echo "\nInvalid input! Please enter a number between 1 and $(echo "$app_data" | wc -l)."
printf "\nPress Enter to continue..."; read input
fi

# Update entry
chosen_app=$(echo "$app_data" | sed -n "${choice}p" | cut -d' ' -f1)
updated_json_content=$(jq --indent 4 --arg chosen_app "$chosen_app" '. |= map(if .name == $chosen_app then .is_enabled |= not else . end)' "$JSON_FILE")
echo "$updated_json_content" > "$JSON_FILE"
;;
e)
# Enable all
updated_json_content=$(jq --indent 4 '. |= map(.is_enabled = true)' "$JSON_FILE")
echo "$updated_json_content" > "$JSON_FILE"
;;
d)
# Disable all
updated_json_content=$(jq --indent 4 '. |= map(.is_enabled = false)' "$JSON_FILE")
echo "$updated_json_content" > "$JSON_FILE"
;;
0)
export_selection
exit 0
;;
*)
echo "\nInvalid option! Please select a valid option."
printf "\nPress Enter to continue..."; read input
;;
esac
choose_application_type "$1"
done
Loading

0 comments on commit ec06c20

Please sign in to comment.