forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcleanup.sh
executable file
·118 lines (98 loc) · 4.13 KB
/
cleanup.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
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Precommit Hook for K8s Manifest Validation pre-CI/CD pipeline.
# Janine Bariuan and Thomas Desrosiers
#########################################################
####### STEP 0 - DEPENDENCY INSTALLATION PRE-WORK #######
#########################################################
# Unset CDPATH to restore default cd behavior. An exported CDPATH can
# cause cd to output the current directory to STDOUT.
unset CDPATH
# Send errors to STDERR
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
exit 1
}
#Define location of dependencies, consistent across files
readonly INSTALL_DIR=".oss_dependencies"
#######################################
# Emulates `readlink -f` behavior, as this is not available by default on MacOS.
# Helps rationalize symlinks or canonical files to the path it represents.
# See: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
# Arguments:
# TARGET_FILE ($1)
# Outputs:
# Writes full path to stdout
#######################################
function readlink_f {
TARGET_FILE=$1
cd "$(dirname "$TARGET_FILE")" || err "Could not cd in realink_f"
TARGET_FILE=$(basename "$TARGET_FILE")
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]; do
TARGET_FILE=$(readlink "$TARGET_FILE")
cd "$(dirname "$TARGET_FILE")" || err "Could not cd in readlink_f"
TARGET_FILE=$(readlink "$TARGET_FILE")
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=$(pwd -P)
RESULT=$PHYS_DIR/$TARGET_FILE
echo "$RESULT"
}
# Prompt user if they really want to continue with the deletion
echo "* This script will delete Pre-Validate Dependencies and remove .git/hooks/
pre-commit.sh."
read -r -p "Are you sure you want to continue? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
exit 0
fi
#############################################
###### STEP 1 - Remove Pre-Commit Hook ######
#############################################
# Determine if user has run this code from the correct directory
# Disable unused variable error.
# shellcheck disable=SC2034
cwd=$(printf "%q\\n" "$(pwd)")
full_path="$(readlink_f "$(cwd)")"
where=$(printf "%q\\n" "$full_path")
if [[ ! -f "$where"/.git/hooks/pre-commit ]]; then
err "It seems this code is either not being run in a git repository, or being run in the wrong place and can't access your pre-commit hook. Please Make sure you run this command in your project root."
fi
# Rename Pre-Commit Hook with .sample
mv "$where"/.git/hooks/pre-commit "$where"/.git/hooks/pre-commit.sample
#############################################################
###### STEP 2 - Delete Necessary Files and Directories ######
#############################################################
# Delete Dependencies (Folder marked by $INSTALL_DIR)
rm -rf $INSTALL_DIR
# Delete locally downloaded ConstraintTemplates
rm -rf constraints_and_templates
###############################################
###### STEP 2 - Remove Remaining Scripts ######
###############################################
rm -f validate.sh
rm -f setup.sh
rm -f cleanup.sh
#############################################
########### STEP 3 - Finish and UX ##########
#############################################
# Finish
printf '\033[0;32mPre-Validate has been removed. Thank you for trying it out!\\n'
echo "Visit https://github.com/GoogleCloudPlatform/professional-services/tree/
main/examples/left-shift-validation-pre-commit-hook"
printf 'for more information.\033[0m\\n'