2023-07-26 21:18:24 +08:00
|
|
|
#!/usr/bin/env bash
|
2022-12-10 19:11:05 +08:00
|
|
|
set -e
|
2022-11-05 19:45:21 +08:00
|
|
|
THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
|
|
|
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
|
2022-12-10 19:11:05 +08:00
|
|
|
if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi
|
2023-05-30 13:43:25 +08:00
|
|
|
if [[ "$DFS_DEV" == "1" ]]; then set -x; fi
|
2023-04-21 12:37:05 +08:00
|
|
|
DFS_CURL_OPTIONS="--retry 2 --max-time 20"
|
2022-08-26 20:43:00 +08:00
|
|
|
|
2023-01-06 16:14:41 +08:00
|
|
|
# parse args and set env, when it is sourced
|
2023-04-21 12:37:05 +08:00
|
|
|
# todo: make this skipable
|
2023-01-06 16:14:41 +08:00
|
|
|
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
|
|
|
ORIGIN_ARGS=("$@")
|
|
|
|
ARG=""
|
|
|
|
GOT_OPTS=()
|
|
|
|
while [[ $# > 0 || -n "$ARG" ]]; do
|
|
|
|
if [[ -z "$ARG" ]]; then ARG=$1; shift; fi
|
|
|
|
case $ARG in
|
|
|
|
-q*|--quite ) export DFS_QUIET=1 ;;
|
|
|
|
-l*|--lite ) export DFS_LITE=1 ;;
|
2023-05-30 13:43:25 +08:00
|
|
|
-d*|--dev ) export DFS_DEV=1; set -x ;;
|
|
|
|
-D*|--dry-run ) export DFS_DRY_RUN=1 ;;
|
2023-01-06 16:14:41 +08:00
|
|
|
--color ) export DFS_COLOR=1 ;;
|
|
|
|
--*=* ) GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}") ;;
|
|
|
|
--* ) GOT_OPTS+=("$ARG") ;;
|
|
|
|
-* ) GOT_OPTS+=("${ARG:0:2}") ;;
|
|
|
|
* ) GOT_OPTS+=("$ARG") ;;
|
|
|
|
esac
|
|
|
|
if [[ "$ARG" == "--"* || ! "$ARG" == "-"* || ${#ARG} -le 2 ]]; then
|
|
|
|
ARG=""
|
|
|
|
else
|
|
|
|
ARG=-${ARG:2}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
set -- "${ORIGIN_ARGS[@]}"
|
|
|
|
unset ARG
|
2023-04-21 12:37:05 +08:00
|
|
|
# outputs: GOT_OPTS and ORIGIN_ARGS
|
2023-01-06 16:14:41 +08:00
|
|
|
fi
|
|
|
|
|
2022-08-26 20:43:00 +08:00
|
|
|
# Color settings
|
|
|
|
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
|
2023-01-06 16:14:41 +08:00
|
|
|
if [[ -t 1 || "$DFS_COLOR" == "1" ]]; then
|
2022-08-26 20:43:00 +08:00
|
|
|
is_tty() {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else
|
|
|
|
is_tty() {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
supports_truecolor() {
|
|
|
|
case "$COLORTERM" in
|
|
|
|
truecolor|24bit) return 0 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case "$TERM" in
|
|
|
|
iterm |\
|
|
|
|
tmux-truecolor |\
|
|
|
|
linux-truecolor |\
|
|
|
|
xterm-truecolor |\
|
|
|
|
screen-truecolor) return 0 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt_fatal() {
|
|
|
|
printf '%sfatal: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "${FMT_RESET}" >&2
|
2022-11-05 19:45:21 +08:00
|
|
|
exit 1
|
2022-08-26 20:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt_error() {
|
|
|
|
printf '%serror: %s%s\n' "${FMT_RED}" "$*" "${FMT_RESET}" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt_warning() {
|
|
|
|
printf '%swarning: %s%s\n' "${FMT_YELLOW}" "$*" "${FMT_RESET}" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt_info() {
|
2023-04-21 12:37:05 +08:00
|
|
|
printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&2
|
2022-08-26 20:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt_note() {
|
2023-04-21 12:37:05 +08:00
|
|
|
printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&2
|
2022-08-26 20:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_color() {
|
|
|
|
# Only use colors if connected to a terminal
|
|
|
|
if ! is_tty; then
|
|
|
|
FMT_RAINBOW=""
|
|
|
|
FMT_RED=""
|
|
|
|
FMT_GREEN=""
|
|
|
|
FMT_YELLOW=""
|
|
|
|
FMT_BLUE=""
|
|
|
|
FMT_BOLD=""
|
|
|
|
FMT_RESET=""
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if supports_truecolor; then
|
|
|
|
FMT_RAINBOW="
|
|
|
|
$(printf '\033[38;2;255;0;0m')
|
|
|
|
$(printf '\033[38;2;255;97;0m')
|
|
|
|
$(printf '\033[38;2;247;255;0m')
|
|
|
|
$(printf '\033[38;2;0;255;30m')
|
|
|
|
$(printf '\033[38;2;77;0;255m')
|
|
|
|
$(printf '\033[38;2;168;0;255m')
|
|
|
|
$(printf '\033[38;2;245;0;172m')
|
|
|
|
"
|
|
|
|
else
|
|
|
|
FMT_RAINBOW="
|
|
|
|
$(printf '\033[38;5;196m')
|
|
|
|
$(printf '\033[38;5;202m')
|
|
|
|
$(printf '\033[38;5;226m')
|
|
|
|
$(printf '\033[38;5;082m')
|
|
|
|
$(printf '\033[38;5;021m')
|
|
|
|
$(printf '\033[38;5;093m')
|
|
|
|
$(printf '\033[38;5;163m')
|
|
|
|
"
|
|
|
|
fi
|
|
|
|
|
|
|
|
FMT_RED=$(printf '\033[31m')
|
|
|
|
FMT_GREEN=$(printf '\033[32m')
|
|
|
|
FMT_YELLOW=$(printf '\033[33m')
|
|
|
|
FMT_BLUE=$(printf '\033[34m')
|
|
|
|
FMT_BOLD=$(printf '\033[1m')
|
|
|
|
FMT_RESET=$(printf '\033[0m')
|
|
|
|
}
|
|
|
|
# END of color settings
|
|
|
|
|
2022-11-17 20:02:35 +08:00
|
|
|
SUDO=''
|
2023-01-06 16:14:41 +08:00
|
|
|
SUDOE=''
|
2022-11-17 20:02:35 +08:00
|
|
|
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
|
|
|
|
SUDO='sudo'
|
2023-01-06 16:14:41 +08:00
|
|
|
SUDOE='sudo -E'
|
2022-11-17 20:02:35 +08:00
|
|
|
fi
|
|
|
|
|
2022-08-26 20:43:00 +08:00
|
|
|
ask_for_yN()
|
|
|
|
{
|
2023-01-06 16:14:41 +08:00
|
|
|
if [[ "$DFS_QUIET" == "1" ]]; then
|
|
|
|
echo 0
|
|
|
|
else
|
2022-08-26 20:43:00 +08:00
|
|
|
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
|
|
|
|
case $yn in
|
2023-01-06 16:14:41 +08:00
|
|
|
[Yy]* ) echo 1;;
|
|
|
|
* ) echo 0;;
|
2022-08-26 20:43:00 +08:00
|
|
|
esac
|
2023-01-06 16:14:41 +08:00
|
|
|
fi
|
2022-11-05 19:45:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ask_for_Yn()
|
|
|
|
{
|
2023-01-06 16:14:41 +08:00
|
|
|
if [[ "$DFS_QUIET" == "1" ]]; then
|
|
|
|
echo 1
|
|
|
|
else
|
2022-11-05 19:45:21 +08:00
|
|
|
read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
|
|
|
|
case $yn in
|
2023-01-06 16:14:41 +08:00
|
|
|
[Nn]* ) echo 0;;
|
|
|
|
* ) echo 1;;
|
2022-11-05 19:45:21 +08:00
|
|
|
esac
|
2023-01-06 16:14:41 +08:00
|
|
|
fi
|
2022-08-26 20:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
post_log()
|
|
|
|
{
|
2022-12-03 20:37:20 +08:00
|
|
|
if [[ $# != 3 || -z "$1" || -z "$2" || -z "$3" ]]; then
|
|
|
|
fmt_fatal "usage: post_log <level> <section> <content>"
|
|
|
|
fi
|
2023-04-21 12:37:05 +08:00
|
|
|
"${DOTFILES}/tools/frigg-client.sh" "log" "[$1][$2] $3"
|
2022-08-26 20:43:00 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
apost_log()
|
|
|
|
{
|
|
|
|
post_log "$@" 1>/dev/null &
|
|
|
|
}
|
|
|
|
|
|
|
|
post_beacon()
|
|
|
|
{
|
2023-04-21 12:37:05 +08:00
|
|
|
if [[ $# < 1 || -z "$1" ]]; then
|
2022-12-03 20:37:20 +08:00
|
|
|
fmt_fatal "usage: post_beacon <beacon>"
|
|
|
|
fi
|
2023-04-21 12:37:05 +08:00
|
|
|
"${DOTFILES}/tools/frigg-client.sh" "beacon" "$1" "$2"
|
2022-12-03 20:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
apost_beacon()
|
|
|
|
{
|
|
|
|
post_beacon "$@" 1>/dev/null &
|
|
|
|
}
|
|
|
|
|
|
|
|
get_os_type()
|
|
|
|
{
|
2024-04-26 21:44:20 +08:00
|
|
|
test -z "$DFS_OS_TYPE" || { echo "$DFS_OS_TYPE"; return; }
|
2022-11-05 19:45:21 +08:00
|
|
|
local ans="unknown"
|
2022-08-26 20:43:00 +08:00
|
|
|
case "$(uname -s)" in
|
|
|
|
Darwin*) ans="MacOS";;
|
|
|
|
CYGWIN*) ans="Cygwin";;
|
|
|
|
MSYS* ) ans="MSYS";;
|
|
|
|
Linux* ) ans="Linux";;
|
|
|
|
*) ans="unknown";;
|
|
|
|
esac
|
2024-04-26 20:18:21 +08:00
|
|
|
export DFS_OS_TYPE="$ans"
|
2022-08-26 20:43:00 +08:00
|
|
|
echo $ans | tr '[:upper:]' '[:lower:]'
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
get_linux_dist()
|
|
|
|
{
|
2024-04-26 21:44:20 +08:00
|
|
|
test -z "$DFS_LINUX_DIST" || { echo "$DFS_LINUX_DIST"; return; }
|
2022-11-05 19:45:21 +08:00
|
|
|
local ans="unknown"
|
2022-08-26 20:43:00 +08:00
|
|
|
if [ -f /etc/os-release ]; then
|
|
|
|
. /etc/os-release
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="$ID"
|
2022-08-26 20:43:00 +08:00
|
|
|
elif type lsb_release >/dev/null 2>&1; then
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="$(lsb_release -si)"
|
2022-08-26 20:43:00 +08:00
|
|
|
elif [ -f /etc/lsb-release ]; then
|
|
|
|
. /etc/lsb-release
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="$DISTRIB_ID"
|
2022-08-26 20:43:00 +08:00
|
|
|
elif [ -f /etc/debian_version ]; then
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="Debian"
|
2022-08-26 20:43:00 +08:00
|
|
|
elif [ -f /etc/SuSe-release ]; then
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="SUSE"
|
2022-08-26 20:43:00 +08:00
|
|
|
elif [ -f /etc/redhat-release ]; then
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="RedHat"
|
2022-08-26 20:43:00 +08:00
|
|
|
else
|
2022-11-05 19:45:21 +08:00
|
|
|
ans="unknown"
|
2022-08-26 20:43:00 +08:00
|
|
|
fi
|
2024-04-26 20:18:21 +08:00
|
|
|
export DFS_LINUX_DIST="$ans"
|
2022-08-26 20:43:00 +08:00
|
|
|
echo $ans | tr '[:upper:]' '[:lower:]'
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:37:20 +08:00
|
|
|
get_os_name()
|
|
|
|
{
|
|
|
|
local ans=$(get_os_type)
|
|
|
|
if [[ "$ans" == "linux" ]]; then
|
|
|
|
ans=$(get_linux_dist)
|
|
|
|
fi
|
|
|
|
echo $ans
|
|
|
|
}
|
|
|
|
|
2023-11-06 19:13:22 +08:00
|
|
|
is_port_free() {
|
|
|
|
( echo $1 | grep -qxE "[1-9][0-9]{0,4}" ) || false
|
|
|
|
local cmd
|
|
|
|
case $(get_os_type) in
|
|
|
|
macos ) cmd="netstat -van | grep -q \".$1\"" ;;
|
|
|
|
cygwin|msys ) cmd="netstat -ano | grep -q \":$1\"" ;;
|
|
|
|
*) cmd="netstat -tuanp | grep -q \":$1\"" ;;
|
|
|
|
esac
|
|
|
|
if eval $cmd; then
|
|
|
|
return 2
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_free_port() {
|
|
|
|
while
|
|
|
|
local port=$(shuf -n 1 -i 49152-65535)
|
|
|
|
! is_port_free $port
|
|
|
|
do
|
|
|
|
continue
|
|
|
|
done
|
|
|
|
echo $port
|
|
|
|
}
|
|
|
|
|
2023-12-06 21:52:54 +08:00
|
|
|
is_function() {
|
|
|
|
test "$(type -t "$1")" = "function"
|
|
|
|
}
|
|
|
|
|
2022-11-05 19:45:21 +08:00
|
|
|
# if bash-ed, else source-d
|
2022-08-26 20:43:00 +08:00
|
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
|
|
$1 "${@:2}"
|
|
|
|
else
|
|
|
|
setup_color
|
|
|
|
fi
|
2022-11-17 20:02:35 +08:00
|
|
|
|
|
|
|
unset THIS_DIR_COMMON_SH
|