|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to pretty print the output of the github-mcp-server |
| 4 | +# log. |
| 5 | +# |
| 6 | +# It uses colored output when running on a terminal. |
| 7 | + |
| 8 | +# show script help |
| 9 | +show_help() { |
| 10 | + cat<<EOF |
| 11 | +Usage:$(basename"$0") [file] |
| 12 | +
|
| 13 | +If [file] is provided, input is read from that file. |
| 14 | +If no argument is given, input is read from stdin. |
| 15 | +
|
| 16 | +Options: |
| 17 | + -h, --help Show this help message and exit |
| 18 | +EOF |
| 19 | +} |
| 20 | + |
| 21 | +# choose color for stdin or stdout if we are printing to |
| 22 | +# an actual terminal |
| 23 | +color(){ |
| 24 | + io="$1" |
| 25 | +if [["$io"=="stdin" ]];then |
| 26 | + color="\033[0;32m"# green |
| 27 | +else |
| 28 | + color="\033[0;36m"# cyan |
| 29 | +fi |
| 30 | +if [!$is_terminal="1" ];then |
| 31 | + color="" |
| 32 | +fi |
| 33 | +echo -e"${color}[$io]" |
| 34 | +} |
| 35 | + |
| 36 | +# reset code if we are printing to an actual terminal |
| 37 | +reset(){ |
| 38 | +if [!$is_terminal="1" ];then |
| 39 | +return |
| 40 | +fi |
| 41 | +echo -e"\033[0m" |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +# Handle -h or --help |
| 46 | +if [["$1"=="-h"||"$1"=="--help" ]];then |
| 47 | + show_help |
| 48 | +exit 0 |
| 49 | +fi |
| 50 | + |
| 51 | +# Determine input source |
| 52 | +if [[-n"$1" ]];then |
| 53 | +if [[!-r"$1" ]];then |
| 54 | +echo"Error: File '$1' not found or not readable.">&2 |
| 55 | +exit 1 |
| 56 | +fi |
| 57 | + input="$1" |
| 58 | +else |
| 59 | + input="/dev/stdin" |
| 60 | +fi |
| 61 | + |
| 62 | +# check if we are in a terminal for showing colors |
| 63 | +iftest -t 1;then |
| 64 | + is_terminal="1" |
| 65 | +else |
| 66 | + is_terminal="0" |
| 67 | +fi |
| 68 | + |
| 69 | +# Processs each log line, print whether is stdin or stdout, using different |
| 70 | +# colors if we output to a terminal, and pretty print json data using jq |
| 71 | +sed -nE's/^.*\[(stdin|stdout)\]:.* ([0-9]+) bytes: (.*)\\n"$/\1 \2 \3/p'$input| |
| 72 | +whileread -r io bytes json;do |
| 73 | +# Unescape the JSON string safely |
| 74 | + unescaped=$(echo"$json"| awk'{ print "echo -e \"" $0 "\" | jq ." }'| bash) |
| 75 | +echo"$(color$io)($bytes bytes):$(reset)" |
| 76 | +echo"$unescaped"| jq. |
| 77 | +echo |
| 78 | +done |