-
Notifications
You must be signed in to change notification settings - Fork 282
/
class-setup
executable file
·257 lines (213 loc) · 7.19 KB
/
class-setup
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
#
# Class Setup
# GLOBALS
NEW_NAME='' # Token owners name
NEW_TOKEN='' # Token owners PAT
NEW_URL='' # Instance URL to GHE
NEW_ORG='' # New instance GHE Organization
SHELL='' # What shell the user is using
RC_FILE='.trainingmanualrc' # RC file to load/pull data
function ValidateJQ() {
# Validate jq installed
VALIDATE_JQ_CMD=$(command -v jq 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed to find [jq] installed on system!"
echo "ERROR:[$VALIDATE_JQ_CMD]"
echo "Please install to system before next run!"
echo "grab it from https://stedolan.github.io/jq/download/"
exit 1
fi
}
function CreateRCFile() {
# Create training RC file
echo "==============================================="
echo "We'll create a file to store all training related configs."
echo "Checking for and creating [$HOME/$RC_FILE]"
# See if file exists
if [ ! -f "$HOME/$RC_FILE" ]; then
echo "failed to find $HOME/$RC_FILE"
echo "Going to create file for you..."
CREATE_FILE=$(touch "$HOME/$RC_FILE" 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed to create file!"
echo "ERROR:[$CREATE_FILE]"
exit 1
fi
# The file already exists
else
echo "The file:[$HOME/$RC_FILE] exists in the system."
echo "Would you like to recreate the file?"
echo "(Y)es or (N)o, followed by [ENTER]"
# Read the input
read -r CREATE_FILE
if [[ "$CREATE_FILE" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
# We have a yes answer
echo "Deleting the file to recreate it..."
DELETE_AND_CREATE_FILE=$(
rm -f "$HOME/$RC_FILE"
touch "$HOME/$RC_FILE" 2>&1
)
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed to delete and recreate file!"
echo "ERROR:[$DELETE_AND_CREATE_FILE]"
exit 1
fi
else
# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"
# Leaving current file in place
echo "Leaving current file in place..."
echo ""
echo "Current Env Vars:"
echo "==============================================="
echo "TOKEN_OWNER=[$TOKEN_OWNER]"
echo "TEACHER_PAT=[NOT DISPLAYED]"
echo "INSTANCE_URL=[$INSTANCE_URL]"
echo "ROOT_URL=[$ROOT_URL]"
echo "CLASS_ORG=[$CLASS_ORG]"
echo "==============================================="
echo ""
echo "NOTE: If any of these appear to be incorrect,"
echo "please destroy the file and restart your shell."
echo ""
# Exit the shell - we're done here
exit 0
fi
fi
echo "==============================================="
echo ""
echo "==============================================="
}
function TokenOwner() {
# Get the token Owner
echo "It looks like we'll need to add the TOKEN_OWNER var"
echo "to your ~/$RC_FILE. Please type the github username"
echo "you'll be using today and we'll add this automatically. Remember, this"
echo "user will need to have full admin access to the teaching organization."
# Read in the input
read -r NEW_NAME
# Write the var to the file
echo "export TOKEN_OWNER='$NEW_NAME'" >>"$HOME/$RC_FILE"
echo "TOKEN_OWNER = $TOKEN_OWNER"
echo "==============================================="
}
function DemoPAT() {
# See if we have teacher PAT
echo "It looks like we need to add the"
echo "TEACHER_PAT var to your ~/$RC_FILE."
echo "Please generate a Personal Access token on the"
echo "account you'll be teaching from and paste it here."
echo "(Note: the token will not be displayed when entered)"
# Read in the token
read -r -s NEW_TOKEN
# Write the var to the file
echo "export TEACHER_PAT='$NEW_TOKEN'" >>"$HOME/$RC_FILE"
echo "TEACHER_PAT=[NOT DISPLAYED]"
echo "==============================================="
}
function InstanceURL() {
# See if we have instance URL
echo "It looks like you need to add the"
echo "INSTANCE_URL var to your ~/$RC_FILE. Please type the"
echo "root endpoint for your github instance."
echo "For example: api.github.com OR github.mycompany.com/api/v3"
# Read the input
read -r NEW_URL
echo "Checking if '$NEW_URL' is accessible... (this may take several moments)"
curl -s -S "$NEW_URL" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -eq 0 ]; then
echo "API endpoint verified"
echo "export INSTANCE_URL='$NEW_URL'" >>"$HOME/$RC_FILE"
else
echo "Can't access your API via that URL, perhaps it needs authentication, checking..."
# Check with Auth
echo "Checking if '$NEW_URL' is accessible with authentication..."
if [[ $(curl -s -S -u "$NEW_NAME:$NEW_TOKEN" "https://$NEW_URL/" | jq .message) = null ]]; then
echo "API endpoint + authentication verified"
echo "export INSTANCE_URL='$NEW_URL'" >>"$HOME/$RC_FILE"
else
echo "Can't make an authenticated request to the API."
exit 1
fi
fi
# Set the vars to empty
ROOT_URL=''
INSTANCE_URL=''
# Check if github.com
if echo "$NEW_URL" | grep -iq "\<api.github.com\>"; then
ROOT_URL="github.com"
INSTANCE_URL="api.github.com"
echo "export ROOT_URL='$ROOT_URL'" >>"$HOME/$RC_FILE"
else
#strip the API part
ROOT_URL=$(echo "$NEW_URL" | sed -E 's/\/api\/v3//')
INSTANCE_URL=$NEW_URL
echo "export ROOT_URL='$ROOT_URL'" >>"$HOME/$RC_FILE"
fi
# Prints
echo "INSTANCE_URL=[$INSTANCE_URL]"
echo "ROOT_URL=[$ROOT_URL]"
echo "==============================================="
}
function ClassOrg() {
# See if we have the Org
echo "It looks like you need to add the CLASS_ORG var"
echo "to your ~/$RC_FILE. Please type the organization name"
echo "used for github training on your instance."
echo "For example: githubschool"
# Read the input
read -r NEW_ORG
# Validate access
echo "Checking if $NEW_ORG is accessible via the API..."
if [[ $(curl -s -S -u "$NEW_NAME:$NEW_TOKEN" "https://$NEW_URL/orgs/$NEW_ORG" | jq .message) = null ]]; then
echo "That organization was verified."
echo "export CLASS_ORG='$NEW_ORG'" >>"$HOME/$RC_FILE"
else
echo "Can't access the organization via the API."
exit 1
fi
# Prints
echo "CLASS_ORG=[$CLASS_ORG]"
echo "==============================================="
}
function ShellCheck() {
# Check the users shell
echo "What kind of shell are you using?"
echo "acceptable options: bash, zsh"
# Read the input
read -r SHELL
echo "Adding to your .${SHELL}rc..."
# shellcheck disable=SC2028
echo "# added by github/training-manual class setup" >>"$HOME/.${SHELL}rc"
echo "test -f \"$HOME/$RC_FILE\" && source \"$HOME/$RC_FILE\"" >>"$HOME/.${SHELL}rc"
# Prints
echo "ATTENTION: You must restart your open terminal sessions"
echo "for changes to take effect."
}
# Validate jq
ValidateJQ
# Create rc file
CreateRCFile
# Get the token owner
TokenOwner
# Get the teacher PAT
DemoPAT
# Get the instance URL
InstanceURL
# Get the class org
ClassOrg
# Check the users shell
ShellCheck