-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
305 lines (271 loc) · 10.4 KB
/
.functions
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/zsh
function log_ok { print -P "%F{green}$1%f"; }
function log_warn { print -P "%F{yellow}$1%f"; }
function log_info { print -P "%F{cyan}$1%f"; }
function log_err { print -P "%F{red}$1%f"; }
# Updates Mac OSX software. Checks for updates, then requires
# user confirmation to install any updates
function update_mac_osx_software {
log_ok "💻 Updating Mac OSX software..."
log_info " - running $(log_warn '\"softwareupdate -l\"')"
local SOFTWARE_STATUS=$(softwareupdate -l 2>&1)
if echo $SOFTWARE_STATUS | grep -q 'No new software available.'; then
log_info " - no new software to install"
else
echo $SOFTWARE_STATUS
log_ok "\nupdate all (may cause reboot)? %F{blue}(y/n)%f"
read -sk1
if [[ $REPLY == "y" ]] then
sudo softwareupdate -ia --restart
log_info " - software updated"
else
log_warn " - skipped softwate updates"
fi
fi
}
# Installs homebrew with autocomplete and disables analytics
function install_homebrew {
log_ok "🍺 Installing Hombrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> $HOME/.zprofile
# Add autocomplete
echo 'FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH' >> $HOME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
# Disable analytics
brew analytics off
log_info " - Homebrew installed"
}
# Updates and upgrades all installed homebrew modules
function update_homebrew {
log_ok "🍺 Updating Homebrew..."
log_info " - running $(log_warn '\"brew update\"')"
brew update
log_info " - running $(log_warn '\"brew doctor\"')"
brew doctor
log_info " - running $(log_warn '\"brew cleanup\"')"
brew cleanup
log_info " - running $(log_warn '\"brew upgrade\"')"
brew upgrade
log_info " - Homebrew updated"
}
# Updates the homebrew bundle apps, install apps that are missing and removing apps that
# are not specified in the Brewfile (requires user confirmation)
function update_homebrew_bundle {
log_ok "🍺 Updating brew bundle for Brewfile..."
log_info " - running $(log_warn '\"brew bundle install\"')"
brew bundle install
log_info " - running $(log_warn '\"brew bundle cleanup\"')"
if [[ $(brew bundle cleanup | wc -c) -ne 0 ]]; then
brew bundle cleanup
log_ok "\nremove? %F{blue}(y/n)%f"
read -sk1
if [[ $REPLY == "y" ]] then
brew bundle cleanup --force
log_info " - extra homebrew apps removed"
else
log_warn " - skipped removing untracked homwbrew apps"
fi
else
log_info " - bundle up to date"
fi
}
# Installs VS Code extensions by parsing the .vscode_extenstions.txt file line by line
# Skips any extensions that are already installed, doesn't remove extensions that are not on the list
function update_vscode_exts {
log_ok "🖇️ Updating VS Code extensions..."
local VSCODE_LIST=$(code --list-extensions)
cat vscode/vscode_extensions.txt | while read line ; do
# Determine the ext name by removing any comments and whitespace at the end of the line
local VSCODE_EXT=$(echo $line | cut -d '#' -f1 | xargs)
if ! echo $VSCODE_LIST | grep -q $VSCODE_EXT; then
code --install-extension $VSCODE_EXT
log_info " - $VSCODE_EXT installed"
fi
done
log_info " - extenstions up to date"
}
# Installs the latest version of node globally via nodeenv
function upgrade_node {
local LATEST=$(nodenv install -l | grep "^[0-9]" | tail -n1)
log_ok "🧊 Upgrading Node to $LATEST..."
if nodenv global | grep -q $LATEST; then
log_info " - version already installed"
else
log_info " - uprading from $(nodenv global) to $LATEST"
log_info " - running $(log_warn "\"nodenv install $LATEST\"")"
nodenv install $LATEST
log_info " - running $(log_warn "\"nodenv global $LATEST\"")"
nodenv global $LATEST
nodenv rehash
eval "$(nodenv init -)"
log_info " - node $LATEST installed"
fi
}
# Installs the latest version of ruby globally via rbenv
function upgrade_ruby {
local LATEST=$(rbenv install -l 2>&1 | grep "^[0-9]" | tail -n1)
log_ok "🧊 Upgrading Ruby to $LATEST..."
if rbenv global | grep -q $LATEST; then
log_info " - version already installed"
else
log_info " - uprading from $(rbenv global) to $LATEST"
log_info " - running $(log_warn "\"rbenv install ${LATEST}\"")"
rbenv install $LATEST
log_info " - running $(log_warn "\"rbenv global ${LATEST}\"")"
rbenv global $LATEST
rbenv rehash
eval "$(rbenv init -)"
log_info " - ruby $LATEST installed"
fi
}
# Installs the latest version of terraform globally via tfenv
function upgrade_terraform {
log_ok "🧊 Upgrading Terrafrom to latest..."
LATEST=$(tfenv install latest)
if echo $LATEST | grep "is already installed" > /dev/null; then
log_info " - $LATEST"
else
tfenv use latest
fi
}
# Generates an SSH key for authentication with Github
function generate_ssh_key_for_github {
log_ok "🔑 Generating SSH key for Github..."
local GITHUB_EMAIL=$(git config --global --get user.email)
if [[ -z "${GITHUB_EMAIL}" ]]; then
log_err "❌ Unable to setup Github, no email setup in .gitconfig"
exit 1
fi
log_ok "🔑 Generating SSH key (ed25519, 256 bit passphrase)..."
local PASSPHRASE=$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64 | sed 's/=//g')
ssh-keygen -t ed25519 -C "${GITHUB_EMAIL}" -f ~/.ssh/id_ed25519 -N "${PASSPHRASE}"
log_warn '\n⚠️ ADD THIS PASSPHRASE TO 1PASSWORD NOW! (copied to clipboard)'
log_warn '+-----------------------------------------------+'
log_warn '| |'
log_warn "| ${PASSPHRASE} |"
log_warn '| |'
log_warn '+-----------------------------------------------+'
echo $PASSPHRASE | pbcopy
read "?Press enter once passphase has been saved"
log_ok "SSH key generated"
pbcopy <~/.ssh/id_ed25519.pub
log_warn '⚠️ This public key has been copied to your clipboard: '
cat ~/.ssh/id_ed25519.pub
log_warn '\n⚠️ To access Github, follow %F{cyan}https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account%f'
read "?Press enter when the public key has been configured in Github"
log_ok "🔑 Adding SSH key to agent..."
eval "$(ssh-agent -s)"
log_warn "⚠️ Use passphase (copied to clipboard): ${PASSPHRASE}"
echo $PASSPHRASE | pbcopy
ssh-add -K ~/.ssh/id_ed25519
log_ok "🧪 Testing Github SSH connection..."
if ssh -T git@github.com | grep "You've successfully authenticated"; then
log_err '❌ Failed to connect to github'
exit 1
fi
log_ok "✅ Github SSH key configured"
}
# Generates a GPG key for verification with Github
function generate_gpg_key_for_github {
log_ok "🔑 Generating GPG key..."
local GITHUB_NAME=$(git config --global --get user.name)
local GITHUB_EMAIL=$(git config --global --get user.email)
local USER_ID=$(echo "${GITHUB_NAME} <${GITHUB_EMAIL}>")
local PASSPHRASE=$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64 | sed 's/=//g')
gpg --quick-generate-key --yes --batch \
--passphrase "${PASSPHRASE}" \
"${GITHUB_NAME} <${GITHUB_EMAIL}>" \
default default 0
log_warn '\n⚠️ ADD THIS GPG PASSPHRASE TO 1PASSWORD NOW! (copied to clipboard)'
log_warn '+-----------------------------------------------+'
log_warn '| |'
log_warn "| ${PASSPHRASE} |"
log_warn '| |'
log_warn '+-----------------------------------------------+'
echo $PASSPHRASE | pbcopy
log_info "Press any key once passphase has been saved"
read -k1 -s
local KEY_ID=$(gpg --list-keys --keyid-format=long $USER_ID | sed -n 1p | awk '{print $2}' | awk -F "/" '{print $2}')
local PUBLIC_KEY=$(gpg --armor --export $USER_ID)
log_info "GPG Public Key (copied to clipboard)"
echo $PUBLIC_KEY | pbcopy
echo $PUBLIC_KEY
log_warn '\n⚠️ To authenticate Github, follow %F{cyan}https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification/adding-a-new-gpg-key-to-your-github-account#adding-a-gpg-key%f'
log_info "Press any key when the public key has been configured in Github"
read -k1 -s
log_warn "Writing signing key $KEY_ID to .env in dotfiles"
echo "export GIT_SIGNING_KEY=\"$KEY_ID\"" >> .env
log_info "Please remember to check 'Save to keychain' when prompted for GPG passphrase on first signed commit (via commandline)"
log_ok "✅ Github GPG key configured"
}
# Installs Oh My Zsh using the install script
function install_oh_my_zsh {
log_ok "😇 Installing Oh My Zsh installed"
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh
log_ok "✅ Oh My Zsh installed"
}
# Initialize powerline bar in terminal
function install_powerline {
log_ok "⚙️ Installing Powerline..."
pip3 install powerline-status
pip3 install powerline-gitstatus
# clone and install the fonts required
git clone https://github.com/powerline/fonts.git --depth=1
cd fonts
./install.sh
# clean-up fonts once installed
cd ..
rm -rf fonts
}
# Generic function that will compare the source file to the target file. If they are
# different allow the user to see a diff and decide to overwrite
function update_config_file {
local SOURCE=$1
local TARGET=$2
log_ok "⚙️ Updating $SOURCE"
if [[ ! -e "$TARGET" ]]; then
log_info " - created file"
cp -a $SOURCE $TARGET
return 0
fi
if [[ $(git --no-pager diff --shortstat $TARGET $SOURCE | wc -c) -ne 0 ]]; then
log_info " - updates detected"
log_warn "view diff? (y/n)"
read -sk1
if [[ $REPLY == "y" ]] then
git diff $TARGET $SOURCE
fi
log_warn "overwrite? (y/n)"
read -sk1
if [[ $REPLY == "y" ]] then
rm -rf $TARGET
cp -a $SOURCE $TARGET
log_info " - updates applied"
return 0
fi
log_info " - updates ignored"
else
log_info " - no changes detected"
fi
return 1
}
# Updates git config by using user information in .env
function update_git_config {
# Make a copy of .gitconfig
cp .gitconfig .gitconfig_with_user
# Update file with user info
git config --file .gitconfig_with_user user.name $GIT_NAME
git config --file .gitconfig_with_user user.email $GIT_EMAIL
git config --file .gitconfig_with_user user.username $GIT_USERNAME
git config --file .gitconfig_with_user user.signingkey $GIT_SIGNING_KEY
# Use the file with user info to update the config
update_config_file .gitconfig_with_user ~/.gitconfig
# Remove the file with user info
rm .gitconfig_with_user
}
# Restarts the GPG agent so new config is picked up
function restart_gpg_agent {
log_info " - restarting gpg agent"
gpg-connect-agent reloadagent /bye
}