Skip to content

Commit adb8c3b

Browse files
Latest
1 parent b81e640 commit adb8c3b

File tree

3 files changed

+195
-141
lines changed

3 files changed

+195
-141
lines changed

scripts/cloudflared-ssh.sh

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -179,66 +179,84 @@ ensureBasicDeps() {
179179
}
180180
ensureBasicDeps
181181

182-
# @description This function ensures Homebrew is installed and available in the `PATH`. It handles the installation of Homebrew on both **Linux and macOS**.
183-
# It will attempt to bypass sudo password entry if it detects that it can do so. The function also has some error handling in regards to various
184-
# directories falling out of the correct ownership and permission states. Finally, it loads Homebrew into the active profile (allowing other parts of the script
185-
# to use the `brew` command).
186-
#
187-
# With Homebrew installed and available, the script finishes by installing the `gcc` Homebrew package which is a very common dependency.
188-
ensureHomebrew() {
182+
### Ensure Homebrew is loaded
183+
loadHomebrew() {
189184
if ! command -v brew > /dev/null; then
190-
if [ -d /home/linuxbrew/.linuxbrew/bin ]; then
191-
logg info "Sourcing from /home/linuxbrew/.linuxbrew/bin/brew" && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
192-
if ! command -v brew > /dev/null; then
193-
logg error "The /home/linuxbrew/.linuxbrew directory exists but something is not right. Try removing it and running the script again." && exit 1
194-
fi
195-
elif [ -d "$HOME/.linuxbrew" ]; then
196-
logg info "Sourcing from $HOME/.linuxbrew/bin/brew" && eval "$($HOME/.linuxbrew/bin/brew shellenv)"
197-
if ! command -v brew > /dev/null; then
198-
logg error "The $HOME/.linuxbrew directory exists but something is not right. Try removing it and running the script again." && exit 1
199-
fi
185+
if [ -f /usr/local/bin/brew ]; then
186+
logg info "Using /usr/local/bin/brew" && eval "$(/usr/local/bin/brew shellenv)"
187+
if [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" ]; then
188+
logg info "Using ${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" && eval "$("${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" shellenv)"
189+
if [ -d "$HOME/.linuxbrew" ]; then
190+
logg info "Using $HOME/.linuxbrew/bin/brew" && eval "$("$HOME/.linuxbrew/bin/brew" shellenv)"
191+
elif [ -d "/home/linuxbrew/.linuxbrew" ]; then
192+
logg info 'Using /home/linuxbrew/.linuxbrew/bin/brew' && eval "(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
200193
else
201-
### Installs Homebrew and addresses a couple potential issues
202-
if command -v sudo > /dev/null && sudo -n true; then
203-
logg info "Installing Homebrew"
204-
echo | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
205-
else
206-
logg info "Homebrew is not installed. The script will attempt to install Homebrew and you might be prompted for your password."
207-
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
208-
if [ -n "$BREW_EXIT_CODE" ]; then
209-
if command -v brew > /dev/null; then
210-
logg warn "Homebrew was installed but part of the installation failed. Trying a few things to fix the installation.."
211-
BREW_DIRS="share/man share/doc share/zsh/site-functions etc/bash_completion.d"
212-
for BREW_DIR in $BREW_DIRS; do
213-
if [ -d "$(brew --prefix)/$BREW_DIR" ]; then
214-
logg info "Chowning $(brew --prefix)/$BREW_DIR" && sudo chown -R "$(whoami)" "$(brew --prefix)/$BREW_DIR"
215-
fi
216-
done
217-
logg info "Running brew update --force --quiet" && brew update --force --quiet && logg success "Successfully ran brew update --force --quiet"
218-
fi
219-
fi
220-
fi
194+
logg info 'Could not find Homebrew installation'
195+
fi
196+
fi
197+
}
221198

222-
### Ensures the `brew` binary is available on Linux machines. macOS installs `brew` into the default `PATH` so nothing needs to be done for macOS.
223-
if [ -d /home/linuxbrew/.linuxbrew/bin ]; then
224-
logg info "Sourcing shellenv from /home/linuxbrew/.linuxbrew/bin/brew" && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
225-
elif [ -f /usr/local/bin/brew ]; then
226-
logg info "Sourcing shellenv from /usr/local/bin/brew" && eval "$(/usr/local/bin/brew shellenv)"
227-
elif [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" ]; then
228-
logg info "Sourcing shellenv from "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew"" && eval "$("${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" shellenv)"
199+
### Ensures Homebrew folders have proper owners / permissions
200+
fixHomebrewPermissions() {
201+
if command -v brew > /dev/null; then
202+
logg info 'Applying proper permissions on Homebrew folders'
203+
sudo chmod -R go-w "$(brew --prefix)/share"
204+
BREW_DIRS="share etc/bash_completion.d"
205+
for BREW_DIR in $BREW_DIRS; do
206+
if [ -d "$(brew --prefix)/$BREW_DIR" ]; then
207+
sudo chown -Rf "$(whoami)" "$(brew --prefix)/$BREW_DIR"
229208
fi
209+
done
210+
logg info 'Running brew update --force --quiet' && brew update --force --quiet
211+
fi
212+
}
213+
214+
### Installs Homebrew
215+
ensurePackageManagerHomebrew() {
216+
if ! command -v brew > /dev/null; then
217+
### Select install type based off of whether or not sudo privileges are available
218+
if command -v sudo > /dev/null && sudo -n true; then
219+
logg info 'Installing Homebrew. Sudo privileges available.'
220+
echo | bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
221+
else
222+
logg info 'Installing Homebrew. Sudo privileges not available. Password may be required.'
223+
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
230224
fi
225+
226+
### Attempt to fix problematic installs
227+
if [ -n "$BREW_EXIT_CODE" ]; then
228+
logg warn 'Homebrew was installed but part of the installation failed to complete successfully.'
229+
fixHomebrewPermissions
230+
fi
231231
fi
232+
}
232233

233-
### Ensure GCC is installed via Homebrew
234+
### Ensures gcc is installed
235+
ensureGcc() {
234236
if command -v brew > /dev/null; then
235237
if ! brew list | grep gcc > /dev/null; then
236-
logg info "Installing Homebrew gcc" && brew install --quiet gcc
238+
logg info 'Installing Homebrew gcc' && brew install --quiet gcc
239+
else
240+
logg info 'Homebrew gcc is available'
237241
fi
238242
else
239-
logg error "Failed to initialize Homebrew" && exit 2
243+
logg error 'Failed to initialize Homebrew' && exit 1
240244
fi
241245
}
246+
247+
# @description This function ensures Homebrew is installed and available in the `PATH`. It handles the installation of Homebrew on both **Linux and macOS**.
248+
# It will attempt to bypass sudo password entry if it detects that it can do so. The function also has some error handling in regards to various
249+
# directories falling out of the correct ownership and permission states. Finally, it loads Homebrew into the active profile (allowing other parts of the script
250+
# to use the `brew` command).
251+
#
252+
# With Homebrew installed and available, the script finishes by installing the `gcc` Homebrew package which is a very common dependency.
253+
ensureHomebrew() {
254+
loadHomebrew
255+
ensurePackageManagerHomebrew
256+
loadHomebrew
257+
fixHomebrewPermissions
258+
ensureGcc
259+
}
242260
ensureHomebrew
243261

244262
# @description Ensures `cloudflared` is installed via Homebrew

scripts/homebrew.sh

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -148,64 +148,82 @@ ensureBasicDeps() {
148148
}
149149
ensureBasicDeps
150150

151-
# @description This function ensures Homebrew is installed and available in the `PATH`. It handles the installation of Homebrew on both **Linux and macOS**.
152-
# It will attempt to bypass sudo password entry if it detects that it can do so. The function also has some error handling in regards to various
153-
# directories falling out of the correct ownership and permission states. Finally, it loads Homebrew into the active profile (allowing other parts of the script
154-
# to use the `brew` command).
155-
#
156-
# With Homebrew installed and available, the script finishes by installing the `gcc` Homebrew package which is a very common dependency.
157-
ensureHomebrew() {
151+
### Ensure Homebrew is loaded
152+
loadHomebrew() {
158153
if ! command -v brew > /dev/null; then
159-
if [ -d /home/linuxbrew/.linuxbrew/bin ]; then
160-
logg info "Sourcing from /home/linuxbrew/.linuxbrew/bin/brew" && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
161-
if ! command -v brew > /dev/null; then
162-
logg error "The /home/linuxbrew/.linuxbrew directory exists but something is not right. Try removing it and running the script again." && exit 1
163-
fi
164-
elif [ -d "$HOME/.linuxbrew" ]; then
165-
logg info "Sourcing from $HOME/.linuxbrew/bin/brew" && eval "$($HOME/.linuxbrew/bin/brew shellenv)"
166-
if ! command -v brew > /dev/null; then
167-
logg error "The $HOME/.linuxbrew directory exists but something is not right. Try removing it and running the script again." && exit 1
168-
fi
154+
if [ -f /usr/local/bin/brew ]; then
155+
logg info "Using /usr/local/bin/brew" && eval "$(/usr/local/bin/brew shellenv)"
156+
if [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" ]; then
157+
logg info "Using ${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" && eval "$("${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" shellenv)"
158+
if [ -d "$HOME/.linuxbrew" ]; then
159+
logg info "Using $HOME/.linuxbrew/bin/brew" && eval "$("$HOME/.linuxbrew/bin/brew" shellenv)"
160+
elif [ -d "/home/linuxbrew/.linuxbrew" ]; then
161+
logg info 'Using /home/linuxbrew/.linuxbrew/bin/brew' && eval "(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
169162
else
170-
### Installs Homebrew and addresses a couple potential issues
171-
if command -v sudo > /dev/null && sudo -n true; then
172-
logg info "Installing Homebrew"
173-
echo | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
174-
else
175-
logg info "Homebrew is not installed. The script will attempt to install Homebrew and you might be prompted for your password."
176-
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
177-
if [ -n "$BREW_EXIT_CODE" ]; then
178-
if command -v brew > /dev/null; then
179-
logg warn "Homebrew was installed but part of the installation failed. Trying a few things to fix the installation.."
180-
BREW_DIRS="share/man share/doc share/zsh/site-functions etc/bash_completion.d"
181-
for BREW_DIR in $BREW_DIRS; do
182-
if [ -d "$(brew --prefix)/$BREW_DIR" ]; then
183-
logg info "Chowning $(brew --prefix)/$BREW_DIR" && sudo chown -R "$(whoami)" "$(brew --prefix)/$BREW_DIR"
184-
fi
185-
done
186-
logg info "Running brew update --force --quiet" && brew update --force --quiet && logg success "Successfully ran brew update --force --quiet"
187-
fi
188-
fi
189-
fi
163+
logg info 'Could not find Homebrew installation'
164+
fi
165+
fi
166+
}
190167

191-
### Ensures the `brew` binary is available on Linux machines. macOS installs `brew` into the default `PATH` so nothing needs to be done for macOS.
192-
if [ -d /home/linuxbrew/.linuxbrew/bin ]; then
193-
logg info "Sourcing shellenv from /home/linuxbrew/.linuxbrew/bin/brew" && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
194-
elif [ -f /usr/local/bin/brew ]; then
195-
logg info "Sourcing shellenv from /usr/local/bin/brew" && eval "$(/usr/local/bin/brew shellenv)"
196-
elif [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" ]; then
197-
logg info "Sourcing shellenv from "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew"" && eval "$("${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" shellenv)"
168+
### Ensures Homebrew folders have proper owners / permissions
169+
fixHomebrewPermissions() {
170+
if command -v brew > /dev/null; then
171+
logg info 'Applying proper permissions on Homebrew folders'
172+
sudo chmod -R go-w "$(brew --prefix)/share"
173+
BREW_DIRS="share etc/bash_completion.d"
174+
for BREW_DIR in $BREW_DIRS; do
175+
if [ -d "$(brew --prefix)/$BREW_DIR" ]; then
176+
sudo chown -Rf "$(whoami)" "$(brew --prefix)/$BREW_DIR"
198177
fi
178+
done
179+
logg info 'Running brew update --force --quiet' && brew update --force --quiet
180+
fi
181+
}
182+
183+
### Installs Homebrew
184+
ensurePackageManagerHomebrew() {
185+
if ! command -v brew > /dev/null; then
186+
### Select install type based off of whether or not sudo privileges are available
187+
if command -v sudo > /dev/null && sudo -n true; then
188+
logg info 'Installing Homebrew. Sudo privileges available.'
189+
echo | bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
190+
else
191+
logg info 'Installing Homebrew. Sudo privileges not available. Password may be required.'
192+
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
199193
fi
194+
195+
### Attempt to fix problematic installs
196+
if [ -n "$BREW_EXIT_CODE" ]; then
197+
logg warn 'Homebrew was installed but part of the installation failed to complete successfully.'
198+
fixHomebrewPermissions
199+
fi
200200
fi
201+
}
201202

202-
### Ensure GCC is installed via Homebrew
203+
### Ensures gcc is installed
204+
ensureGcc() {
203205
if command -v brew > /dev/null; then
204206
if ! brew list | grep gcc > /dev/null; then
205-
logg info "Installing Homebrew gcc" && brew install --quiet gcc
207+
logg info 'Installing Homebrew gcc' && brew install --quiet gcc
208+
else
209+
logg info 'Homebrew gcc is available'
206210
fi
207211
else
208-
logg error "Failed to initialize Homebrew" && exit 2
212+
logg error 'Failed to initialize Homebrew' && exit 1
209213
fi
210214
}
215+
216+
# @description This function ensures Homebrew is installed and available in the `PATH`. It handles the installation of Homebrew on both **Linux and macOS**.
217+
# It will attempt to bypass sudo password entry if it detects that it can do so. The function also has some error handling in regards to various
218+
# directories falling out of the correct ownership and permission states. Finally, it loads Homebrew into the active profile (allowing other parts of the script
219+
# to use the `brew` command).
220+
#
221+
# With Homebrew installed and available, the script finishes by installing the `gcc` Homebrew package which is a very common dependency.
222+
ensureHomebrew() {
223+
loadHomebrew
224+
ensurePackageManagerHomebrew
225+
loadHomebrew
226+
fixHomebrewPermissions
227+
ensureGcc
228+
}
211229
ensureHomebrew

0 commit comments

Comments
 (0)