Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Last active March 14, 2023 23:47
Show Gist options
  • Save littlefuntik/3f1c20b223268d5125cd01bb5c2c5d40 to your computer and use it in GitHub Desktop.
Save littlefuntik/3f1c20b223268d5125cd01bb5c2c5d40 to your computer and use it in GitHub Desktop.
#!/bin/sh
# utils / helpers
CSRF() {
printf '%s' "$(tr -dc '0-9a-z' </dev/urandom | head -c3)"
}
RANDOM_EMAIL() {
printf '%d%[email protected]' "$(date +%s)" "$(tr -dc '[:lower:]' </dev/urandom | head -c8)"
}
RANDOM_PASSWORD() {
printf '%s' "$(tr -dc 'a-z0-9' </dev/urandom | head -c8)"
}
RANDOM_NAME() {
printf '%s%s' "$(tr -dc '[:upper:]' </dev/urandom | head -c1)" "$(tr -dc '[:lower:]' </dev/urandom | head -c7)"
}
RANDOM_PHONE() {
printf '%s%s' "3" "$(tr -dc '[:digit:]' </dev/urandom | head -c11)"
}
# JSONVAL_S '{"text":"test"}' 'text' >> test
JSONVAL_S() {
printf '%s' "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*\"[^\"]*" | grep -o '[^"]*$'
}
# JSONVAL_N '{"text":123}' 'text' >> 123
JSONVAL_N() {
printf '%s' "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*[[:digit:]][[:digit:]]*[.]*[[:digit:]]*" | grep -o '[[:digit:]][[:digit:]]*[.]*[[:digit:]]*$'
}
# JSONVAL_B '{"text":true}' 'text' >> true
JSONVAL_B() {
printf '%s' "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*true\|\"$2\"[[:space:]]*:[[:space:]]*false" | grep -o "true$\|false$"
# | grep -o '[(true|false)]$'
}
# COOKIE_VALUE 'sid' '/tmp/curl-cookie-file.jar'
COOKIE_VALUE() {
VALUE="$(grep -oP "\t$1\t([^\s]+)" "$2" | grep -oP "[^\s]+$")"
}
LOG_TIME() {
LOG_TIME_NOW="$(date +%s)"
if [ "$LOG_TIME_PREV" = "" ]; then
printf "[%s]\n" "$(date +%T)"
else
printf "[%s] +%s sec\n" "$(date +%T)" "$(( $LOG_TIME_NOW - $LOG_TIME_PREV ))"
fi
LOG_TIME_PREV="$LOG_TIME_NOW"
}
START() {
printf "~ %s\n" "$1"
}
REPORT() {
REPORT="$(printf "%s\n%s\n\n" "$REPORT" "$1")"
printf "\n# %s\n" "$1"
}
STEP () {
if [ "$STEP_NUMBER" = "" ]; then
STEP_NUMBER="0"
fi
STEP_NUMBER="$(( STEP_NUMBER += 1 ))"
REPORT "$STEP_NUMBER. $1"
}
SUMMARY() {
printf "\n~ Summary%s\n" "$REPORT"
}
FAIL() {
printf "%s\n%s\n\n FAILED (%s)\n\n" "$(printf "! ERROR: %s" "$1")" "$(SUMMARY)" "$(date)"
exit 1
}
# Run the command and set the result to the $RES variable
# Example usage:
# CMD "bash --version"
CMD() {
printf "\n> %s\n" "$1"
RES="$(sh -c "$1")"
printf "< %s\n" "$RES"
}
# Run PostgreSQL command and set the result to the $RES variable
# Example usage:
# PSQL "SELECT id FROM cart LIMIT 1"
PSQL() {
CMD "PGPASSWORD=\"$PGPASSWORD\" psql -c \"$1\" -h \"$PGHOST\" -p \"$PGPORT\" -U \"$PGUSER\" -d \"$PGDATABASE\" -tA"
}
# Run PostgreSQL command: delete all tables from public or custom schema
# Example usage:
# PSQL_DROP_TABLES "public"
PSQL_DROP_TABLES() {
if [ "$1" = "" ]; then
PSQL_SCHEMA="public"
else
PSQL_SCHEMA="$1"
fi
PSQL "select 'drop table if exists \\\"' || tablename || '\\\" cascade;' from pg_tables where schemaname = '$PSQL_SCHEMA';"
PSQL "$RES"
}
# Run PostgreSQL command: delete all sequences from public or custom schema
# Example usage:
# PSQL_DROP_TABLES "public"
PSQL_DROP_SEQUENCES() {
if [ "$1" = "" ]; then
PSQL_SCHEMA="public"
else
PSQL_SCHEMA="$1"
fi
PSQL "select 'drop sequence if exists \\\"' || relname || '\\\" cascade;' from pg_class where relkind = 'S';"
PSQL "$RES"
}
# Run PostgreSQL command: delete all tables and sequences where from public or custom schema
# Example usage:
# PSQL_CLEAR "public"
PSQL_CLEAR() {
PSQL_DROP_TABLES "$1"
PSQL_DROP_SEQUENCES "$1"
}
# Run PostgreSQL command: delete all tables and sequences where from public or custom schema
# Example usage:
# PSQL_CLEAR "public"
PSQL_DUMP() {
if [ "$1" = "" ]; then
PSQL_DUMP_FILE="$(mktemp --suffix="-psql-dump")"
else
PSQL_DUMP_FILE="$1"
fi
CMD "PGPASSWORD=\"$PGPASSWORD\" pg_dump --data-only -h \"$PGHOST\" -p \"$PGPORT\" -U \"$PGUSER\" -d \"$PGDATABASE\" >\"$PSQL_DUMP_FILE\""
}
GQL() {
GQL_PREPARED_QUERY="$(printf "%s" "$1" | sed "s/\"/\\\\\\\\\\\\\"/g")"
CMD "curl -s -d \"{\\\"query\\\":\\\"$GQL_PREPARED_QUERY\\\"}\" -H \"$GRAPHQL_BASIC_AUTH\" -H \"R-Auth: Bearer $GRAPHQL_JWT_TOKEN\" -H \"Content-Type: application/json\" -X POST \"$GRAPHQL_URL\""
}
# USER_GQL "url" "query" "cookie_file_path" "csrf"
USER_GQL() {
if [ "$4" = "" ]; then
USER_GQL_CSRF="$(CSRF)"
else
USER_GQL_CSRF="$4"
fi
GQL_PREPARED_QUERY="$(printf "%s" "$2" | sed "s/\"/\\\\\\\\\\\\\"/g")"
CMD "curl -s -d \"{\\\"query\\\":\\\"$GQL_PREPARED_QUERY\\\"}\" -H \"Csrf-Token: $USER_GQL_CSRF\" -H \"Cookie: _uss-csrf=$USER_GQL_CSRF\" -H \"Content-Type: application/json\" -b \"$3\" -X POST \"$1\""
}
# VALIDATE "test_field_name" "eq" "1" "test_value"
# VALIDATE "test_field_name" "contains" "substring" "test_value"
VALIDATE() {
if [ "$#" -gt "3" ]; then
TEST_VALUE="$4"
else
TEST_VALUE="$RES"
fi
VALIDATE_EXPECTED_MSG_ENABLED="1"
VALIDATE_ERROR="1"
case "$2" in
"="|"==") if [ "$TEST_VALUE" = "$3" ]; then VALIDATE_ERROR="0"; VALIDATE_EXPECTED_MSG_ENABLED="0"; fi;;
"!=") if [ "$TEST_VALUE" != "$3" ]; then VALIDATE_ERROR="0"; fi;;
"eq") if [ "$TEST_VALUE" -eq "$3" ]; then VALIDATE_ERROR="0"; VALIDATE_EXPECTED_MSG_ENABLED="0"; fi;;
"ne") if [ "$TEST_VALUE" -ne "$3" ]; then VALIDATE_ERROR="0"; fi;;
"gt"|">") if [ "$TEST_VALUE" -gt "$3" ]; then VALIDATE_ERROR="0"; fi;;
"ge"|">=") if [ "$TEST_VALUE" -ge "$3" ]; then VALIDATE_ERROR="0"; fi;;
"lt"|"<") if [ "$TEST_VALUE" -lt "$3" ]; then VALIDATE_ERROR="0"; fi;;
"le"|"<=") if [ "$TEST_VALUE" -le "$3" ]; then VALIDATE_ERROR="0"; fi;;
"contains") if printf "%s" "$TEST_VALUE" | grep -q "$3"; then VALIDATE_ERROR="0"; fi;;
esac
VALIDATE_MSG="$(printf "condition: %s %s \"%s\"" "$1" "$2" "$3")"
if [ "$VALIDATE_EXPECTED_MSG_ENABLED" = "1" ]; then
VALIDATE_MSG="$(printf "%s, expected value \"%s\"" "$VALIDATE_MSG" "$TEST_VALUE")"
fi
if [ "$VALIDATE_ERROR" = "0" ]; then
printf "+ %s\n" "$VALIDATE_MSG"
else
FAIL "$VALIDATE_MSG"
fi
}
# VALIDATE_JSON "code" "eq" "1" "$JSON"
VALIDATE_JSON() {
if [ "$#" -gt "3" ]; then
JSON_CONTENT="$4"
else
JSON_CONTENT="$RES"
fi
VALUE="$(JSONVAL_S "$JSON_CONTENT" "$1" )"
if [ "$VALUE" = "" ]; then
VALUE="$(JSONVAL_N "$JSON_CONTENT" "$1" )"
fi
if [ "$VALUE" = "" ]; then
VALUE="$(JSONVAL_B "$JSON_CONTENT" "$1" )"
fi
VALIDATE "$1" "$2" "$3" "$VALUE"
}
CMP_DIRECTORIES_GUI() {
meld "$1" "$2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment