-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotfiles.sh
executable file
·234 lines (193 loc) · 5.52 KB
/
dotfiles.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bash
export STOW_DIR=$HOME/.dotfiles
export FOLDERS_FILE=dotfiles_folders
get_stow_folders() {
cat $FOLDERS_FILE
}
set_stow_folders() {
echo ${@} > $FOLDERS_FILE
}
stowFolders="$(get_stow_folders)"
stowFlags=""
personal=0
addFolder=
addFile=
_help() {
echo """
COMMAND
dotfiles [setup|remove|update] [-d|--dry]
dotfiles [add|delete] FOLDER FILE
dotfiles [add-folder|delete-folder] FOLDER
dotfiles [-h|--help|help]
DESCRIPTION
dotfiles command is supposed to be used to backup and sync your dotfiles using git & stow.
OPTIONS
setup, remove, update [-d|--dry]
Pretty self explanatory.
add FOLDER FILE
Add an existing FILE. Will move FILE to the FOLDER in dotfiles with correct path and link it.
delete FOLDER FILE
Stop tracking FILE. Will move the FILE to its location and restow the FOLDER removing the linking.
add-folder FOLDER
Adds FOLDER to dotfiles_folders
delete-folder FOLDER
Delete FOLDER from dotfiles_folders
-h, --help, help
Will print this.
-d, --dry
If present, this only does a dry run and outputs what will happen.
"""
}
_setup() {
echo Setting up - $stowFolders
stow $stowFlags --adopt -S $stowFolders
echo "✨ All done"
}
_remove() {
echo Removing - $stowFolders
stow $stowFlags -D $stowFolders
echo "✨ All done"
}
_update() {
echo Updating - $stowFolders
stow $stowFlags --adopt -R $stowFolders
echo "✨ All done"
}
createIfNotExists() {
test -d ${1} || mkdir -p ${1}
}
addDotfilesFolder() {
local dotfilesFolders=$(get_stow_folders)
if [[ $dotfilesFolders == *"${1}"* ]];
then
echo ${1} already present in $FOLDERS_FILE
else
set_stow_folders "$(get_stow_folders) ${1}"
echo ${1} added in $FOLDERS_FILE
fi
}
removeDotfilesFolder() {
local dotfilesFolders=($(get_stow_folders))
local toDelete=${1}
if [[ $dotfilesFolders == *"${1}"* ]];
then
set_stow_folders "${dotfilesFolders[@]/$toDelete}"
echo ${1} removed from $FOLDERS_FILE
else
echo ${1} not in $FOLDERS_FILE
fi
}
_add() {
if [[ -z $addFile ]]; then echo Require the FILE argument && exit 1; fi;
if [[ -z $addFolder ]]; then echo Require the FOLDER argument && exit 1; fi;
local relativePathToHome=$(realpath --relative-to="$HOME" $addFile)
local filePath=$HOME/$relativePathToHome
if ! [[ -f $filePath ]]; then echo File $filePath does not exist && exit 1; fi;
local moveTo=$STOW_DIR/$addFolder/$relativePathToHome
# Move the file in the dotfiles
echo Moving $filePath to $moveTo
createIfNotExists $(dirname $moveTo)
mv $filePath $moveTo
# Setup that folder to link the file
addDotfilesFolder $addFolder
stowFolders=($(echo $addFolder))
_setup
}
_delete() {
if [[ -z $addFile ]]; then echo Require the FILE argument && exit 1; fi;
if [[ -z $addFolder ]]; then echo Require the FOLDER argument && exit 1; fi;
local filePath=$STOW_DIR/$addFolder/$addFile
if ! [[ -f $filePath ]]; then echo File $filePath does not exist && exit 1; fi;
local relativePathToHome=$(realpath --relative-to="$STOW_DIR/$addFolder" $filePath)
local moveTo=$HOME/$relativePathToHome
# Move the file from dotfiles to correct path
if [[ -f $filePath ]]; then echo Deleting $moveTo; rm $moveTo; fi;
echo Moving $filePath to $moveTo
mv $filePath $moveTo
# Setup that folder once again
stowFolders=($(echo $addFolder))
_setup
}
_addFolder() {
addDotfilesFolder $addFolder
# Setup that folder to link the file
stowFolders=($(echo $addFolder))
_setup
}
_deleteFolder() {
removeDotfilesFolder $addFolder
# Remove that folder to unlink files
stowFolders=($(echo $addFolder))
_remove
}
main() {
local position=0
local fnToRun="_help"
while [[ "${#}" -gt 0 ]]; do
case "${1}" in
-h|--help|help)
_help
exit 0
;;
setup)
fnToRun="_setup"
shift
;;
remove)
fnToRun="_remove"
shift
;;
update)
fnToRun="_update"
shift
;;
add)
fnToRun="_add"
shift
;;
delete)
fnToRun="_delete"
shift
;;
add-folder)
fnToRun="_addFolder"
shift
;;
delete-folder)
fnToRun="_deleteFolder"
shift
;;
-f|--folders)
folders=${2}
stowFolders=($(echo $folders | sed "s/,/ /g"))
shift 2
;;
-p|--personal)
personal=1
shift
;;
-d|--dry)
stowFlags="-nv"
shift
;;
*)
case "${position}" in
0)
addFolder="${1}"
position=1
shift
;;
1)
addFile="${1}"
position=2
shift
;;
esac
;;
esac
done
# echo Running $fnToRun with folders=$stowFolders flags=$stowFlags addFolder=$addFolder addFile=$addFile
$fnToRun # Run whatever needs to run
return 0
}
main "${@:-}"