This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metabox
executable file
·254 lines (223 loc) · 6.06 KB
/
metabox
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# Set overrideable local files and folders.
LOCAL_ARCHIVES_DIR="data/archives"
LOCAL_EXTRACTS_DIR="data/extracts"
LOCAL_RECORDS_DB="versions.txt"
# Set overrideable s3 destinations.
AWS_S3_BUCKET="default"
AWS_S3_PREFIX="prefix"
# If found on .env, override the variables declared above.
set -a; source .env; set +a
readonly AWS_CLI="docker run --rm -v $(pwd)/$LOCAL_ARCHIVES_DIR:/$AWS_S3_PREFIX -u $(id -u):$(id -g) --env-file .env -it amazon/aws-cli:latest"
# Reset AWS archives.
# $AWS_CLI s3 rm "s3://$AWS_S3_BUCKET/$AWS_S3_PREFIX" --recursive
# exit 1
function list_s3_files() {
$AWS_CLI s3 ls "s3://$AWS_S3_BUCKET/$AWS_S3_PREFIX/"
}
function upload_to_s3() {
$AWS_CLI s3 cp "/$AWS_S3_PREFIX/$1.tar.gz" "s3://$AWS_S3_BUCKET/$AWS_S3_PREFIX/"
}
function download_from_s3() {
$AWS_CLI s3 cp "s3://$AWS_S3_BUCKET/$AWS_S3_PREFIX/$1.tar.gz" "/$AWS_S3_PREFIX/$1.tar.gz"
}
# RECORD struct
# hash (hash ./target)
# timestamp (date +%s)
# creator (git config --global user.email)
# tags (<comma-delimited array of strings>)
# helper: git rev-parse --abbrev-ref HEAD
#
# For example,
# 67dc74329181f3aaf1d304a4fc40c01c 1588448900 [email protected] development,master
function record_exists() {
local sum=$1
return $(grep -q "^$sum " $LOCAL_RECORDS_DB)
}
function record_retrieve() {
local sum=$1
echo $(grep "^$sum " $LOCAL_RECORDS_DB)
}
function record_create() {
record_exists "$1" && echo "Record already exists: $1" && exit
echo "$1 $2 $3 $4" >> $LOCAL_RECORDS_DB
}
function record_get_tags() {
# Converts comma into space, then let default IFS convert the
# 4th parameter into an array.
echo ${4//,/ }
}
function record_print() {
echo "$1"
if [ "$(uname)" == "Darwin" ]; then
echo " created: $(date -d $2)"
else
echo " created: $(date -d @$2)"
fi
echo " creator: $3"
local tags=($(record_get_tags $@))
echo " tags: (${tags[*]})"
}
function array_contains () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
function print_all_records() {
while read line; do
record_print $line
done <$LOCAL_RECORDS_DB
}
function latest_record_with_tag() {
local record=""
while read line; do
local tags=$(record_get_tags $line)
array_contains "$1" $tags && record=$line
done <$LOCAL_RECORDS_DB
if [ -z "$record" ]; then
return 1
fi
echo $record
}
function tag_record() {
local record=$(record_retrieve "$1")
if [ -z "$record" ]; then
echo "Record does not exist: $1"
exit 1
fi
local tags=($(record_get_tags $record))
array_contains "$2" ${tags[*]}
if [ "$?" -eq 0 ]; then
echo "Record already has tag: $2"
exit 1
fi
local prev_tags=$(local IFS=, ; echo "${tags[*]}")
local appended=(${tags[*]})
appended+=($2)
local next_tags=$(local IFS=, ; echo "${appended[*]}")
sed -ri "s/^($1.+)$prev_tags$/\1$next_tags/g" $LOCAL_RECORDS_DB
}
function untag_record() {
local record=$(record_retrieve "$1")
if [ -z "$record" ]; then
echo "Record does not exist: $1"
exit 1
fi
local tags=($(record_get_tags $record))
array_contains "$2" ${tags[*]}
if ! [ "$?" -eq 0 ]; then
echo "Record doesn't have tag: $2"
exit 1
fi
local prev_tags=$(local IFS=, ; echo "${tags[*]}")
local array=()
for value in "${tags[@]}"; do
[[ $value != $2 ]] && array+=($value)
done
local next_tags=$(local IFS=, ; echo "${array[*]}")
sed -ri "s/^($1.+)$prev_tags$/\1$next_tags/g" $LOCAL_RECORDS_DB
}
function hash() {
local hasher=md5sum
# Handle MacOS using md5 insted of md5sum.
if [ "$(uname)" == "Darwin" ]; then
hasher=md5
fi
local target=${1:-$(pwd)}
if ! [ -d "$target" ]; then
echo "Unknown folder: $target"
exit 1
fi
pushd $(pwd) > /dev/null
cd $target
local folder=${target##*/}
local sum=`{
export LC_ALL=C;
echo $folder;
find -type f -exec wc -c {} \; | sort; echo;
find -type f -exec $hasher {} + | sort; echo;
find . -type d | sort; find . -type d | sort | $hasher;
} | $hasher | awk -F" " '{ print $1 }'`
popd > /dev/null
echo $sum
}
function archive() {
local target=${1:-$(pwd)}
if ! [ -d "$target" ]; then
echo "Unknown folder: $target"
exit 1
fi
local sum=$(hash $target)
mkdir -p $LOCAL_ARCHIVES_DIR
local folder=${target##*/}
tar cvzf "$LOCAL_ARCHIVES_DIR/$sum.tar.gz" -C $target/.. $folder
record_create $sum $(date +%s) $(git config --global user.email) $2
echo $sum
}
function unarchive() {
local sum="$1"
local filename="$LOCAL_ARCHIVES_DIR/$sum.tar.gz"
if ! [ -f "$filename" ]; then
echo "File not found: $filename"
exit 1
fi
if ! [ -z $2 ]; then
rm -rf "$LOCAL_EXTRACTS_DIR/$2"
mkdir -p "$LOCAL_EXTRACTS_DIR/$2"
tar xzf "$filename" -C "$LOCAL_EXTRACTS_DIR/$2" --strip-components 1
else
mkdir -p $LOCAL_EXTRACTS_DIR
tar xzf "$filename" -C "$LOCAL_EXTRACTS_DIR"
fi
echo $sum
}
# Setup.
touch $LOCAL_RECORDS_DB
mkdir -p $LOCAL_ARCHIVES_DIR
mkdir -p $LOCAL_EXTRACTS_DIR
# Get command.
CMD=$1
shift
# Select command.
case $CMD in
"hash")
hash $@
;;
"arc")
archive $@
;;
"unarc"|"dearc")
unarchive $@
;;
"push")
archive $@ && upload_to_s3 $(hash $1)
;;
"pull")
download_from_s3 $1 && unarchive $@
;;
"remotes")
list_s3_files
;;
"list")
# print all from LOCAL_RECORDS_DB
print_all_records
;;
"tagged")
record=$(latest_record_with_tag $1)
if ! [ "$?" -eq 0 ]; then
echo "No records with tag: $1"
exit 1
fi
record_print $record
;;
"tag")
tag_record $1 $2
;;
"untag"|"detag")
untag_record $1 $2
;;
*)
echo "default"
;;
esac