-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
91 lines (64 loc) · 2.21 KB
/
install.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
#!/usr/bin/env bash
GO_HOOKS_DIR_NAME=".go-hooks"
GO_HOOKS_DIR=$HOME/$GO_HOOKS_DIR_NAME
GO_HOOKS_CONFIG=$GO_HOOKS_DIR/config.yaml
GO_HOOKS_EXECUTABLE="gohooks"
GO_HOOKS_EXECUTABLE_PATH="/usr/local/bin/$GO_HOOKS_EXECUTABLE"
RELEASE_URL="https://github.com/UsingCoding/gohooks/releases/download/v0.1.3/gohooks"
GIT_HOOKS=("commit-msg" "pre-push")
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
LIGHT_CYAN='\033[1;36m'
CC='\033[0m' # Clear Color
gitHookTemplate() {
HOOK_NAME=$1
cat <<-EOF
#!/usr/bin/env bash
# Code generated by gohooks install script. DO NOT EDIT.
$GO_HOOKS_EXECUTABLE hook $HOOK_NAME "\$@"
EOF
}
configTemplate() {
cat <<-EOF
protectedReposRegExps:
# NOTE: regexp must fully match remote address
# Regexp to check will be chosen by remote name
# If remote url satisfy any of regexp hook applied
# origin:
# Apply for repos from github
# - .*github.com.*
# Apply only for ssh remotes
# - git@+*
# source:
# Apply for repos with specific names
# - .*SpecificName.*
EOF
}
# Installing gohooks binary at $PATH
curl -Ls $RELEASE_URL -o "$GO_HOOKS_EXECUTABLE" || (echo -e "${RED}Failed to download release binary from $RELEASE_URL${CC}" && exit 127)
chmod +x "$GO_HOOKS_EXECUTABLE"
echo -e "${LIGHT_CYAN}$GO_HOOKS_EXECUTABLE: ${GREEN}downloaded${CC}"
if [ ! -d "$GO_HOOKS_DIR" ]; then
echo "Creating $GO_HOOKS_DIR"
mkdir "$GO_HOOKS_DIR" || exit 1
fi
echo -e "${LIGHT_CYAN}Note:${CC} This will rewrite git hooks files"
for GIT_HOOK in "${GIT_HOOKS[@]}"; do
echo "..Set up $GIT_HOOK"
echo "....Creating $GIT_HOOK"
HOOK_PATH="$GO_HOOKS_DIR/$GIT_HOOK"
TEMPLATE=$(gitHookTemplate "$GIT_HOOK")
echo "$TEMPLATE" > "$HOOK_PATH"
echo "....Setup $GIT_HOOK as executable"
chmod u+x "$HOOK_PATH"
done
if [ ! -f "$GO_HOOKS_CONFIG" ]; then
echo -e "${LIGHT_CYAN}Config not found:${CC} creating new one"
configTemplate > "$GO_HOOKS_CONFIG"
fi
echo -e "${LIGHT_CYAN}go-hooks as global git hooks: ${GREEN}installed${CC}"
git config --global core.hooksPath "$GO_HOOKS_DIR"
echo -e "${YELLOW}PLease move $GO_HOOKS_EXECUTABLE to $GO_HOOKS_EXECUTABLE_PATH${CC}"
echo -e "${YELLOW}Run:${CC} mv $GO_HOOKS_EXECUTABLE $GO_HOOKS_EXECUTABLE_PATH"