forked from samrocketman/gitlab-mirrors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_mirror.sh
executable file
·173 lines (153 loc) · 4.26 KB
/
delete_mirror.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
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
#!/bin/bash
#Created by Sam Gleske
#MIT License
#Created Thu Sep 12 16:04:35 EDT 2013
#bash option stop on first error
set -e
#Include all user options and dependencies
git_mirrors_dir="${0%/*}"
[ -f "${git_mirrors_dir}/config.sh" ] && . "${git_mirrors_dir}/config.sh"
. "${git_mirrors_dir}/lib/VERSION"
. "${git_mirrors_dir}/lib/functions.sh"
if [ ! -f "${git_mirrors_dir}/config.sh" ];then
red_echo "config.sh missing! Copy and customize from config.sh.SAMPLE. Aborting." 1>&2
exit 1
fi
#export env vars for python script
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify
cd "${git_mirrors_dir}"
PROGNAME="${0##*/}"
PROGVERSION="${VERSION}"
#Default script options
project_name=""
quiet=false
no_delete=false
#
# ARGUMENT HANDLING
#
usage()
{
cat <<EOF
${PROGNAME} ${PROGVERSION} - MIT License by Sam Gleske
USAGE:
${PROGNAME} --delete PROJECT
DESCRIPTION:
This program deletes a project so that it will no longer be mirrored.
-h,--help Show help
-v,--version Show program version
-d,--delete PROJECT
Deletes a project so it is no longer mirrored.
-n,--no-delete PROJECT
Only deletes the local project but not the remote.
This option is forced for projects with
--no-create set when the mirror was added.
-q,--quiet Suppress user confirmation messages.
EOF
}
#Short options are one letter. If an argument follows a short opt then put a colon (:) after it
SHORTOPTS="hvd:n:q"
LONGOPTS="help,version,delete:,no-delete:,quiet"
ARGS=$(getopt -s bash --options "${SHORTOPTS}" --longoptions "${LONGOPTS}" --name "${PROGNAME}" -- "$@")
eval set -- "$ARGS"
while true; do
case $1 in
-h|--help)
usage
exit 1
;;
-v|--version)
echo "${PROGNAME} ${PROGVERSION}"
exit 1
;;
-d|--delete)
project_name="${2}"
shift 2
;;
-n|--no-delete)
project_name="${2}"
no_delete=true
shift 2
;;
-q|--quiet)
quiet=true
shift
;;
--)
shift
break
;;
*)
shift
;;
esac
done
#
# Program functions
#
function preflight() {
STATUS=0
if [ -z "${project_name}" ];then
red_echo -n "Must specify "
yellow_echo -n "--delete"
red_echo -n " or "
yellow_echo -n "--no-delete"
red_echo " option."
STATUS=1
elif [ ! -e "${repo_dir}/${gitlab_namespace}/${project_name}" ];then
yellow_echo -n "${repo_dir}/${gitlab_namespace}/${project_name}" 1>&2
echo " does not exist."
STATUS=1
fi
return ${STATUS}
}
#
# Main execution
#
#Run a preflight check on options for compatibility.
if ! preflight 1>&2;then
echo "Command aborted due to previous errors." 1>&2
exit 1
fi
if ! ${quiet};then
echo -n "Will DELETE "
red_echo "${repo_dir}/${gitlab_namespace}/${project_name}"
echo
red_echo "This action CANNOT be undone!"
echo
echo -n "Are you sure you wish to delete project "
yellow_echo -n "${gitlab_namespace}/${project_name}"
echo -n "? (y/N): "
read ans
echo
#convert upper case to lower case
ans="$(echo "${ans}" | tr '[A-Z]' '[a-z]')"
if [ ! "${ans}" = "y" -a ! "${ans}" = "yes" ];then
echo "User aborted operation." 1>&2
exit 1
fi
fi
pushd "${repo_dir}/${gitlab_namespace}/${project_name}" &> /dev/null
if git config --get gitlabmirrors.nocreate &> /dev/null && [ "$(git config --get gitlabmirrors.nocreate)" = "true" ];then
no_delete=true
fi
if git config --get gitlabmirrors.noremote &> /dev/null && [ "$(git config --get gitlabmirrors.noremote)" = "true" ];then
no_remote_set=true
fi
popd &> /dev/null
rm -rf "${repo_dir}/${gitlab_namespace}/${project_name}"
green_echo -n "DELETED" 1>&2
echo " ${repo_dir}/${gitlab_namespace}/${project_name}" 1>&2
if ! ${no_remote_set};then
if ! ${no_delete};then
if ! python lib/manage_gitlab_project.py --delete "${project_name}";then
red_echo "There was an unknown issue with manage_gitlab_project.py" 1>&2
exit 1
fi
green_echo -n "DELETED" 1>&2
echo " ${gitlab_namespace}/${project_name} from GitLab" 1>&2
else
echo 1>&2
yellow_echo -n "**NOTE**:" 1>&2
echo " You must log into the GitLab web interface in order to delete the project from GitLab!" 1>&2
fi
fi