This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch-listener
More file actions
executable file
·190 lines (151 loc) · 6.49 KB
/
Copy pathbranch-listener
File metadata and controls
executable file
·190 lines (151 loc) · 6.49 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
INSTALLED_BRANCH_LISTENER_DIR="$HOME/.config/branch-listener"
function check_perfomance {
if [ -z "$BRANCH_LISTENER_MAIN_DIR" ] || [ -d "$BRANCH_LISTENER_MAIN_DIR" ]; then
if [ -d "$INSTALLED_BRANCH_LISTENER_DIR" ]; then
if echo "${PWD##*/}" | grep -q "branch-listener" && [ "$1" != "uninstall" ]; then
BRANCH_LISTENER_MAIN_DIR="."
else
BRANCH_LISTENER_MAIN_DIR=$INSTALLED_BRANCH_LISTENER_DIR
fi
else
BRANCH_LISTENER_MAIN_DIR="."
fi
else
BRANCH_LISTENER_MAIN_DIR="."
fi
if [ -n "$BRANCH_LISTENER_MAIN_DIR" ] && [ -f "$BRANCH_LISTENER_MAIN_DIR"/configs/server/config.json ]; then
BRANCH_LISTENER_PORT=$(runTool "port" | tail -1)
fi
}
function replaceUrlVar {
line=$1
echo "trying to remove $line"
new_file=$(grep -v "$line" "$BRANCH_LISTENER_SHELL_RC")
echo "$new_file" > "$BRANCH_LISTENER_SHELL_RC"
}
function kill_listener {
echo "checking is listener alive..."
if [ -n "$BRANCH_LISTENER_PORT" ] && [ "$(lsof -t -i:$BRANCH_LISTENER_PORT -sTCP:LISTEN)" ]; then
kill -9 "$(lsof -t -i:$BRANCH_LISTENER_PORT -sTCP:LISTEN)"
echo "listener has been killed"
else
echo "listener is not alive at port <$BRANCH_LISTENER_PORT>"
fi
}
function restart_listener {
echo "checking is listener alive..."
if [ -n "$BRANCH_LISTENER_PORT" ] && [ "$(lsof -t -i:$BRANCH_LISTENER_PORT -sTCP:LISTEN)" ]; then
curl localhost:"$BRANCH_LISTENER_PORT"/restart
echo "listener has been restarted"
else
echo "listener is not alive at port <$BRANCH_LISTENER_PORT>"
fi
}
function uninstall_listener {
if ! [ -d "$INSTALLED_BRANCH_LISTENER_DIR" ]; then
echo "branch-listener is not installed."
# exit 1
fi
if [ -z "$BRANCH_LISTENER_SHELL_RC" ]; then
echo "cannot find registered shell rc, trying to defind..."
if echo "$SHELL" | grep -q 'zsh'; then
BRANCH_LISTENER_SHELL_RC="$HOME/.zshrc"
elif echo "$SHELL" | grep -q 'bash'; then
BRANCH_LISTENER_SHELL_RC="$HOME/.bashrc"
else
echo "unsupportable shell! exit"
exit 1
fi
echo "defined $BRANCH_LISTENER_SHELL_RC"
fi
if cat "$BRANCH_LISTENER_SHELL_RC" | grep -q "export BRANCH_LISTENER_SHELL_RC="; then
replaceUrlVar "$BRANCH_LISTENER_SHELL_RC"
fi
if cat "$BRANCH_LISTENER_SHELL_RC" | grep -q "branch-listener run"; then
sed -i '/branch-listener run/d' "$BRANCH_LISTENER_SHELL_RC"
fi
BRANCH_LISTENER_MAIN_DIR=$INSTALLED_BRANCH_LISTENER_DIR
echo "checking for BRANCH_LISTENER_MAIN_DIR: $BRANCH_LISTENER_MAIN_DIR"
if [ "$BRANCH_LISTENER_MAIN_DIR" != "." ]; then
if cat "$BRANCH_LISTENER_SHELL_RC" | grep -q "export BRANCH_LISTENER_MAIN_DIR="; then
replaceUrlVar "$BRANCH_LISTENER_MAIN_DIR"
fi
if cat "$BRANCH_LISTENER_SHELL_RC" | grep -q 'export PATH=$PATH:'"$BRANCH_LISTENER_MAIN_DIR"; then
replaceUrlVar "\$PATH:$BRANCH_LISTENER_MAIN_DIR" #"export PATH=\$PATH:"
fi
echo "removing $BRANCH_LISTENER_MAIN_DIR"
rm -rf "$BRANCH_LISTENER_MAIN_DIR"
fi
unset BRANCH_LISTENER_MAIN_DIR
unset BRANCH_LISTENER_SHELL_RC
kill_listener false
}
function status_listener {
echo "$BRANCH_LISTENER_PORT"
if [ "$(lsof -t -i:$BRANCH_LISTENER_PORT -sTCP:LISTEN)" ]; then
echo "branch-listener is alive at $BRANCH_LISTENER_PORT" false
else
echo "branch-listener is not alive"
fi
}
function runTool {
if [ -n "$BRANCH_LISTENER_MAIN_DIR" ]; then
local list_path="$BRANCH_LISTENER_MAIN_DIR/dist/src/tools/runTool.js"
elif echo "${PWD##*/}" | grep -q "branch-listener"; then
local list_path="./dist/src/tools/runTool.js"
else
echo "uknown current working directory! please go to branch-listener repository directory!"
exit 1
fi
echo "$list_path" "$1" "$2" "$3" "$4" "$5"
node "$list_path" "$1" "$2" "$3" "$4" "$5"
}
function help_usage {
echo "Usage: branch-listener [setup] [run] [add] [start]
[restart] [stop] [remove]
[list] [edit] [kill] [status]
[uninstall] [help] <args>
setup Setups branch-listener configs. No args needed;
run Runs Branch-listener server. No args needed;
add Adds listener for specified CVS. Second arg is name of cvs;
start Starts listeners. If args was not specified - runs all listeners,
to start specified listener you have to write cvs name of listener
as the first argument and id of the config of listener as the second;
restart Restarts branch-listener server. No args needed;
stop Stops listeners. The arguments works as well as for start command;
remove Removes listeners. The arguments works as well as for start command;
list Shows table with listeners information.
If args was not specified - shows all listeners tables.
To show specified cvs listeners you need to specify cvs name as the second arg;
edit Edits listener configs.
Example: branch-listener edit gitlab 0 token ********************;
You need to restart the branch-listener to get you changes to work as well;
kill Kills branch-listener.
Stops the branch-listener server and all listeners. No args needed;
status Outputs status of branch-listener's server. No args needed;
uninstall Uninstalls branch-listener and removes all configs and logs. No args needed;
help Shows this page. No args needed;
"
exit 0
}
function narrowing() {
tool_name="$1"
cvs_name="$2"
id="$3"
key="$4"
value="$5"
if [ "$tool_name" = "kill" ]; then
kill_listener
elif [ "$tool_name" = "uninstall" ]; then
uninstall_listener
elif [ "$tool_name" = "status" ]; then
status_listener
elif [ "$tool_name" = "help" ]; then
help_usage
else
runTool "$tool_name" "$cvs_name" "$id" "$key" "$value"
fi
}
check_perfomance "$1"
narrowing "$1" "$2" "$3" "$4" "$5"