dotfiles/tools/logger.sh
Dict Xiong 9a4c9556f6
[dev] common.sh parses args; riot; getdfs; install.sh -x KEY=VAL (#35)
* common.sh: export vars in env; zshrc: gdebug

* fix error when locale not exists

* DFS_COLOR

* common.sh: parse args automatically

* ci: test common.sh getopts

* common.sh: argparser supports spaces

* ciot: init

* ciot -> diot; remove sibd, sob and snasp

* rename diot -> riot

* update home0 ssh pubkey; fix ci temperarily

* gdebug will record time

* gdebug supports empty; --dry-run wip

* get.dotfiles.cn

* bug fix (Thu Jan  5 20:53:58 CST 2023)

* fix ci

* fix ci

* install.sh: -d will set -x

* ci: -asl

* getdfs: install for another user using -u <uname>

* try fix when su doesnot exist

* bug fix (Thu Jan  5 22:58:40 CST 2023)

* bug fix (Thu Jan  5 22:59:33 CST 2023)

* introduce SUDOE and so debug

* ask_for_yn now use stdout to return

* getdfs: support multiple users

* install.sh: -x to set dfs config; ci

* fix ci

* bug fix (Fri Jan  6 15:11:24 CST 2023)

* auto-detect DFS_NO_WALL

* bug fix (Fri Jan  6 15:43:25 CST 2023)

* getdfs: ${repo}

* bug fix (Fri Jan  6 16:08:41 CST 2023)

* getdfs: prompt user

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2023-01-06 16:14:41 +08:00

105 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
if [[ -x $(command -v hostname) ]]; then
hostname=$(hostname)
elif [[ -x $(command -v uname) ]]; then
hostname=$(uname -n)
elif [[ -x $(command -v hostnamectl) ]]; then
hostname=$(hostnamectl --static)
elif [[ -n "$HOSTNAME" ]]; then
hostname=$HOSTNAME
elif [[ -f /proc/sys/kernel/hostname ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
elif [[ -f /etc/hostname ]]; then
hostname=$(cat /etc/hostname)
else
fmt_fatal "unable to get hostname"
fi
init_uuid()
{
if [[ -f ~/.config/dotfiles/uuid ]]; then
uuid=$(cat ~/.config/dotfiles/uuid)
else
if [[ -x $(command -v uuidgen) ]]; then
uuid=$(uuidgen)
elif [[ -f /proc/sys/kernel/random/uuid ]]; then
uuid=$(cat /proc/sys/kernel/random/uuid)
else
fmt_fatal "unable to generate uuid"
fi
mkdir -p ~/.config/dotfiles
echo "$uuid" > ~/.config/dotfiles/uuid
fi
}
post_beacon()
{
local beacon_type=$1
if [[ -z "$beacon_type" ]]; then
fmt_fatal "beacon type is required"
fi
resp=$(curl -sSL -X POST "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type")
if grep -q "200" <<< "$resp"; then
echo $resp
else
echo $resp >&2
fmt_fatal "error posting beacon"
fi
}
post_log()
{
local log_content=$1
if [[ -z "$log_content" ]]; then
fmt_fatal "log content is required"
fi
init_uuid
resp=$(curl -sSL -X POST -H "Content-Type: text/plain" -d "$1" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid")
if grep -q "200" <<< "$resp"; then
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error posting log: authentification failed"
fmt_info "try to register you hostname and uuid"
fmt_info "hostname: $hostname"
fmt_info "uuid: $uuid"
else
echo $resp >&2
fmt_fatal "error posting log"
fi
}
print_help()
{
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
}
router()
{
if [[ $# != 2 ]]; then
print_help >&2
exit 1
fi
case "$1" in
-h|--help)
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
;;
beacon)
post_beacon "$2"
;;
log)
post_log "$2"
;;
*)
fmt_fatal "invalid argument"
;;
esac
}
router "${GOT_OPTS[@]}"