-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.bash
More file actions
executable file
Β·145 lines (118 loc) Β· 2.83 KB
/
bootstrap.bash
File metadata and controls
executable file
Β·145 lines (118 loc) Β· 2.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
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
set -eu -o pipefail
__dirname="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
installRoot="${HOME}"
isDryRun='false'
shouldForceRun='false'
shouldShowDiff='false'
verbosity=0
versionString='0.1.0'
showHelp() {
cat <<-EOF
Usage: ./bootstrap.sh [OPTIONS]
Examples:
Install all dotfiles:
./bootstrap.sh
Check if any files would be replaced by running this:
./bootstrap.sh --dry-run
Options:
-D, --diff show the difference in conflicting files
-d, --dry-run just show the files that would be copied over
-f, --force skip the warnings and go straight to setup
-h, --help show this help text
-v, --verbose be more verbose
-V, --version print script version and exit
EOF
}
printError() {
echo 'Error:' "$@" >&2
}
fileIsConflict() {
src="${__dirname}/$1"
dest="${installRoot}/$1"
# Remember, in Bash, 0 is "truthy" and 1 is "falsy"
# File doesn't exist, so it's not a conflict to add
if [ ! -e "$dest" ]; then
return 1
fi
# File exists but has the same contents
if output="$(diff "$src" "$dest")"; then
return 1
fi
# File exists and has different contents
if $shouldShowDiff; then
printf '%s < > %s:\n%s' "$src" "$dest" "$output"
fi
return 0
}
main() {
while (($# > 0)); do
arg="$1"
case $arg in
-d | --dry-run) isDryRun='true' ;;
-D | --diff) shouldShowDiff='true' ;;
-f | --force) shouldForceRun='true' ;;
-h | --help)
showHelp
exit
;;
-v | --verbose) verbosity=1 ;;
-V | --version)
echo "${versionString}"
exit
;;
*)
echo "Error: unrecognized argument '$1'. Use --help to show usage." >&2
exit 1
;;
esac
shift
done
# Stop if expected behavior is unknown
if $isDryRun && $shouldForceRun; then
printError '--dry-run and --force cannot be set at the same time'
exit 1
fi
if ((verbosity > 0)); then
echo "Install root is ${installRoot}"
fi
# Make list of files to install
local files=(
.bash*
'.gitconfig'
'.gitignore'
'.tmux.conf'
)
local conflicts=()
if ! $shouldForceRun; then
# Check for possible conflicts
for file in "${files[@]}"; do
if fileIsConflict "$file"; then
conflicts+=("$file")
fi
done
if ((${#conflicts[@]} > 0)); then
echo ''
echo 'CONFLICTING FILES:'
echo '=================='
for file in "${conflicts[@]}"; do
echo " - $file"
done
echo 'Resolve the conflicting files or use --force to overwrite'
exit 1
fi
fi
for file in "${files[@]}"; do
# Dry run just prints files that would be linked
if $isDryRun; then
printf 'would link %s => %s\n' "${file}" "${installRoot}/${file}"
continue
fi
# Verbose flag still prints files that are linked
if ((verbosity > 0)); then
printf 'linking %s => %s\n' "${file}" "${installRoot}/${file}"
fi
ln -sfn "${__dirname}/${file}" "${installRoot}/${file}"
done
}
main "$@"