Skip to content

Commit

Permalink
Merge pull request #19 from gvatsal60/minor_fixes
Browse files Browse the repository at this point in the history
Minor Fixes
  • Loading branch information
andmpel authored Oct 27, 2024
2 parents ab855a4 + f06900f commit fbd2365
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 49 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ jobs:
uses: actions/checkout@v4
- name: Run Test
run: |
zsh update-all.sh || { echo "The script exited with an error."; exit 1; }
set -x # Enable Execution Logs
zsh install.sh || { echo "The script exited with an error."; exit 1; }
echo "The script executed successfully."
ZSHRC_PATH="${HOME}/.zshrc"
if ! awk '/^update\(\) {/,/^}/' "${ZSHRC_PATH}" | grep -q 'curl'; then
echo "Error: Test Failed!!!"
exit 1
fi
echo "TEST PASSED"
60 changes: 43 additions & 17 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,39 @@ set -e

readonly FILE_NAME="update-all.sh"
readonly UPDATE_SCRIPT_SOURCE_URL="https://raw.githubusercontent.com/andmpel/MacOS-All-In-One-Update-Script/HEAD/${FILE_NAME}"
readonly UPDATE_ALIAS_SEARCH_STR="alias update='curl -fsSL ${UPDATE_SCRIPT_SOURCE_URL} | zsh'"

UPDATE_ALIAS_SOURCE_STR=$(
UPDATE_SOURCE_STR=$(
cat <<EOF
# Alias for Update
${UPDATE_ALIAS_SEARCH_STR}
# System Update
update() {
# Check if curl is available
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required but not installed. Please install curl." >&2
exit 1
fi
# Check internet connection by pinging a reliable server
TEST_URL="https://www.google.com"
# Use curl to check the connection
TEST_RESP=\$(curl -Is --connect-timeout 5 --max-time 10 "\${TEST_URL}" 2>/dev/null | head -n 1)
# Check if response is empty
if [ -z "\${TEST_RESP}" ]; then
echo "No Internet Connection!!!" >&2
exit 1
fi
# Check for "200" in the response
if ! printf "%s" "\${TEST_RESP}" | grep -q "200"; then
echo "Internet is not working!!!" >&2
exit 1
fi
curl -fsSL ${UPDATE_SCRIPT_SOURCE_URL} | zsh
}
EOF
)

Expand All @@ -33,7 +59,13 @@ EOF
# Function: println
# Description: Prints each argument on a new line, suppressing any error messages.
println() {
command printf %s\\n "$*" 2>/dev/null
printf "%s\n" "$*" 2>/dev/null
}

# Function: print_err
# Description: Prints each argument as error messages.
print_err() {
printf "%s\n" "$*" >&2
}

# Function: update_rc
Expand All @@ -45,16 +77,16 @@ update_rc() {
_rc="${HOME}/.zshrc"
;;
*)
println >&2 "Error: Unsupported or unrecognized distribution ${ADJUSTED_ID}"
print_err "Error: Unsupported or unrecognized distribution ${ADJUSTED_ID}"
exit 1
;;
esac

# Check if `alias update='sudo sh ${HOME}/.update.sh'` is already defined, if not then append it
if [ -f "${_rc}" ]; then
if ! grep -qxF "${UPDATE_ALIAS_SEARCH_STR}" "${_rc}"; then
if ! awk '/^update\(\) {/,/^}/' "${_rc}" | grep -q 'curl'; then
println "==> Updating ${_rc} for ${ADJUSTED_ID}..."
println "${UPDATE_ALIAS_SOURCE_STR}" >>"${_rc}"
println "${UPDATE_SOURCE_STR}" >>"${_rc}"
fi
else
# Notify if the rc file does not exist
Expand All @@ -63,7 +95,7 @@ update_rc() {
# Create the rc file
touch "${_rc}"
# Append the sourcing block to the newly created rc file
println "${UPDATE_ALIAS_SOURCE_STR}" >>"${_rc}"
println "${UPDATE_SOURCE_STR}" >>"${_rc}"
fi

println ""
Expand All @@ -84,16 +116,10 @@ Darwin)
ADJUSTED_ID="darwin"
;;
*)
println >&2 "Error: Unsupported or unrecognized OS distribution: ${OS}"
print_err "Error: Unsupported or unrecognized OS distribution: ${OS}"
exit 1
;;
esac

# Check if curl is available
if ! command -v curl >/dev/null 2>&1; then
println >&2 "Error: curl is required but not installed. Please install curl."
exit 1
fi

# Update the rc (.zshrc) file for `update` alias
# Update the rc (.zshrc) file for `update`
update_rc
90 changes: 59 additions & 31 deletions update-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ print_err() {
printf "\n${RED}%s${CLEAR}\n" "$*" >&2
}

check_command() {
command_name="$1"

if ! command -v "${command_name}" >/dev/null 2>&1; then
print_err "${command_name} is not installed."
return 1
fi

return 0
}

update_brew() {
println "Updating Brew Formula's"

if ! command -v brew >/dev/null 2>&1; then
print_err "Brew is not installed."
if ! check_command brew; then
return
fi

Expand All @@ -49,8 +59,7 @@ update_brew() {
update_vscode() {
println "Updating VSCode Extensions"

if ! command -v code >/dev/null 2>&1; then
print_err "VSCode is not installed."
if ! check_command code; then
return
fi

Expand All @@ -72,8 +81,7 @@ update_office() {
update_gem() {
println "Updating Gems"

if ! command -v gem >/dev/null 2>&1; then
print_err "Gem is not installed."
if ! check_command gem; then
return
fi

Expand All @@ -83,8 +91,7 @@ update_gem() {
update_npm() {
println "Updating Npm Packages"

if ! command -v npm >/dev/null 2>&1; then
print_err "Npm is not installed."
if ! check_command npm; then
return
fi

Expand All @@ -94,8 +101,7 @@ update_npm() {
update_yarn() {
println "Updating Yarn Packages"

if ! command -v yarn >/dev/null 2>&1; then
print_err "Yarn is not installed."
if ! check_command yarn; then
return
fi

Expand All @@ -105,8 +111,7 @@ update_yarn() {
update_pip3() {
println "Updating Python 3.x pips"

if ! command -v python3 >/dev/null 2>&1 || ! command -v pip3 >/dev/null 2>&1; then
print_err "Python3 or pip3 is not installed."
if ! check_command python3 || ! check_command pip3; then
return
fi

Expand All @@ -116,20 +121,17 @@ update_pip3() {
update_cargo() {
println "Updating Rust Cargo Crates"

if ! command -v cargo >/dev/null 2>&1; then
print_err "Rust/Cargo is not installed."
if ! check_command cargo; then
return
fi

cargo install "$(cargo install --list | grep -E '^[a-z0-9_-]+ v[0-9.]+:$' | cut -f1 -d' ')"
}


update_app_store() {
println "Updating App Store Applications"

if ! command -v mas >/dev/null 2>&1; then
print_err "mas is not installed."
if ! check_command mas; then
return
fi

Expand All @@ -141,23 +143,49 @@ update_macos() {
softwareupdate -i -a
}

check_internet() {
if ! check_command curl; then
print_err "Error: curl is required but not installed. Please install curl."
return 1
fi

# Check internet connection by pinging a reliable server
TEST_URL="https://www.google.com"

# Use curl to check the connection
TEST_RESP=$(curl -Is --connect-timeout 5 --max-time 10 "${TEST_URL}" 2>/dev/null | head -n 1)

# Check if response is empty
if [ -z "${TEST_RESP}" ]; then
print_err "No Internet Connection!!!"
return 1
fi

# Check for "200" in the response
if ! printf "%s" "${TEST_RESP}" | grep -q "200"; then
print_err "Internet is not working!!!"
return 1
fi

return 0
}

update_all() {
readonly PING_IP=8.8.8.8
if ping -q -W 1 -c 1 $PING_IP >/dev/null 2>&1; then
update_brew
update_office
update_vscode
update_gem
update_npm
update_yarn
update_pip3
update_cargo
update_app_store
update_macos
else
print_err "Internet Disabled!!!"
# Check if internet is available
if ! check_internet; then
exit 1
fi

update_brew
update_office
update_vscode
update_gem
update_npm
update_yarn
update_pip3
update_cargo
update_app_store
update_macos
}

# COMMENT OUT IF SOURCING
Expand Down

0 comments on commit fbd2365

Please sign in to comment.