-
Notifications
You must be signed in to change notification settings - Fork 1
/
symvol.sh
executable file
·196 lines (183 loc) · 5.79 KB
/
symvol.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
#!/bin/bash
set -e
##
## SymVol v0.0.2
## -------------
## by Francesco Bianco
## info@javanile.org
## MIT License
##
WORKDIR="$(echo "${PWD}")"
TEMPTAR=".symvol.${RANDOM}.tar"
## TODO: use 'getopt' ref: https://stackoverflow.com/a/39376824/999329
## Print message
symvol_info () {
echo -e "\e[1m>>> $1\e[0m"
return 0
}
## Print error and exit
symvol_exit () {
echo -e "\e[31m>>> $2\e[0m"
exit $1
}
## Validate source argument
symvol_validate_source () {
[[ -z "$1" ]] && symvol_exit 1 "Missing source directory."
[[ ! -d "$1" ]] && symvol_exit 1 "Source '$1' seem not a directory."
[[ ! -f "$1/.symvol" ]] && symvol_exit 1 "Missing '.symvol' file in '$1' source directory."
[[ ".symvol" != "$(head -n 1 $1/.symvol)" ]] && symvol_exit 1 "Missing '.symvol' as first line of '.symvol' file."
return 0
}
## Validate target argument
symvol_validate_target () {
[[ -z "$1" ]] && symvol_exit 1 "Missing target directory"
[[ ! -d "$1" ]] && symvol_exit 1 "Target '$1' seem not a directory"
return 0
}
## Move source files to target directory
symvol_move () {
symvol_validate_source $1
symvol_validate_target $2
symvol_info "move: '$1' to '$2'"
cd $1;
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ -h "$(realpath -qs ${item})" ]] && continue
[[ ! -f "${item}" ]] && [[ ! -d "${item}" ]] && continue
echo "${item}"
tar -uf ${TEMPTAR} ${item}
rm -fr ${item}
done < .symvol
cd ${WORKDIR}
if [[ -f $1/${TEMPTAR} ]]; then
mv $1/${TEMPTAR} $2
cd $2
tar -xf ${TEMPTAR}
rm -f ${TEMPTAR}
fi
return 0
}
## Safe copy source files to target directory
symvol_copy () {
symvol_validate_source $1
symvol_validate_target $2
[[ -f "$2/.symvol" ]] && symvol_exit 0 "Copy from '$1' was stopped because target '$2' is not empty."
symvol_info "copy: '$1' to '$2'"
cd $1;
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ -h "$(realpath -qs ${item})" ]] && continue
[[ ! -f "${item}" ]] && [[ ! -d "${item}" ]] && continue
tar -uf ${TEMPTAR} ${item}
done < .symvol
cd ${WORKDIR}
if [[ -f $1/${TEMPTAR} ]]; then
mv $1/${TEMPTAR} $2
cd $2
tar -xf ${TEMPTAR}
rm -f ${TEMPTAR}
fi
return 0
}
## Force copy source files to target directory
symvol_push () {
symvol_validate_source $1
symvol_validate_target $2
symvol_info "push: '$1' to '$2'"
cd $1;
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ -h "$(realpath -qs ${item})" ]] && continue
[[ ! -f "${item}" ]] && [[ ! -d "${item}" ]] && continue
tar -uf ${TEMPTAR} ${item}
done < .symvol
cd ${WORKDIR};
if [[ -f $1/${TEMPTAR} ]]; then
mv $1/${TEMPTAR} $2
cd $2;
tar -xf ${TEMPTAR}
rm -f ${TEMPTAR}
fi
return 0
}
## Create links from source to target
symvol_link () {
symvol_validate_source $1
symvol_validate_target $2
symvol_info "link: '$1' to '$2'"
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ ! -f "$1/${item}" ]] && [[ ! -d "$1/${item}" ]] && continue
[[ -h "$(realpath -qs $2/${item})" ]] && continue
[[ -L "$2/${item}" ]] && continue
echo ${item}
mkdir -p $(dirname $2/${item}) && true
ln -s $(readlink -f $1/${item}) $(readlink -f $2/${item})
done < $1/.symvol
return 0
}
## Change mode of source files and symlinks
symvol_mode () {
symvol_validate_source $1
symvol_info "mode: '$1'"
[[ -z "$2" ]] && symvol_exit 1 "Missing 'user:group' mode."
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ ! -f "$1/${item}" ]] && [[ ! -d "$1/${item}" ]] && continue
echo ${item}
chmod 777 -R $1/${item}
chown -h -R $2 $(realpath -s $1/${item})
chown -R $2 $1/${item}
done < $1/.symvol
return 0
}
## Remove source files and symlinks
symvol_drop () {
symvol_validate_source $1
symvol_info "drop: '$1'"
while IFS= read item || [[ -n "${item}" ]]; do
[[ -z "${item}" ]] && continue
[[ "${item::1}" == "#" ]] && continue
[[ ! -f "$1/${item}" ]] && [[ ! -d "$1/${item}" ]] && continue
echo ${item}
rm -fr $(realpath -s $1/${item})
done < $1/.symvol
return 0
}
## Show help
symvol_help () {
echo "==== SymVol ===="
echo "Manage symbolic links to create persisten volume on docker contanier"
echo ""
echo " symvol [move|copy|push|link] SOURCE TARGET"
echo " symvol [mode] SOURCE USER:GROUP"
echo " symvol [drop] SOURCE"
echo " symvol [help]"
echo ""
echo " move Move SOURCE files to TARGET directory"
echo " copy Safe copy SOURCE files to TARGET directory"
echo " push Force copy SOURCE files to TARGET directory"
echo " link Link SOURCE files to TARGET directory"
echo " mode Change mode of SOURCE files or symlinks"
echo " drop Delete SOURCE files or symlinks"
echo " help Show this help"
echo ""
echo "More info at https://github.com/javanile/symvol"
}
## Entrypoint
case $1 in
drop) symvol_drop $2; ;;
move) symvol_move $2 $3; ;;
copy) symvol_copy $2 $3; ;;
push) symvol_push $2 $3; ;;
link) symvol_link $2 $3; ;;
mode) symvol_mode $2 $3; ;;
help) symvol_help; ;;
"") symvol_exit 1 "Require command: use 'symvol help'."; ;;
*) symvol_exit 1 "Unknown command: use 'symvol help'."; ;;
esac