-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-configure-git.sh
executable file
·218 lines (199 loc) · 7.91 KB
/
10-configure-git.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
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
#!/bin/sh
#-------------------------------------------------------------------------------
# Set up git for current user by:
#
# 1. asking for user details;
# 2. setting global git user;
# 3. setting global git email;
# 4. setting global branch to "main";
# 5. generating an ssh key;
# 6. adding the ssh key to the ssh agent; and
# 7. generating an ssh config file.
#
# N.B.
# This script needs to be run as "sudo".
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imported shared variables.
#-------------------------------------------------------------------------------
. ./linshafun/linshafun.var
#-------------------------------------------------------------------------------
# Imported project specific variables.
#-------------------------------------------------------------------------------
. ./linux-setup.var
#-------------------------------------------------------------------------------
# Imported shared functions.
#-------------------------------------------------------------------------------
. ./linshafun/comments.sh
# . ./linshafun/docker-env-variables.sh
# . ./linshafun/docker-images.sh
# . ./linshafun/docker-services.sh
# . ./linshafun/files-directories.sh
# . ./linshafun/firewall.sh
# . ./linshafun/host-env-variables.sh
# . ./linshafun/host-information.sh
# . ./linshafun/network.sh
. ./linshafun/ownership-permissions.sh
# . ./linshafun/packages.sh
# . ./linshafun/services.sh
. ./linshafun/setup-config.sh
. ./linshafun/setup.sh
. ./linshafun/ssh-keys.sh
. ./linshafun/text.sh
. ./linshafun/user-input.sh
#-------------------------------------------------------------------------------
# Config key and service variables.
#-------------------------------------------------------------------------------
CONFIG_KEY='configuredGit'
SERVICE="$(changeCase "${CONFIG_KEY#'configured'}" "lower")"
#-------------------------------------------------------------------------------
# Set the ssh file variables for the Linux user.
#-------------------------------------------------------------------------------
SSH_CONF="$SSH_DIR/config"
SSH_KEY="$SSH_DIR/github"
#-------------------------------------------------------------------------------
# Request user details to use in global git settings and when generating ssh key
# for Github.
#-------------------------------------------------------------------------------
getGitDetails () {
promptForUserInput 'What global git username do you want to use with git?'
GIT_USERNAME="$(getUserInput)"
promptForUserInput 'What global git email do you want to use with git?'
GIT_EMAIL="$(getUserInput)"
}
#-------------------------------------------------------------------------------
# Set global git username and email, using values from getGitDetails.
#
# N.B.
# As the script is run as "sudo" it is necessary to execute the git commands
# as the "$SUDO_USER", as per:
#
# - https://stackoverflow.com/a/1988255
#-------------------------------------------------------------------------------
setGitDetails () {
echoComment "Setting global git username to $GIT_USERNAME."
su -c "git config --global user.name "$GIT_USERNAME"" "$SUDO_USER"
echoComment "Setting global git email to $GIT_EMAIL"
su -c "git config --global user.email "$GIT_EMAIL"" "$SUDO_USER"
}
#-------------------------------------------------------------------------------
# Set global default branch to "main".
#
# N.B.
# As the script is run as "sudo" it is necessary to execute the git commands
# as the "$SUDO_USER", as per:
#
# - https://stackoverflow.com/a/1988255
#-------------------------------------------------------------------------------
setGitDefaultBranch () {
echoComment 'Setting global default branch to main.'
su -c "git config --global init.defaultBranch main" "$SUDO_USER"
}
#-------------------------------------------------------------------------------
# Start the "ssh-agent" and add the newly generated key to it.
#-------------------------------------------------------------------------------
addSshKeytoAgent () {
echoComment 'Adding the generated key to the ssh-agent.'
echoSeparator
eval "$(ssh-agent -s)"
ssh-add "$SSH_KEY"
echoSeparator
echoComment 'Key added to agent.'
}
#-------------------------------------------------------------------------------
# Generate an ssh config file.
#-------------------------------------------------------------------------------
generateSshConfig () {
echoComment 'Generating ssh config file at:'
echoComment "$SSH_CONF"
cat <<EOF > "$SSH_CONF"
Host github.com
Hostname github.com
IdentityFile ~/.ssh/github
IdentitiesOnly yes
EOF
echoComment 'Config file generated.'
}
#-------------------------------------------------------------------------------
# Get the user to copy public ssh key to Github account.
#-------------------------------------------------------------------------------
getUserToAddKey () {
echoComment 'You must add the contents of ~/.ssh/github.pub to your Github'
echoComment 'account via:'
echoComment 'Settings > Access > SSH and GPG keys'
echoComment 'You will likely need to open a separate command line session to'
echoComment 'copy the contents. We will wait a while you go add the key…'
}
#-------------------------------------------------------------------------------
# Check if the user has added the key to their Github account. Block progress
# until they have added it.
#
# N.B.
# As we are trying to be POSIX compliant, we are using "-eq" and "-o" within
# single brackets as per:
#
# - https://queirozf.com/entries/posix-shell-tests-and-conditionals-examples-and-reference
#-------------------------------------------------------------------------------
checkUserAddedKey () {
sleep 5
promptForUserInput 'Have you added the ssh key to your account (y/n)?'
KEY_ADDED="$(getUserInputYN)"
if [ "$KEY_ADDED" = true ]; then
echoComment 'Key added to Github – we will know later if you fibbed…'
else
echoComment 'You must add your key to Github proceed. Please add it now via:'
echoSeparator
echoComment 'github.com > Settings > Access > SSH and GPG keys'
echoSeparator
echoComment 'You will likely need to open a separate command line session to'
echoComment 'copy the contents of the key.'
checkUserAddedKey
fi
}
#-------------------------------------------------------------------------------
# List git configuration.
#-------------------------------------------------------------------------------
listGitConfig () {
echoComment 'Listing git configuration.'
echoSeparator
git config --list
echoSeparator
}
#-------------------------------------------------------------------------------
# Test ssh connection.
#-------------------------------------------------------------------------------
testGitSsh () {
echoComment 'Testing ssh connection to git:'
echoSeparator
ssh -T [email protected]
echoSeparator
echoComment 'If you saw a success message, you are good to go. If you saw an'
echoComment 'error about permissions, when this script exits you can try:'
echoComment 'ssh -T [email protected]'
echoComment 'If that still does not work, you fibbed about adding your key…'
}
#-------------------------------------------------------------------------------
# Executes the main functions of the script.
#-------------------------------------------------------------------------------
mainScript () {
getGitDetails
setGitDetails
setGitDefaultBranch
generateSshKey "$SSH_KEY" "$GIT_EMAIL"
setOwner "$SUDO_USER" "$SSH_KEY"
setOwner "$SUDO_USER" "$SSH_KEY.pub"
addSshKeytoAgent
generateSshConfig
setPermissions 600 "$SSH_CONF"
setOwner "$SUDO_USER" "$SSH_CONF"
getUserToAddKey
checkUserAddedKey
listGitConfig
testGitSsh
}
#-------------------------------------------------------------------------------
# Run the script.
#-------------------------------------------------------------------------------
initialiseScript "$CONFIG_KEY"
mainScript
finaliseScript "$CONFIG_KEY"