-
Notifications
You must be signed in to change notification settings - Fork 6
/
viv
executable file
·97 lines (77 loc) · 2.25 KB
/
viv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
install_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
vivify_server="$install_dir/vivify-server"
print_usage() {
cat <<EOF
usage: viv target [target ...]
View file/directory in your browser with Vivify.
arguments:
target Path to file or directory to view. For Markdown, you can
suffix with :n to scroll to the content at line n in the
source file
options:
--help show this help message and exit
--version show version information and exit
EOF
}
print_bug_report() {
cat <<EOF
Fatal: "$vivify_server" crashed.
Please use the link below to submit a bug report.
The bug report template will help you provide the necessary information and
maybe even find a solution yourself.
https://github.com/jannis-baum/Vivify/issues/new?labels=type%3Abug&template=bug-report.md
EOF
}
print_server_file_error() {
cat <<EOF
Fatal: "$vivify_server" $1.
Please make sure that the "vivify-server" binary is located in the same
directory as the "viv" script and that it is executable.
EOF
}
if [ "$#" -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]; then
print_usage
exit 1
fi
if ! test -f "$vivify_server"; then
print_server_file_error "not found"
exit 1
fi
if ! test -x "$vivify_server"; then
print_server_file_error "not executable"
exit 1
fi
output=$(mktemp)
cleanup() {
rm -f "$output"
}
trap cleanup EXIT
nohup "$vivify_server" "$@" > "$output" 2> /dev/null &
server_pid=$!
monitor_server() {
while true; do
# check if startup was completed successfully, if so we can exit
test -f "$output" || exit 0
grep --quiet "STARTUP COMPLETE" "$output" && exit 0
# server process ended otherwise
if ! kill -0 $server_pid 2>/dev/null; then
# if not, the startup failed
print_bug_report
# kill tail from while loop below
pkill -P $$ tail
exit 1
fi
sleep 0.3
done
}
monitor_server &
# print stdout of vivify-server until STARTUP COMPLETE is found
tail -f "$output" | while read -r line; do
# server finished starting
if echo "$line" | grep --quiet "STARTUP COMPLETE"; then
pkill -P $$ tail
break
fi
echo "$line"
done