-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·134 lines (110 loc) · 3.83 KB
/
deploy.sh
File metadata and controls
executable file
·134 lines (110 loc) · 3.83 KB
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
#!/usr/bin/env bash
# deploy.sh - Symlink dotfile into the user home directory via GNU Stow.
# Copyright (C) 2026 Thiago C Silva <librefos@hotmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
readonly RED='\033[1;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m'
info() { printf "${GREEN}[INFO]${NC} %b\n" "$1" >&2; }
warn() { printf "${YELLOW}[WARN]${NC} %b\n" "$1" >&2; }
error() { printf "${RED}[ERROR]${NC} %b\n" "$1" >&2; exit 1; }
# Parse a comma-separated string of names against a reference array.
parse_selection()
{
local choices_string="$1"
local -n reference_array="$2"
local choice_token
IFS=',' read -ra tokens <<< "$choices_string"
for choice_token in "${tokens[@]}"; do
choice_token="${choice_token// /}"
local is_valid_choice=false
local valid_item
for valid_item in "${reference_array[@]}"; do
if [[ "$choice_token" == "$valid_item" ]]; then
is_valid_choice=true
break
fi
done
if $is_valid_choice; then
printf '%s\n' "$choice_token"
else
warn "Invalid selection ignored: $choice_token"
fi
done
}
[[ "$EUID" -eq 0 ]] && error 'Do not run this script as root.'
command -v stow &>/dev/null || error "'stow' is not installed."
readonly REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly TARGET_DIR="$HOME"
shopt -s nullglob
readonly all_dirs=("$REPO_DIR"/*/)
dotfiles=()
for dir in "${all_dirs[@]}"; do
dotfiles+=("$(basename "$dir")")
done
shopt -u nullglob
[[ ${#dotfiles[@]} -eq 0 ]] && error 'No stow packages found.'
# Subshell is used to isolate IFS changes when generating the list.
readonly comma_separated_list="$(IFS=','; printf '%s' "${dotfiles[*]}")"
if [[ "${1:-}" == "--list" ]]; then
printf '%s\n' "$comma_separated_list"
exit 0
fi
if [[ $# -eq 1 ]]; then
choices="$1"
info "Using provided selection: $choices"
else
printf 'Available dotfile packages: %s\n' "$comma_separated_list"
printf 'Enter names to stow (comma-separated) ' >/dev/tty
printf 'or press Enter to skip all: ' >/dev/tty
read -r choices </dev/tty
fi
mapfile -t selected_dotfiles < <(parse_selection "$choices" dotfiles)
if [[ ${#selected_dotfiles[@]} -eq 0 ]]; then
info 'No packages selected. Exiting.'
exit 0
fi
info "Selected for deployment: ${selected_dotfiles[*]}"
backup_conflict()
{
local file="$1"
[[ -e "$file" || -L "$file" ]] || return 0
warn "Backing up: $file -> ${file}.bak"
mv "$file" "${file}.bak"
}
# Discard the prefix and captures all non-whitespace characters
# representing the target file path before the word 'since'.
stow_conflict_pat='over existing target \K\S+(?= since)'
stow_flags=(--no-folding --dir="$REPO_DIR" --target="$TARGET_DIR")
for pkg in "${selected_dotfiles[@]}"; do
[[ -d "$REPO_DIR/$pkg" ]] || {
warn "Package '$pkg' not found. Skipping."
continue
}
info "Checking for conflicts in $pkg..."
while IFS= read -r conflict; do
backup_conflict "$TARGET_DIR/$conflict"
done < <(
stow --simulate "${stow_flags[@]}" "$pkg" 2>&1 |
grep -oP "$stow_conflict_pat" || true
)
info "Stowing $pkg..."
if ! stow "${stow_flags[@]}" "$pkg"; then
warn "Unresolved conflicts in '$pkg'. Resolve manually and re-run."
fi
done
info 'Dotfile deployment complete.'