1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-11-25 22:00:41 +08:00
NeoNetwork/scripts/pretty-output.sh

144 lines
3.2 KiB
Bash
Raw Normal View History

2020-04-30 21:10:29 +08:00
#!/usr/bin/env bash
set -e
2020-07-21 02:06:15 +08:00
##########################################
# =============== Colors =============== #
##########################################
2020-04-30 21:10:29 +08:00
ESC='\033'
2020-07-21 02:06:15 +08:00
RESET="${ESC}[0m" #Reset all attributes
BRIGHT="${ESC}[1m" #Bright
DIM="${ESC}[2m" #Dim
BLINK="${ESC}[5m" #Blink
2020-04-30 21:10:29 +08:00
# Foreground Colours #
2020-07-21 02:06:15 +08:00
FBLACK="${ESC}[30m" #Black
FRED="${ESC}[31m" #Red
FGREEN="${ESC}[32m" #Green
FYELLOW="${ESC}[33m" #Yellow
FBLUE="${ESC}[34m" #Blue
FMAGENTA="${ESC}[35m" #Magenta
FCYAN="${ESC}[36m" #Cyan
FWHITE="${ESC}[37m" #White
2020-04-30 21:10:29 +08:00
# Background Colours #
2020-07-21 02:06:15 +08:00
BBLACK="${ESC}[40m" #Black
BRED="${ESC}[41m" #Red
BGREEN="${ESC}[42m" #Green
BYELLOW="${ESC}[43m" #Yellow
BBLUE="${ESC}[44m" #Blue
BMAGENTA="${ESC}[45m" #Magenta
BCYAN="${ESC}[46m" #Cyan
BWHITE="${ESC}[47m" #White
#########################
# Functions: #
# Make your life easier #
#########################
2020-04-30 21:10:29 +08:00
# Error Message that stops the script
2020-07-21 02:06:15 +08:00
errmsg() {
2020-04-30 21:10:29 +08:00
echo -en "${BRED}>>${RESET} ${FMAGENTA}${*}${RESET}"
return 1
}
# Normal Message
2020-07-21 02:06:15 +08:00
msg() {
2020-04-30 21:10:29 +08:00
echo -en "${BBLUE}>>${RESET} ${BRIGHT}${FGREEN}${*}${RESET}"
}
# Debug Level Verbose
2020-07-21 02:06:15 +08:00
dbgmsg() {
2020-04-30 21:10:29 +08:00
echo -en "${BRIGHT}${BBLUE}>>${RESET} ${BRIGHT}${FGREEN}${*}${RESET}"
}
# Verbose Message
2020-07-21 02:06:15 +08:00
vmsg() {
2020-04-30 21:10:29 +08:00
echo -en "${BRIGHT}${BBLUE}>>${RESET} ${BRIGHT}${FCYAN}${*}${RESET}"
}
# Formatted Output
# for TUN30
2020-07-21 02:06:15 +08:00
print_tun30() {
2020-04-30 21:10:29 +08:00
printf "${FGREEN}%-20s${RESET}|${FYELLOW}%10s${RESET}| ${FCYAN}%20s ${BRIGHT}${FBLUE}<--> ${FMAGENTA}%s${RESET}\n" \
"$1" "$2" "$3" "$4"
}
2020-07-21 02:06:15 +08:00
print_subnet() {
2020-04-30 21:10:29 +08:00
printf "${FGREEN}%-20s${RESET}${BRIGHT}${FBLUE}|| ${FMAGENTA}%s${RESET}\n\t>> %s\n" \
"$1" "$2" "$3"
}
2020-07-21 02:06:15 +08:00
print_ptp() {
2020-04-30 21:10:29 +08:00
upstream_ip="${1%~*}"
downstream_ip="${1#$upstream_ip}"
downstream_ip="${downstream_ip#*~}"
upstream_ip="${upstream_ip#PTP/}"
printf "${BRIGHT}${FGREEN}%-10s${RESET}| ${BRIGHT}${FYELLOW}%10s ${FBLUE}<<>> ${FYELLOW}%s${RESET}\n>>> ${FCYAN}%20s ${BRIGHT}${FBLUE}<--> ${FMAGENTA}%s${RESET}\n" \
"$2" "$upstream_ip" "$downstream_ip" "$3" "$4"
}
2020-07-21 02:06:15 +08:00
print_lo() {
2020-04-30 21:10:29 +08:00
printf "${FGREEN}%-20s${RESET}${BRIGHT}${FBLUE}||${FMAGENTA}%24s${RESET} ${BRIGHT}${FBLUE}|| ${RESET}%s\n" \
"$1" "$2" "$3"
}
#################
# PROGRAM BEGIN #
#################
if [ $# -lt 1 ]; then
# Print usage
errmsg \
"Usage: table-output.sh <table type>\n" \
"\n" \
" table types:\n" \
" asn, route, entity, node\n"
2020-04-30 21:10:29 +08:00
fi
2020-07-21 02:06:15 +08:00
arg="$2" # Optional argument
2020-04-30 21:10:29 +08:00
case "$1" in
asn)
2020-05-11 17:33:01 +08:00
(
2020-07-21 02:06:15 +08:00
cd asn
for i in *; do
msg "${i#asn/}\n"
source "$i"
2020-04-30 21:10:29 +08:00
2020-07-21 02:06:15 +08:00
printf "${BRIGHT}${FMAGENTA}%-16s${RESET}| ${BRIGHT}${FYELLOW}%s\n\t>> %s\n" \
"$OWNER" "$NAME" "$DESC"
done
2020-05-11 17:33:01 +08:00
)
2020-04-30 21:10:29 +08:00
;;
2020-04-30 21:42:12 +08:00
route)
for i in route*/*; do
subnet="${i#route*/}"
subnet="${subnet/,/\/}"
2020-04-30 21:10:29 +08:00
source "$i"
case "$TYPE" in
2020-07-21 02:06:15 +08:00
SUBNET) print_subnet "$subnet" "$NAME" "$DESC" ;;
LO) print_lo "$subnet" "$NAME" "$DESC" ;;
*) errmsg "Invalid \$TYPE in $i\n" ;;
2020-04-30 21:10:29 +08:00
esac
done
;;
2020-07-21 02:06:15 +08:00
entity) ;;
2020-05-13 21:18:53 +08:00
node)
for i in node/*; do
node="${i#node/}"
source "$i"
echo -e \
"${BRIGHT}${BBLUE}${FYELLOW}========================================${RESET}"
printf "${BRIGHT}${FYELLOW}%12s${RESET} | ${BRIGHT}${FGREEN}%20s${RESET} | ${FCYAN}%s${RESET}\n" "AS${ASN}" "$node" "$DESC"
for ip in "${IP[@]}"; do
printf "\t%s\n" "$ip"
done
done
;;
2020-07-21 02:06:15 +08:00
*) errmsg "Invalid type\n" ;;
2020-04-30 21:10:29 +08:00
esac
# vim: set tabstop=8:softtabstop=8:shiftwidth=8