mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-14 20:53:25 +00:00
chore(lint): formatting
This commit is contained in:
38
local/bin/t
38
local/bin/t
@@ -12,7 +12,8 @@ DOTFILES="${DOTFILES:-$HOME/.dotfiles}"
|
|||||||
T_MAX_DEPTH="${T_MAX_DEPTH:-3}"
|
T_MAX_DEPTH="${T_MAX_DEPTH:-3}"
|
||||||
|
|
||||||
# Function to print an error message and exit
|
# Function to print an error message and exit
|
||||||
error_exit() {
|
error_exit()
|
||||||
|
{
|
||||||
echo "Error: $1" >&2
|
echo "Error: $1" >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
@@ -22,9 +23,9 @@ if [[ ! -d "$T_ROOT" ]]; then
|
|||||||
error_exit "T_ROOT directory '$T_ROOT' does not exist."
|
error_exit "T_ROOT directory '$T_ROOT' does not exist."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Check for required dependencies
|
# Check for required dependencies
|
||||||
check_dependencies() {
|
check_dependencies()
|
||||||
|
{
|
||||||
local T_DEPS=(tmux fzf find)
|
local T_DEPS=(tmux fzf find)
|
||||||
for cmd in "${T_DEPS[@]}"; do
|
for cmd in "${T_DEPS[@]}"; do
|
||||||
if ! command -v "$cmd" &> /dev/null; then
|
if ! command -v "$cmd" &> /dev/null; then
|
||||||
@@ -37,19 +38,21 @@ check_dependencies
|
|||||||
|
|
||||||
# Generate an array of '-not -path' rules for each exclusion pattern
|
# Generate an array of '-not -path' rules for each exclusion pattern
|
||||||
# without using namerefs.
|
# without using namerefs.
|
||||||
generate_exclude_rules() {
|
generate_exclude_rules()
|
||||||
|
{
|
||||||
local result_var="$1"
|
local result_var="$1"
|
||||||
shift
|
shift
|
||||||
local arr=()
|
local arr=()
|
||||||
for pattern in "$@"; do
|
for pattern in "$@"; do
|
||||||
# Exclude both the directory and any subdirectories under it.
|
# Exclude both the directory and any subdirectories under it.
|
||||||
arr+=( -not -path "*/${pattern}" -not -path "*/${pattern}/*" )
|
arr+=(-not -path "*/${pattern}" -not -path "*/${pattern}/*")
|
||||||
done
|
done
|
||||||
# Use eval to assign the array to the variable whose name was passed.
|
# Use eval to assign the array to the variable whose name was passed.
|
||||||
eval "$result_var=(\"\${arr[@]}\")"
|
eval "$result_var=(\"\${arr[@]}\")"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_directories() {
|
get_directories()
|
||||||
|
{
|
||||||
local exclude_patterns=(
|
local exclude_patterns=(
|
||||||
".bzr" ".git" ".hg" ".idea" ".obsidian" ".run" ".svn" ".vscode"
|
".bzr" ".git" ".hg" ".idea" ".obsidian" ".run" ".svn" ".vscode"
|
||||||
"build" "dist" "node_modules" "out" "target" "vendor"
|
"build" "dist" "node_modules" "out" "target" "vendor"
|
||||||
@@ -60,16 +63,18 @@ get_directories() {
|
|||||||
local dirs
|
local dirs
|
||||||
# Use $'string' to correctly process escape sequences.
|
# Use $'string' to correctly process escape sequences.
|
||||||
dirs=$'# Directories\n'
|
dirs=$'# Directories\n'
|
||||||
dirs+=$(find "$T_ROOT" \
|
dirs+=$(
|
||||||
-maxdepth "$T_MAX_DEPTH" \
|
find "$T_ROOT" \
|
||||||
-mindepth 1 \
|
-maxdepth "$T_MAX_DEPTH" \
|
||||||
-type d \
|
-mindepth 1 \
|
||||||
"${exclude_rules[@]}"
|
-type d \
|
||||||
|
"${exclude_rules[@]}"
|
||||||
)
|
)
|
||||||
echo -e "$dirs"
|
echo -e "$dirs"
|
||||||
}
|
}
|
||||||
|
|
||||||
check_tmux() {
|
check_tmux()
|
||||||
|
{
|
||||||
if ! command -v tmux &> /dev/null; then
|
if ! command -v tmux &> /dev/null; then
|
||||||
error_exit "tmux is not installed."
|
error_exit "tmux is not installed."
|
||||||
fi
|
fi
|
||||||
@@ -80,7 +85,8 @@ check_tmux() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
get_sessions() {
|
get_sessions()
|
||||||
|
{
|
||||||
check_tmux
|
check_tmux
|
||||||
|
|
||||||
T_TMUX_SESSIONS=$(tmux list-sessions -F "#{session_name}" 2> /dev/null)
|
T_TMUX_SESSIONS=$(tmux list-sessions -F "#{session_name}" 2> /dev/null)
|
||||||
@@ -119,8 +125,8 @@ fi
|
|||||||
session_name="${session_name//./}"
|
session_name="${session_name//./}"
|
||||||
|
|
||||||
# Attempt to switch to an existing session
|
# Attempt to switch to an existing session
|
||||||
tmux switch-client -t "=$session_name" 2>/dev/null
|
tmux switch-client -t "=$session_name" 2> /dev/null
|
||||||
active_session=$(tmux display-message -p -F '#{session_name}' 2>/dev/null)
|
active_session=$(tmux display-message -p -F '#{session_name}' 2> /dev/null)
|
||||||
|
|
||||||
if [[ "$active_session" == "$session_name" ]]; then
|
if [[ "$active_session" == "$session_name" ]]; then
|
||||||
exit 0
|
exit 0
|
||||||
@@ -132,7 +138,7 @@ if [ -z "$TMUX" ]; then
|
|||||||
tmux new-session -A -s "$session_name" -c "$selected"
|
tmux new-session -A -s "$session_name" -c "$selected"
|
||||||
else
|
else
|
||||||
# Inside tmux: check if the target session exists.
|
# Inside tmux: check if the target session exists.
|
||||||
if tmux has-session -t "$session_name" 2>/dev/null; then
|
if tmux has-session -t "$session_name" 2> /dev/null; then
|
||||||
# Session exists; switch to it.
|
# Session exists; switch to it.
|
||||||
tmux switch-client -t "$session_name"
|
tmux switch-client -t "$session_name"
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Function to remove node_modules and vendor folders
|
# Function to remove node_modules and vendor folders
|
||||||
remove_node_modules_vendor() {
|
remove_node_modules_vendor()
|
||||||
|
{
|
||||||
local dir=$1
|
local dir=$1
|
||||||
|
|
||||||
# If the directory is a symlink, skip it
|
# If the directory is a symlink, skip it
|
||||||
|
|||||||
@@ -12,13 +12,15 @@ GITHUB_API_URL="${GITHUB_API_URL:-https://api.github.com/repos}"
|
|||||||
VERBOSE="${VERBOSE:-0}"
|
VERBOSE="${VERBOSE:-0}"
|
||||||
|
|
||||||
# Prints a message if VERBOSE=1
|
# Prints a message if VERBOSE=1
|
||||||
msg() {
|
msg()
|
||||||
|
{
|
||||||
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
|
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Show usage information
|
# Show usage information
|
||||||
usage() {
|
usage()
|
||||||
cat <<EOF
|
{
|
||||||
|
cat << EOF
|
||||||
Usage: $0 <repo> (e.g. ivuorinen/dotfiles)
|
Usage: $0 <repo> (e.g. ivuorinen/dotfiles)
|
||||||
|
|
||||||
Fetches the latest release version, latest branch tag, or latest commit SHA from GitHub.
|
Fetches the latest release version, latest branch tag, or latest commit SHA from GitHub.
|
||||||
@@ -65,9 +67,10 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Check that required dependencies are installed
|
# Check that required dependencies are installed
|
||||||
check_dependencies() {
|
check_dependencies()
|
||||||
|
{
|
||||||
for cmd in curl jq; do
|
for cmd in curl jq; do
|
||||||
if ! command -v "$cmd" &>/dev/null; then
|
if ! command -v "$cmd" &> /dev/null; then
|
||||||
echo "Error: '$cmd' is required but not installed." >&2
|
echo "Error: '$cmd' is required but not installed." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -76,7 +79,8 @@ check_dependencies() {
|
|||||||
|
|
||||||
# Fetches the latest release or the oldest if OLDEST_RELEASE=1
|
# Fetches the latest release or the oldest if OLDEST_RELEASE=1
|
||||||
# $1 - GitHub repository (string)
|
# $1 - GitHub repository (string)
|
||||||
get_release_version() {
|
get_release_version()
|
||||||
|
{
|
||||||
local repo="$1"
|
local repo="$1"
|
||||||
local include_prereleases="${INCLUDE_PRERELEASES:-0}"
|
local include_prereleases="${INCLUDE_PRERELEASES:-0}"
|
||||||
local oldest_release="${OLDEST_RELEASE:-0}"
|
local oldest_release="${OLDEST_RELEASE:-0}"
|
||||||
@@ -93,7 +97,7 @@ get_release_version() {
|
|||||||
json_response=$(curl -sSL "${auth_header[@]}" "$api_url")
|
json_response=$(curl -sSL "${auth_header[@]}" "$api_url")
|
||||||
|
|
||||||
# Check for API errors
|
# Check for API errors
|
||||||
if echo "$json_response" | jq -e 'has("message")' >/dev/null; then
|
if echo "$json_response" | jq -e 'has("message")' > /dev/null; then
|
||||||
msg "GitHub API error: $(echo "$json_response" | jq -r '.message')"
|
msg "GitHub API error: $(echo "$json_response" | jq -r '.message')"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -117,7 +121,8 @@ get_release_version() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Fetches the latest tag from the specified branch
|
# Fetches the latest tag from the specified branch
|
||||||
get_latest_branch_tag() {
|
get_latest_branch_tag()
|
||||||
|
{
|
||||||
local repo="$1"
|
local repo="$1"
|
||||||
local branch="${BRANCH:-main}"
|
local branch="${BRANCH:-main}"
|
||||||
local api_url="${GITHUB_API_URL}/${repo}/git/refs/tags"
|
local api_url="${GITHUB_API_URL}/${repo}/git/refs/tags"
|
||||||
@@ -139,7 +144,8 @@ get_latest_branch_tag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Fetches the latest commit SHA from the specified branch
|
# Fetches the latest commit SHA from the specified branch
|
||||||
get_latest_commit() {
|
get_latest_commit()
|
||||||
|
{
|
||||||
local repo="$1"
|
local repo="$1"
|
||||||
local branch="${BRANCH:-main}"
|
local branch="${BRANCH:-main}"
|
||||||
local api_url="${GITHUB_API_URL}/${repo}/commits/$branch"
|
local api_url="${GITHUB_API_URL}/${repo}/commits/$branch"
|
||||||
@@ -162,7 +168,8 @@ get_latest_commit() {
|
|||||||
|
|
||||||
# Main function
|
# Main function
|
||||||
# $1 - GitHub repository (string)
|
# $1 - GitHub repository (string)
|
||||||
main() {
|
main()
|
||||||
|
{
|
||||||
if [[ $# -ne 1 ]]; then
|
if [[ $# -ne 1 ]]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user