-
Notifications
You must be signed in to change notification settings - Fork 2
/
push.sh
executable file
·125 lines (104 loc) · 1.71 KB
/
push.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
SELF="$0"
VERSION="0.0.1"
NULL=/dev/null
STDIN=0
STDOUT=1
STDERR=2
VERBOSE=0
if [ -t 0 ]; then
ISATTY=1
else
ISATTY=0
fi
throw () {
{
printf "push: error: "
echo "$@"
} >&$STDERR
exit 1
}
version () {
echo $VERSION
}
verbose () {
if [ "1" = "$VERBOSE" ]; then
printf "push: verbose: "
printf "$@"
printf "\n"
fi
}
usage () {
echo "usage: umq push <host> <port> [-hvV] "
if [ "$1" = "1" ]; then
echo
echo "examples:"
echo "$ echo \"ping\" | umq push localhost 3000"
echo
echo "options:"
echo " -v, --verbose show verbose output"
echo " -h, --help display this message"
echo " -V, --version output version"
fi
}
if [ "${1:0:1}" != "-" ]; then
host="$1"
shift
fi
if [ "${1:0:1}" != "-" ]; then
port="$1"
shift
fi
while true; do
arg="$1"
if [ "" = "$1" ]; then
break;
fi
if [ "${arg:0:1}" != "-" ]; then
shift
continue
fi
case $arg in
-v|--verbose)
VERBOSE=1
shift
;;
-h|--help)
usage 1
exit 1
;;
-V|--version)
version
exit 0
;;
*)
{
echo "unknown option \`$arg'"
} >&$STDERR
usage
exit 1
;;
esac
done
verbose "host='%s'" "$host"
verbose "port='%s'" "$port"
if [ -z "$host" ]; then
throw "Missing host"
elif [ -z "$port" ]; then
throw "Missing port"
fi
while read -r line; do
verbose "push: '%s'" "$line"
echo "$line" | {
nc "$host" "$port" | {
while read chunk; do
if [ "" != "$chunk" ]; then
echo "$chunk"
fi
done
} >$NULL
if [ "1" = "$?" ]; then
throw "Failed to connect to '$host:$port'"
fi
};
done