This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·167 lines (148 loc) · 4.45 KB
/
bootstrap.sh
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#===============================================================================
# AUTHOR: Manoel Brunnen, [email protected]
# CREATED: 13.08.2017
# LICENSE: MIT
# FILE: bootstrap.sh
# USAGE: ./bootstrap.sh
#
# DESCRIPTION: boostrap the dotfiles.
#
# No warranty! For nothing. Use it at your own risk.
#===============================================================================
set -e
set -u
#================================== Variables ==================================
red="\E[91m"
green="\E[92m"
yellow="\E[93m"
blue="\E[94m"
# magenta="\E[95m"
cyan="\E[96m"
reset="\E[0m"
# bold="\E[1m"
timestamp=$(date +"%s")
log_dir="/tmp/boostrap_logs_$timestamp"
# absolute or relative to destination dir
backup_dir="/tmp/boostrap_backup_$timestamp"
log_prefix="$log_dir/$(basename "$0")"
action=
# a = -rlptgoD, u = update via timestamp, hence -t is necessary
# -FF: --filter=': /.rsync-filter' --filter='- .rsync-filter'
# Filter in $DOTFILES only applies for this script and .rsync-filter in source
# directories apply for possibly all rsyncs
base_cmd='rsync -Ca --no-D'
if [ -f "$DOTFILES/.bootstrap-filter" ]; then
base_cmd+=" -FF -f'. $DOTFILES/.bootstrap-filter' -f'- .bootstrap-filter'"
fi
gather_cmd="$base_cmd -k --existing"
deploy_cmd="$base_cmd -Kb --backup-dir=$backup_dir"
add_cmd="$base_cmd -k --ignore-existing"
dest_dir=$HOME
# TODO: make options, pathspec to array
options=''
pathspec=''
mkdir -p "$log_dir" "$backup_dir"
#=================================== Logging ===================================
# Backup stdout(&1) and stderr(&2) to fd 3 and fd 4
exec 3>&1 4>&2
# Restore stdout and stderr
trap 'exec 2>&4 1>&3' 0 1 2 3
# Use tee to redirect fd 1 to logfile.out and to stdout
exec 1> >(tee "${log_prefix}.out.log" >&3)
# Use tee to redirect fd 2 to logfile.err and to stderr
exec 2> >(tee "${log_prefix}.err.log" >&4)
parse_args() {
[ -z "$DOTFILES" ] && fail 'DOTFILES not set.'
info 'Parsing user input'
# Parse command
for i in "$@"
do
case "$i" in
u|update)
action=update
;;
g|gather)
action=gather
;;
d|deploy)
action=deploy
;;
a|add)
action=add
;;
-*)
options+=" $i"
;;
*)
pathspec+=" $i"
;;
esac
done
}
do_action() {
info "Doing action \"$action\" with options:\"$options\" ..."
if [ -z $action ]; then
fail 'No action.'
fi
eval "$action"
info "See the logs in $log_dir."
if [ -z "$(ls -A "$backup_dir")" ]; then
success "No backups created in $backup_dir."
else
# TODO: improve the output and show changes, filter not existing files
warning "Backups were created in $backup_dir. Please check:"
if type colordiff >/dev/null 2>&1; then
colordiff -rw --exclude .git "$backup_dir" "$DOTFILES"
else
diff -rw --exclude .git "$backup_dir" "$DOTFILES"
fi
fi
}
# Synchronize two directories by taking the newest file in case of conflict.
update() {
eval "$gather_cmd -u $options $dest_dir/ $DOTFILES"
eval "$deploy_cmd -u $options $DOTFILES/ $dest_dir"
success "Synchronized $dest_dir and $DOTFILES."
}
# Collect all relevant files to the backup directory and overwrite it with the
# content from the working directory.
gather() {
eval "$gather_cmd $options $dest_dir/ $DOTFILES"
success "Gathered $dest_dir to $DOTFILES."
}
# Put all backup files to the working directory and overwrite them. This is
# useful while installing.
deploy() {
eval "$deploy_cmd $options $DOTFILES/ $dest_dir"
success "Deployed $DOTFILES to $dest_dir"
}
add() {
# TODO: make pathspec to array
local src_files=($(realpath -s $pathspec))
for src in "${src_files[@]}"; do
dest=${src/$dest_dir/$DOTFILES}
mkdir -p "$(dirname "$dest")"
eval "$add_cmd $options $src $dest"
success "Added $src to $dest"
git -C "$DOTFILES" add "$dest"
done
}
success () {
printf "[%bOK%b] $1\n" "$green" "$reset"
}
info () {
printf "[%bINFO%b] $1\n" "$blue" "$reset"
}
warning () {
printf "[%bWARNING%b] $1\n" "$yellow" "$reset"
}
fail () {
printf "[%bFAIL%b] $1\n" "$red" "$reset"
exit
}
user () {
printf "[%bINPUT%b] $1\n" "$cyan" "$reset"
}
parse_args "$@"
do_action