-
Notifications
You must be signed in to change notification settings - Fork 0
/
.common
208 lines (168 loc) · 5.31 KB
/
.common
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
#!/usr/bin/env bash
# TODO Add these functions to the readme
# TODO Sort functions in alphabetic order
# The location of .config when .common is loaded via .zsh_profile.
CONFIG_FILE_LOCAL="$(dirname "$0")/.config"
# The location of .config file when .common is sourced from a file.
CONFIG_FILE_ABSOLUTE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/.config"
if [ -f "$CONFIG_FILE_LOCAL" ]; then
source "$CONFIG_FILE_LOCAL"
elif [ -f "$CONFIG_FILE_ABSOLUTE" ]; then
source "$CONFIG_FILE_ABSOLUTE"
fi
# Set default variables
MYSQL_HOST=${MYSQL_HOST:-localhost}
MYSQL_USER=${MYSQL_USER:-root}
MYSQL_PASS=${MYSQL_PASS:-root}
MYSQL_CHAR_SET=${MYSQL_CHAR_SET:-utf8mb4}
MYSQL_COLLATE=${MYSQL_COLLATE:-utf8mb4_unicode_ci}
# Bash colors and formatting
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
function log() {
echo -e "\033[34m[$(date +'%F %T')]\033[0m $1\033[0m"
}
function log_error() {
echo -e "\033[31m$1\033[0m"
}
function log_success() {
echo -e "\033[32m$1\033[0m"
}
function log_text() {
echo -e "$1\033[0m"
}
function log_warning() {
echo -e "\033[33m$1\033[0m"
}
# Converts bytes value to human-readable string [$1: bytes value]
# Source: https://unix.stackexchange.com/a/259254
function bytesToHumanReadable() {
local i=${1:-0} d="" s=0 S=("Bytes" "KiB" "MiB" "GiB" "TiB" "PiB" "EiB" "YiB" "ZiB")
while ((i > 1024 && s < ${#S[@]}-1)); do
printf -v d ".%02d" $((i % 1024 * 100 / 1024))
i=$((i / 1024))
s=$((s + 1))
done
echo "$i$d ${S[$s]}"
}
function getWebRoot() {
for DIR in "${WEBROOT_POSSIBILITIES[@]:-("web")}"; do
if [ -d "./$DIR" ]; then
echo "$DIR"
break
fi
done
}
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$_" || exit 1;
}
# Determine size of a file or total size of a directory
function fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$*" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* ./*;
fi;
}
# Create a data URL from a file
function dataurl() {
mimeType=$(file -b --mime-type "$1");
local mimeType
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
function _mysql_get_password_option() {
if [ -z "$MYSQL_PASS" ]; then
echo ""
else
echo "--password=${MYSQL_PASS}"
fi
}
function mysql_test_connection() {
command -v mysqladmin > /dev/null || { log_error "mysqladmin not found."; return 1; }
command -v mysql > /dev/null || { log_error "mysql not found."; return 1; }
if [ -z "$MYSQL_PASS" ]; then
mysqladmin -u "$MYSQL_USER" --skip-password status > /dev/null || { log_error "Could not reach database."; return 1; }
else
mysqladmin -u "$MYSQL_USER" "--password=$MYSQL_PASS" status > /dev/null || { log_error "Could not reach database."; return 1; }
fi
}
function mysql_db_exists() {
DB_NAME=$1
if [ -z "$DB_NAME" ]; then
log_error "Please provide a database name."
return 1
fi
DB_EXISTS=$(mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" "$(_mysql_get_password_option)" \
--batch --skip-column-names -e "SHOW DATABASES LIKE '$DB_NAME';" \
| grep "$DB_NAME" > /dev/null; echo "$?"
)
if [ 0 -eq "$DB_EXISTS" ]; then
log_success "Database named \033[1m$DB_NAME\033[0;32m exists."
else
log_error "Database named \033[1m$DB_NAME\033[0;31m not found."
fi
return "$DB_EXISTS"
}
# TODO Add to docs
# TODO Create multiple databases
function mysql_create_db() {
DB_NAME=$1
if [ -z "$DB_NAME" ]; then
log_error "Please provide a database name."
return 1
fi
mysql_test_connection || return 1
mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" "$(_mysql_get_password_option)" -e "
CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` CHARACTER SET \`$MYSQL_CHAR_SET\` COLLATE \`$MYSQL_COLLATE\`;
CREATE USER IF NOT EXISTS $MYSQL_USER@$MYSQL_HOST IDENTIFIED BY '$MYSQL_PASS';
GRANT ALL PRIVILEGES ON \`$DB_NAME\`.* TO '$MYSQL_USER'@'$MYSQL_HOST' IDENTIFIED BY '$MYSQL_PASS';
FLUSH PRIVILEGES;
"
CODE=$?
if [ 0 -ne $CODE ]; then
log_error "Failed to create database."
return $CODE
fi
log_success "Database named \033[1m$DB_NAME\033[0;32m created succesfully."
}
# TODO Add to docs
alias mcdb="mysql_create_db"
# TODO Add to docs
# TODO Delete multiple databases
# TODO Add confirmation to make sure the DB should be removed
function mysql_drop_db() {
DB_NAME=$1
if [ -z "$DB_NAME" ]; then
log_error "Please provide a database name."
return 1
fi
mysql_test_connection || return 1
mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" "$(_mysql_get_password_option)" -e "
DROP DATABASE IF EXISTS \`$DB_NAME\`;
"
CODE=$?
if [ 0 -ne $CODE ]; then
log_error "Failed to drop database."
return $CODE
fi
log_success "Database named \033[1m$DB_NAME\033[0;32m dropped succesfully."
}
# TODO Add to docs
alias mddb="mysql_drop_db"
# My last commits
git-standup() {
AUTHOR=${AUTHOR:="$(git config user.name)"}
since=yesterday
if [[ $(date +%u) == 1 ]]; then
since="2 days ago"
fi
git log --all --since "$since" --oneline --author="$AUTHOR"
}