Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add shfmt #50

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/lint_pr.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check failure on line 1 in .github/workflows/lint_pr.yaml

View check run for this annotation

Trunk.io / Trunk Check

prettier

Incorrect formatting, autoformat by running 'trunk fmt'
name: Lint PR
on: # yamllint disable-line rule:truthy
pull_request:
Expand All @@ -16,6 +16,15 @@
SHELLCHECK_OPTS: -x
with:
ignore_names: .zshrc .zprofile
shfmt:
name: Run shfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: shfmt
uses: luizm/action-sh-checker@master
env:
SHFMT_OPTS: -s
yamllint:
name: Run yamllint
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions apps.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[base]

Check failure on line 1 in apps.toml

View check run for this annotation

Trunk.io / Trunk Check

taplo

Incorrect formatting, autoformat by running 'trunk fmt'
mas = "formula" # Mac App Store command-line interface
uv = "formula" # Extremely fast Python package installer and resolver, written in Rust

Expand All @@ -21,6 +21,7 @@
jupyter-notebook-viewer = "cask" # Utility to render Jupyter notebooks
"postgresql@14" = "formula" # Object-relational database system
shellcheck = "formula" # Static analysis and lint tool, for (ba)sh scripts
shfmt = "formula" # Autoformat shell script source code
redis = "formula" # Persistent key-value database, with built-in net interface
visual-studio-code = "cask" # Open-source code editor
warp = "cask" # Rust-based terminal
Expand Down
12 changes: 6 additions & 6 deletions bash_traceback.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash

# Bash traceback (from https://gist.github.com/Asher256/4c68119705ffa11adb7446f297a7beae)
set -o errexit # stop the script each time a command fails
set -o nounset # stop if you attempt to use an undef variable
set -o errexit # stop the script each time a command fails
set -o nounset # stop if you attempt to use an undef variable

function bash_traceback() {
local lasterr="$?"
Expand All @@ -13,10 +13,10 @@ function bash_traceback() {
if [ ${#FUNCNAME[@]} -gt 2 ]; then
# Print out the stack trace described by $function_stack
echo "Traceback of ${BASH_SOURCE[1]} (most recent call last):" >&2
for ((i=0; i < ${#FUNCNAME[@]} - 1; i++)); do
local funcname="${FUNCNAME[$i]}"
[ "$i" -eq "0" ] && funcname=$bash_command
echo -e " ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]}\\t$funcname" >&2
for ((i = 0; i < ${#FUNCNAME[@]} - 1; i++)); do
local funcname="${FUNCNAME[$i]}"
[ "$i" -eq "0" ] && funcname=$bash_command
echo -e " ${BASH_SOURCE[i + 1]}:${BASH_LINENO[$i]}\\t$funcname" >&2
done
fi
echo "Exiting with status ${code}" >&2
Expand Down
30 changes: 15 additions & 15 deletions bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#!/usr/bin/env bash

Check failure on line 1 in bootstrap.sh

View check run for this annotation

Trunk.io / Trunk Check

shfmt

Incorrect formatting, autoformat by running 'trunk fmt'

{ # Prevent script from running if partially downloaded

set -euo pipefail
set -euo pipefail

DOTPATH=$HOME/.dotfiles
DOTPATH=$HOME/.dotfiles

echo -e "\033[1;34m🥾 Bootstrapping dotfiles\033[0m"
echo -e "\033[1;34m🥾 Bootstrapping dotfiles\033[0m"

if [ ! -d "$DOTPATH" ]; then
git clone https://github.com/martimlobao/dotfiles.git "$DOTPATH"
echo -e "\033[1;32m✅ Cloned $DOTPATH\033[0m"
else
echo -e "\033[1;34m✅ Dotfiles already downloaded to $DOTPATH\033[0m"
fi
if [ ! -d "$DOTPATH" ]; then
git clone https://github.com/martimlobao/dotfiles.git "$DOTPATH"
echo -e "\033[1;32m✅ Cloned $DOTPATH\033[0m"
else
echo -e "\033[1;34m✅ Dotfiles already downloaded to $DOTPATH\033[0m"
fi

cd "$DOTPATH"
cd "$DOTPATH"

if [[ "${1:-}" == "--yes" ]] || [[ "${1:-}" == "-y" ]]; then
./run.sh -y
else
./run.sh
fi
if [[ ${1:-} == "--yes" ]] || [[ ${1:-} == "-y" ]]; then
./run.sh -y
else
./run.sh
fi

} # Prevent script from running if partially downloaded
10 changes: 4 additions & 6 deletions dock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,19 @@ function add_folder_to_dock {
sortby="1"
displayas="0"
viewcontentas="0"
while [[ "$#" -gt 0 ]]
do
while [[ $# -gt 0 ]]; do
case $1 in
-s|--sortby)
-s | --sortby)
if [[ $2 =~ ^[1-5]$ ]]; then
sortby="${2}"
fi
;;
-d|--displayas)
-d | --displayas)
if [[ $2 =~ ^[0-1]$ ]]; then
displayas="${2}"
fi
;;
-v|--viewcontentas)
-v | --viewcontentas)
if [[ $2 =~ ^[0-3]$ ]]; then
viewcontentas="${2}"
fi
Expand Down Expand Up @@ -113,7 +112,6 @@ function reset_launchpad {
defaults write com.apple.dock ResetLaunchPad -bool true
}


###############################################################################
# CONFIGURE MACOS DOCK #
###############################################################################
Expand Down
17 changes: 8 additions & 9 deletions dotsync.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

Check failure on line 1 in dotsync.sh

View check run for this annotation

Trunk.io / Trunk Check

shfmt

Incorrect formatting, autoformat by running 'trunk fmt'

# Root is $DOTPATH if it exists, otherwise the directory of this script
root=$(realpath "${DOTPATH:-$(dirname "$(realpath "$0")")}")
Expand All @@ -10,37 +10,36 @@
# UPDATE DOTFILES #
###############################################################################


function dotlink() {
find "linkme" -type d -mindepth 1 | sed "s|^linkme/||" | \
find "linkme" -type d -mindepth 1 | sed "s|^linkme/||" |
while read -r dir; do mkdir -p "$HOME/$dir"; done
find "linkme" -type f -not -name '.DS_Store' | sed "s|^linkme/||" | \
find "linkme" -type f -not -name '.DS_Store' | sed "s|^linkme/||" |
while read -r file; do
echo -e "\033[1;32m🔗 Linked $(pwd)/linkme/$file -> $HOME/$file\033[0m"
ln -fvns "$(pwd)/linkme/$file" "$HOME/$file" &> /dev/null;
ln -fvns "$(pwd)/linkme/$file" "$HOME/$file" &>/dev/null
done
}

function dotunlink() {
rsync -av --exclude='.DS_Store' linkme/ "$HOME" | \
grep -v "building file list ... done" | \
rsync -av --exclude='.DS_Store' linkme/ "$HOME" |
grep -v "building file list ... done" |
awk '/^$/ { exit } !/\/$/ { printf "\033[1;32m🔙 Restored %s\033[0m\n", $0; }'
}

# Copy all files from copyme/ to $HOME
if [ "${1:-}" == "unlink" ]; then
echo -e "\033[1;34m📋 Restoring dotfiles...\033[0m"
dotunlink;
dotunlink
else
echo -e "\033[1;34m🔗 Linking dotfiles...\033[0m"
if [[ "${1:-}" != "-y" ]] && [[ "${1:-}" != "--yes" ]]; then
if [[ ${1:-} != "-y" ]] && [[ ${1:-} != "--yes" ]]; then
read -rp $'❓ \e[1;31mOverwrite existing dotfiles with symlinks to stored dotfiles? (y/n)\e[0m ' LINK
else
LINK="y"
fi

if [[ $LINK =~ ^[Yy]$ ]]; then
dotlink;
dotlink
fi
fi

Expand Down
92 changes: 46 additions & 46 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

Check failure on line 1 in install.sh

View check run for this annotation

Trunk.io / Trunk Check

shfmt

Incorrect formatting, autoformat by running 'trunk fmt'

# Root is $DOTPATH if it exists, otherwise the directory of this script
root=$(realpath "${DOTPATH:-$(dirname "$(realpath "$0")")}")
Expand All @@ -14,12 +14,12 @@

# Check if the first argument is -y or --yes
auto_yes=false
if [[ "${1:-}" == "-y" ]] || [[ "${1:-}" == "--yes" ]]; then
if [[ ${1:-} == "-y" ]] || [[ ${1:-} == "--yes" ]]; then
auto_yes=true
fi

# Ensure yq is installed to parse the apps.toml file
if ! command -v yq &> /dev/null; then
if ! command -v yq &>/dev/null; then
echo -e "⬇️ \033[1;34mInstalling yq to parse apps.toml...\033[0m"
brew install yq
fi
Expand Down Expand Up @@ -57,45 +57,45 @@
shift
local item
for item; do
[[ "$item" == "$needle" ]] && return 0
[[ $item == "$needle" ]] && return 0
done
return 1
}

# Install function that uses the correct command based on the installation method
install () {
install() {
local app="$1"
local app_name="${app##*/}" # Extract the part after the last slash
local app_name="${app##*/}" # Extract the part after the last slash
local method="$2"
local cmd=""
local is_installed=false

case "$method" in
"cask")
cmd="brew install --cask"
in_array "$app_name" "${installed_casks[@]}" && is_installed=true
;;
"formula")
cmd="brew install --formula"
in_array "$app_name" "${installed_formulas[@]}" && is_installed=true
;;
"mas")
if mas list | grep -q "$app "; then
is_installed=true
app_name=$(mas list | grep "$app " | sed -E 's/.*[0-9]+[[:space:]]+(.*)[[:space:]]+\(.*/\1/' | sed -E 's/[[:space:]]*$//')
else
app_name=$(mas info "$app" | head -n 1 | sed -E 's/(.*)[[:space:]]+[0-9\.]+ \[.*\]/\1/')
cmd="mas install"
fi
;;
"uv")
cmd="uv tool install"
in_array "$app" "${installed_uv[@]}" && is_installed=true
;;
*)
echo -e "❌ \033[1;31mUnknown installation method: $method for $app_name\033[0m"
return 1
;;
"cask")
cmd="brew install --cask"
in_array "$app_name" "${installed_casks[@]}" && is_installed=true
;;
"formula")
cmd="brew install --formula"
in_array "$app_name" "${installed_formulas[@]}" && is_installed=true
;;
"mas")
if mas list | grep -q "$app "; then
is_installed=true
app_name=$(mas list | grep "$app " | sed -E 's/.*[0-9]+[[:space:]]+(.*)[[:space:]]+\(.*/\1/' | sed -E 's/[[:space:]]*$//')
else
app_name=$(mas info "$app" | head -n 1 | sed -E 's/(.*)[[:space:]]+[0-9\.]+ \[.*\]/\1/')
cmd="mas install"
fi
;;
"uv")
cmd="uv tool install"
in_array "$app" "${installed_uv[@]}" && is_installed=true
;;
*)
echo -e "❌ \033[1;31mUnknown installation method: $method for $app_name\033[0m"
return 1
;;
esac

if ! $is_installed; then
Expand All @@ -112,7 +112,7 @@
brew_sync() {
local toml_apps
toml_apps=$(yq eval 'to_entries | map(.value | to_entries | map(select(.value == "cask" or .value == "formula") | .key)) | flatten | .[]' "$apps_toml")
toml_apps=$(echo "$toml_apps" | sed -E 's|.*/||') # get name from tapped apps (slashes in name)
toml_apps=$(echo "$toml_apps" | sed -E 's|.*/||') # get name from tapped apps (slashes in name)

local missing_formulae
missing_formulae=$(comm -23 <(brew leaves | sort) <(echo "$toml_apps" | sort))
Expand All @@ -121,18 +121,18 @@
local missing_apps
missing_apps=$(echo -e "$missing_formulae\n$missing_casks" | sort -u)

if [[ -n "$missing_apps" ]]; then
if [[ -n $missing_apps ]]; then
echo -e "❗️ \033[1;31mThe following Homebrew-installed formulae and casks are missing from apps.toml:\033[0m"
# shellcheck disable=SC2001
echo "$missing_formulae" | sed 's/^/ /'
# shellcheck disable=SC2001
# shellcheck disable=SC2001
echo "$missing_casks" | sed 's/^/ /'
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'❓ \e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for app in $missing_apps; do
brew uninstall "$app"
echo -e "🚮 \033[1;35mUninstalled $app.\033[0m"
Expand All @@ -152,16 +152,16 @@
local missing_uv_apps
missing_uv_apps=$(comm -23 <(uv tool list | awk '{print $1}' | grep -v '^-*$' | sort) <(echo "$toml_apps" | sort))

if [[ -n "$missing_uv_apps" ]]; then
if [[ -n $missing_uv_apps ]]; then
echo -e "❗️ \033[1;31mThe following uv-installed apps are missing from apps.toml:\033[0m"
# shellcheck disable=SC2001
# shellcheck disable=SC2001
echo "$missing_uv_apps" | sed 's/^/ /'
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'❓ \e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for app in $missing_uv_apps; do
uv tool uninstall "$app"
echo -e "🚮 \033[1;35mUninstalled $app.\033[0m"
Expand All @@ -182,25 +182,25 @@
installed_mas_apps=$(mas list | sed -E 's/^([0-9]+)[[:space:]]+(.*)[[:space:]]+\(.*/\1 \2/' | sort)

# `-A` requires bash 4+, can't use Apple-provided bash which is 3.2
declare -A missing_mas_apps=() # Ensure it's initialized as an empty associative array
declare -A missing_mas_apps=() # Ensure it's initialized as an empty associative array

while read -r id name; do
if ! echo "$toml_apps" | grep -q "^$id$"; then
missing_mas_apps["$id"]="$name" # Store ID as key and app name as value
missing_mas_apps["$id"]="$name" # Store ID as key and app name as value
fi
done <<< "$installed_mas_apps"
done <<<"$installed_mas_apps"

if [[ ${#missing_mas_apps[@]} -gt 0 ]]; then
echo -e "❗️ \033[1;31mThe following Mac App Store apps are missing from apps.toml:\033[0m"
for id in "${!missing_mas_apps[@]}"; do
echo -e " ${missing_mas_apps[$id]} ($id)"
done
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'❓ \e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for id in "${!missing_mas_apps[@]}"; do
name="${missing_mas_apps[$id]}"
if ! mas uninstall "$id"; then
Expand All @@ -227,8 +227,8 @@
# Install apps from each category in the apps.toml file
current_category=""
echo "$parsed_toml" | while IFS=$'\t' read -r category app method; do
if [[ "$category" != "$current_category" ]]; then
suffix=$([[ "$category" == *s ]] && echo "" || echo " apps")
if [[ $category != "$current_category" ]]; then
suffix=$([[ $category == *s ]] && echo "" || echo " apps")
echo -e "\n📦 \033[1;35mInstalling ${category}${suffix}...\033[0m"
current_category="$category"
fi
Expand Down
6 changes: 3 additions & 3 deletions local.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

Check failure on line 1 in local.sh

View check run for this annotation

Trunk.io / Trunk Check

shfmt

Incorrect formatting, autoformat by running 'trunk fmt'

# Root is $DOTPATH if it exists, otherwise the directory of this script
root=$(realpath "${DOTPATH:-$(dirname "$(realpath "$0")")}")
Expand Down Expand Up @@ -33,14 +33,14 @@
fi

# Copy all files from copyme/ to $HOME
if [[ "${1:-}" != "--yes" ]] && [[ "${1:-}" != "-y" ]]; then
if [[ ${1:-} != "--yes" ]] && [[ ${1:-} != "-y" ]]; then
read -rp $'❓ \e[1;31mDo you want to copy and overwrite all files from copyme/ to $HOME? (y/n)\e[0m ' COPYME
else
COPYME="y"
fi
if [[ $COPYME =~ ^[Yy]$ ]]; then
rsync -av --exclude='.DS_Store' copyme/ "$HOME" | \
grep -v "building file list ... done" | \
rsync -av --exclude='.DS_Store' copyme/ "$HOME" |
grep -v "building file list ... done" |
awk '/^$/ { exit } !/\/$/ { printf "\033[1;32m📋 Copied %s\033[0m\n", $0; }'
fi

Expand Down
Loading