-
Notifications
You must be signed in to change notification settings - Fork 0
/
stowman.sh
executable file
·191 lines (162 loc) · 4.11 KB
/
stowman.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
#!/usr/bin/env bash
set -euo pipefail
DOTDIR="${STOWMAN_DOTDIR:-$HOME/.dotfiles}"
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
PINK='\033[0;35m'
RED='\033[0;31m'
NC='\033[0m'
stowcmd="stow -d $DOTDIR -t $HOME -v"
gitcmd="git -C $DOTDIR"
function push() {
msg="$1"
if [[ -z "$msg" ]]; then
msg=$(eval "$gitcmd status --porcelain | awk '{print $2}' | cut -d'/' -f1-2 | sort -u | xargs")
fi
eval "$gitcmd add ."
eval "$gitcmd commit -am '$msg'"
eval "$gitcmd push"
}
function maybeCreateDir() {
mkdir -p "$DOTDIR" 2>/dev/null
}
function init() {
if [[ -z "$1" ]]; then
usage
return
fi
maybeCreateDir
git clone "$1" "$DOTDIR"
}
function pull() {
eval "$gitcmd pull"
}
function reload() {
if [[ -z "$1" ]]; then
usage
return
fi
if [[ $1 = "all" ]]; then
for i in "$DOTDIR"/*; do
if [[ ! -d "$i" ]]; then continue; fi
i="${i/$DOTDIR\/''/}"
echo -e "Reloading ${BLUE}${i%%/}${NC}"
eval "$stowcmd ${i%%/}"
done
else
echo -e "Reloading ${BLUE}$1${NC}"
eval "$stowcmd $1"
fi
}
function add() {
if [[ -z "$1" || -z "$2" ]]; then
usage
return
fi
src=$1
pkg=$2
if [[ ! -e "$DOTDIR" ]]; then
echo -e "$DOTDIR ${RED}not found!${NC}. Please run ${BLUE}init${NC} first."
fi
if [[ "$src" = "." ]]; then
src=$(pwd)
fi
if [[ "$src" != /* ]]; then
src=$(pwd)/$src
fi
what=$src
what=$(echo "$what" | sed -e "s/~\///g")
what=$(echo "$what" | sed -e "s/\/home\/$(whoami)\///g")
if [[ ! -e "$src" ]]; then
echo
echo -e "$src ${RED}not found!${NC}"
return
fi
echo -e "${BLUE}From:\t${NC} $src"
echo -e "${BLUE}To:\t${NC} $DOTDIR/${PINK}$pkg${NC}/$what"
read -r -p "Continue [Y/n]" response
if [[ "$response" =~ ^[Nn]$ ]]; then
echo "Aborted."
else
if [[ ! -e "$DOTDIR/$pkg" ]]; then
mkdir -p "$DOTDIR/$pkg"
fi
mv "$src" "$DOTDIR/$pkg/$what"
eval "$stowcmd $pkg"
fi
}
function usage() {
echo -e "Invalid operation. Use stowman.sh ${PINK}--help${NC} for help."
}
function help() {
cat <<EOF
_==_ _
_,(",)|_|
\/. \-| stowman.sh
__( : )|_ Manage your dotfiles easily.
EOF
gv=$(git --version 2>/dev/null)
sv=$(stow --version 2>/dev/null)
ddir="${GREEN}$DOTDIR${NC}"
if [[ ! -e "$DOTDIR" ]]; then
ddir="${RED}$DOTDIR not found${NC}"
fi
if [[ -z $gv ]]; then
gv="${RED}not found"
else
gv=$(echo "$gv" | cut -d" " -f3)
fi
if [[ -z $sv ]]; then
sv="${RED}not found"
else
sv=$(echo "$sv" | cut -d" " -f5)
fi
echo -e "git: \t${GREEN}$gv${NC}\tstow: \t${GREEN}$sv${NC} \t dots: ${ddir}"
echo
echo -e "${BLUE}Usage:${NC}"
echo -e "stowman.sh ${PINK}init <repo>${NC} \t${BLUE}Initialize a new config${NC}"
echo -e "stowman.sh ${PINK}add <src> <pkg>${NC} \t${BLUE}Adds a file/folder to a specific package${NC}"
echo -e "stowman.sh ${PINK}reload <pkg>|all${NC} \t${BLUE}Applies changes to a specific package or all packages${NC}"
echo -e "stowman.sh ${PINK}push${NC} \t\t${BLUE}Push changes to the repository${NC}"
echo -e "stowman.sh ${PINK}pull${NC} \t\t${BLUE}Pull changes from the repository${NC}"
echo
echo -e "${BLUE}Initializing a new config${NC}"
echo -e "stowman.sh ${PINK}init${NC} git@github.com:user/repo.git"
echo
echo -e "${BLUE}Adding new files or folders${NC}"
echo -e "stowman.sh ${PINK}add${NC} ~/.config/nvim packagename"
echo -e "stowman.sh ${PINK}add${NC} . packagename"
echo
echo -e "${BLUE}Reloading changes${NC}"
echo -e "stowman.sh ${PINK}reload${NC} all"
echo -e "stowman.sh ${PINK}reload${NC} packagename"
echo
}
if [[ ! -e "$DOTDIR" ]]; then
mkdir -p "$DOTDIR"
echo -e "Created: ${YELLOW}$DOTDIR${NC}"
fi
case $1 in
add)
add "$2" "$3"
;;
push)
push "$2"
;;
pull)
pull
;;
reload)
reload "$2"
;;
init)
init "$2"
;;
--help)
help
;;
*)
usage
;;
esac