-
Notifications
You must be signed in to change notification settings - Fork 0
/
libV2.sh
213 lines (192 loc) · 6.03 KB
/
libV2.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# NextLibV2 - the next library for bash
DEBUG=false
DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
-d|--debug)
DEBUG=true
shift
;;
-D|--dir)
DIR="$2"
shift 2
;;
*)
break
;;
esac
done
BLUE='\033[38;5;32;1m'
GREEN='\033[1;32m'
RED='\033[1;31m'
WHITE='\033[1;37m'
YELLOW='\033[1;33m'
NC='\033[0m'
RESET='\e[0m'
if [ "$WATERMARK" != "false" ]; then
echo -e "\033[1;32m-------------------------\033[0m"
echo -e "\033[1;32m[LIB]\033[0m \033[1;32mPowered by \033[0;32mNextLib\033[0m"
echo -e "\033[1;32m-------------------------\033[0m"
fi
log_info() {
echo -e "${GREEN}[INFO]${NC} ${WHITE}$1${NC}"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} ${WHITE}$1${NC}"
}
log_error() {
echo -e "${RED}[ERROR]${NC} ${WHITE}$1${NC}"
}
log_fatal() {
echo -e "${RED}[FATAL]${NC} ${WHITE}$1${NC}"
}
runasroot() {
if [[ $EUID -ne 0 ]]; then
fatal "This script requires root privileges. Please run it as root or with sudo."
exit 1
fi
}
check_distribution() {
supported_distributions=("$@")
if [[ -f /etc/os-release ]]; then
source /etc/os-release
for dist in "${supported_distributions[@]}"; do
if [[ "$PRETTY_NAME" == *"$dist"* ]]; then
return
fi
done
fi
log_fatal "This is an unsupported Linux distribution/version."
exit 1
}
apt_install() {
apt-get update -y > "$OUTPUT_TARGET"
apt-get upgrade -y > "$OUTPUT_TARGET"
local package_list="$1" # Get the comma-separated package list as the first argument
IFS=',' read -ra packages <<< "$package_list" # Parse the list into an array
for package in "${packages[@]}"; do
if ! dpkg -l | grep -q "$package"; then
log_info "Installing $package..."
if apt-get install -y "$package" > "$OUTPUT_TARGET"; then
log_info "$package is successfully installed."
else
log_error "Failed to install $package."
fi
else
log_info "$package is already installed."
fi
done
}
mariadb_manage() {
if ! dpkg -l | grep -q "mariadb-server"; then
apt-get install -y "mariadb-server" > "$OUTPUT_TARGET"
fi
local action="$1"
local name="$2"
local pass="$3"
if [ "$pass" = "random" ]; then
pass=$(openssl rand -base64 12)
log_warn "Generated random password: $pass"
export PASS=$pass
fi
case "$action" in
dbcreate)
log_info "Creating database: $name"
mariadb -e "DROP DATABASE IF EXISTS $name; CREATE DATABASE $name;"
;;
dbdelete)
log_info "Deleting database: $name"
mariadb -e "DROP DATABASE IF EXISTS $name;"
;;
usercreate)
log_info "Creating user: $name"
mariadb -e "DROP USER IF EXISTS '$name'@'127.0.0.1'; FLUSH PRIVILEGES; CREATE USER '$name'@'127.0.0.1' IDENTIFIED BY '$pass';"
;;
userdelete)
log_info "Deleting user: $name"
mariadb -e "DROP USER IF EXISTS '$name'@'localhost';"
;;
*)
fatal "Invalid action. Use dbcreate, dbdelete, usercreate, or userdelete."
exit 1
;;
esac
}
question() {
local question_text="$1"
local response_var="$2"
local validate_type="$3"
local user_response
while true; do
echo -e "${BLUE}[QUESTION]${NC} ${WHITE}$question_text${NC}"
if [ "$validate_type" == "password" ]; then
read -s -p "Your answer: " user_response
else
read -p "Your answer: " user_response
fi
if [ -n "$validate_type" ]; then
if [ "$validate_type" == "url" ]; then
if [[ $user_response =~ ^(http:\/\/localhost|http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|https?:\/\/(localhost|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[a-zA-Z0-9.-]+\.[a-zA-Z]+))$ ]]; then
echo "Valid URL."
eval "$response_var=\"$user_response\""
break
else
error "Invalid URL. Please try again."
fi
elif [ "$validate_type" == "email" ]; then
if [[ $user_response =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]; then
echo "Valid email."
eval "$response_var=\"$user_response\""
break
else
error "Invalid email. Please try again."
fi
elif [ "$validate_type" == "password" ]; then
echo # Add a newline after the hidden password input
eval "$response_var=\"$user_response\""
break
else
echo "Validation type not supported."
break
fi
else
eval "$response_var=\"$user_response\""
break
fi
done
}
replace() {
local file="$1"
local search_string="$2"
local replace_string="$3"
if [ -f "$file" ]; then
# Use sed with double quotes to allow variable substitution
sed -i "s|$search_string|$replace_string|g" "$file"
log_info "Replaced '$search_string' with '$replace_string' in $file."
else
log_error "File $file not found."
fi
}
manage_cron_job() {
local action="$1"
local cron_command="$2"
if [ "$action" == "add" ]; then
(crontab -l ; echo "$cron_command") | crontab -
log_info "Added the following cron job: $cron_command"
elif [ "$action" == "remove" ]; then
crontab -l | grep -v "$cron_command" | crontab -
log_info "Removed the cron job containing: $cron_command"
else
log_error "Invalid action. Use 'add' or 'remove'."
fi
}
if [ "$DEBUG" = true ]; then
log_info "Debug mode is enabled."
OUTPUT_TARGET="/dev/stdin"
else
OUTPUT_TARGET="/dev/null"
fi
if [ -n "$DIR" ]; then
log_info "Directory set to: $DIR"
fi