-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapdir-sqlite3-catalog
executable file
·560 lines (513 loc) · 20.3 KB
/
snapdir-sqlite3-catalog
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env bash
# # snapdir-sqlite3-catalog
#
# Logs manifest and push events to a sqlite3 database and allows
# basic querying of the database.
#
# ## Background:
#
# This is Reference implementation of snapdir catalog using
# a local sqlite3 database. The methods in this file are
# called by the snapdir script.
#
# ## Usage
#
# snapdir-sqlite3-catalog [OPTIONS] [SUBCOMMAND]
#
# ### Options
#
# --event=name Event name that triggered a log entry.
# --debug Enable debug output.
# --help, -h Prints help message.
# --id=ID Manifest ID to use.
# --location=DIR|STORE Location for catalog queries.
# --verbose Enable verbose output.
# --version, -v Prints version.
#
# ### Commands
#
# ancestors --id= Get a list of ancestor snapdir IDs their location.
# help [COMMAND] Prints help information.
# locations Lists directories and stores where snapshots
# have been taken or published.
# log --id= --event= --location= Saves an event. Calls save under the hood.
# revisions --location= Get a list of snapdir IDs created on a
# location (store or abs path).
# save --id= --location= Saves an entry and sets it's ancestor.
# test Runs unit tests.
# version Prints the version.
#
# ### Environment variables
#
# SNAPDIR_SQLITE3_BIN Path to sqlite3 binary with json support.
# SNAPDIR_SQLITE3_CATALOG_DB_PATH Path where the database will be created.
# Defaults to ~/.snapdir/catalog-production.sqlite3.db.
# ### Examples
#
# # Saves a log entry to the database for newly generated manifest.
# snapdir-sqlite3-catalog log --event "manifest" --id "${SNAP_MANIFEST_ID}" --location "/some/dir"
#
# # Saves a log entry to the database for newly pushed manifest.
# snapdir-sqlite3-catalog log --event "push" --id "${SNAP_MANIFEST_ID}" --location "s3://some-bucket/"
#
# # Lists all locations and stores where snapshots have been taken or published.
# snapdir-sqlite3-catalog locations
#
# # shows all the ancestors of a given snapdir ID.
# snapdir-sqlite3-catalog ancestors --id="${SNAPDIR_ID}"
#
# # shows all ancestors for a given snapdir ID in a given location.
# snapdir-sqlite3-catalog ancestors --id="${SNAPDIR_ID}" --location="s3://some-bucket/"
#
# # Gets a list of revisions stored on a store
# snapdir-sqlite3-catalog revisions --location="s3://my-bucket/some/path"
#
# # Gets a list of revisions stored on a local directory
# snapdir-sqlite3-catalog revisions --location="/home/user/some/path"
#
# LICENSE: MIT Copyright (c) 2022 Bermi Ferrer
set -eEuo pipefail
IFS=$'\n\t'
snapdir_sqlite3_catalog_log() {
# Receives a log message from a a snapdir event.
#
# This is the only write interface for the catalog.
# Called after manifest generation and store pushing.
#
# Usage:
#
# snapdir-sqlite3-catalog log \
# --event="$EVENT_NAME" \
# --location="${LOCATION}" \
# --id="${ID}"
#
set -eEuo pipefail
local event="${_SNAPDIR_SQLITE3_CATALOG_EVENT:?Missing --event}"
local id="${_SNAPDIR_SQLITE3_CATALOG_ID:?Missing --id}"
local location="${_SNAPDIR_SQLITE3_CATALOG_LOCATION:?Missing --location}"
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
INSERT INTO snapdir_event_log (event, id, location) VALUES ('${event}', '${id}', '${location}');
EOF
snapdir_sqlite3_catalog_save "${location}" "$id"
}
snapdir_sqlite3_catalog_save() {
# Saves an entry on the snapdir_history table.
#
# This is not called directly by snapdir but is called
# the snapdir_sqlite3_catalog_log function on a new subshell.
#
# Usage:
#
# snapdir-sqlite3-catalog save \
# --location="${LOCATION}" \
# --id="${ID}"
#
set -eEuo pipefail
local location="${1:-${_SNAPDIR_SQLITE3_CATALOG_LOCATION:?Missing --location}}"
local id="${2:-${_SNAPDIR_SQLITE3_CATALOG_ID:?Missing --id}}"
local previous_id
previous_id="$(
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
SELECT id FROM snapdir_history
WHERE location='${location}'
ORDER BY created_at DESC LIMIT 1;
EOF
)"
# subsequent snapdir_history will not be logged
if [[ ${previous_id} == "${id}" ]]; then
return 0
fi
previous_id="${previous_id:-"NULL"}"
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
INSERT INTO snapdir_history
(location, id, previous_id)
VALUES
('$location', '$id', NULLIF('$previous_id', 'NULL'));
EOF
}
snapdir_sqlite3_catalog_locations() {
# Lists locations tracked by the catalog. These include local directories and stores.
#
# Usage:
#
# snapdir-sqlite3-catalog locations
#
# Returns: JSON lines of the form:
#
# {
# "created_at": "YYYY-MM-DD HH:MM:SS.SSS",
# "id": "${SNAPDIR_ID}",
# "location": "${ABSOLUTE_DIR_NAME_OR_STORE_URI}"
# }
#
# Example:
#
# snapdir-sqlite3-catalog locations
#
set -eEuo pipefail
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
SELECT json_object(
'created_at', STRFTIME('%Y-%m-%d %H:%M:%f', s1.created_at),
'id', s1.id,
'location', s1.location
)
FROM snapdir_history s1
LEFT JOIN snapdir_history s2
ON s1.location = s2.location
AND s1.created_at < s2.created_at
WHERE s2.id IS NULL
EOF
}
snapdir_sqlite3_catalog_ancestors() {
# Get a list of ancestor snapdir IDs and the location where they where created.
#
# Usage:
#
# snapdir-sqlite3-catalog ancestors \
# --id="${SNAPDIR_ID}" \
# [--location="${ABSOLUTE_DIR_NAME_OR_STORE_URI}"]
#
# Returns: JSON lines of the form:
#
# {
# "created_at": "YYYY-MM-DD HH:MM:SS.SSS",
# "id": "${PARENT_SNAPDIR_ID}",
# "location": "${ABSOLUTE_DIR_NAME_OR_STORE_URI}"
# }
#
# Examples:
#
# snapdir-sqlite3-catalog ancestors --id="${SNAPDIR_ID}"
# snapdir-sqlite3-catalog ancestors --id="${SNAPDIR_ID}" --location="s3://some-bucket/"
#
set -eEuo pipefail
local id="${_SNAPDIR_SQLITE3_CATALOG_ID:?Missing --id}"
local location="${_SNAPDIR_SQLITE3_CATALOG_LOCATION:-""}"
local location_condition=""
if [[ $location != "" ]]; then
location_condition=" AND location='${location//\'/\'\'}'"
fi
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
SELECT json_object(
'created_at', STRFTIME('%Y-%m-%d %H:%M:%f', created_at),
'id', previous_id,
'location', location
)
FROM snapdir_history WHERE (
id = '$id' AND previous_id IS NOT NULL $location_condition
) ORDER BY created_at DESC;
EOF
}
snapdir_sqlite3_catalog_revisions() {
# Get a list of snapdir IDs created on a specific location.
#
# Usage:
#
# snapdir-sqlite3-catalog revisions \
# --location="${ABSOLUTE_DIR_NAME_OR_STORE_URI}"
#
# Returns: JSON lines of the form:
#
# {
# "created_at": "YYYY-MM-DD HH:MM:SS.SSS",
# "id": "${SNAPDIR_ID}",
# "previous_id": "${PREVIOUS_SNAPDIR_ID}",
# "location": "${ABSOLUTE_DIR_NAME_OR_STORE_URI}"
# }
#
# Example2:
#
# # Gets a list of revisions stored on a store
# snapdir-sqlite3-catalog revisions --location="s3://my-bucket/some/path"
#
# # Gets a list of revisions stored on a local directory
# snapdir-sqlite3-catalog revisions --location="/home/user/some/path"
#
set -eEuo pipefail
local location="${_SNAPDIR_SQLITE3_CATALOG_LOCATION:?Missing --location}"
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
SELECT json_object(
'created_at', STRFTIME('%Y-%m-%d %H:%M:%f', created_at),
'id', id,
'previous_id', previous_id
)
FROM snapdir_history WHERE location='$location' ORDER BY created_at DESC;
EOF
}
_snapdir_sqlite3_catalog_run() (
set -eEuo pipefail
# Saves the event into the run log for debugging, documentation, etc.
if [[ ${ENVIRONMENT:-""} == "test" ]] && [[ ${_SNAPDIR_RUN_LOG_PATH:-""} != "" ]] && test -f "${_SNAPDIR_RUN_LOG_PATH:-""}"; then
# shellcheck disable=SC2145
echo "snapdir-sqlite3-catalog ${@}" >>"${_SNAPDIR_RUN_LOG_PATH}"
fi
local subcommands="save|revisions|locations|ancestors|log"
local boolean_args="debug|verbose"
local value_required_args="id|location|event"
local legal_argument_keys="${boolean_args}|${value_required_args}"
_snapdir_sqlite3_catalog_parse_argument_key() {
sed -E 's|^--?|_SNAPDIR_SQLITE3_CATALOG_|; s|-|_|g;' <<<"${1^^}"
}
_snapdir_sqlite3_catalog_validate_option() {
set -eEuo pipefail
grep -q -E "^_SNAPDIR_SQLITE3_CATALOG_(${legal_argument_keys^^})$" <<<"${1}" || {
echo "error: Unknown option: ${1//_SNAPDIR_SQLITE3_CATALOG_/}" | tr '[:upper:]' '[:lower:]' >&2
echo "Valid options are: --(${legal_argument_keys})" >&2
exit 1
}
}
_snapdir_sqlite3_catalog_ensure_db() {
set -eEuo pipefail
if test -f "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}"; then
# if "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" is older than "${_SNAPDIR_BIN_DIR}/snapdir"
# attempt to set the version
if [[ ${SNAPDIR_SQLITE3_CATALOG_DB_PATH} -ot "${_SNAPDIR_BIN_DIR}/snapdir" ]]; then
# Updates the version so that we can determine the
# version in which each snapdir_history entry was created.
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
INSERT OR IGNORE INTO snapdir_system (version) VALUES ("${_SNAPDIR_VERSION}");
EOF
fi
return 0
fi
mkdir -p "$(dirname "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}")"
# creates a sqlite3 db with the following tables:
# - snapdir_history: id, previous_id, dirname, created_at
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
CREATE TABLE snapdir_system (
version TEXT CHECK(length(version) < 16),
created_at DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),
PRIMARY KEY (version, created_at)
);
CREATE TABLE snapdir_event_log (
event TEXT NOT NULL CHECK(length(event) <= 128),
id TEXT NOT NULL CHECK(length(id) = 64),
location TEXT NOT NULL CHECK(length(location) < 4096),
created_at DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))
);
CREATE TABLE snapdir_history (
id TEXT NOT NULL CHECK(length(id) = 64),
previous_id TEXT CHECK(length(previous_id) = 64),
location TEXT NOT NULL CHECK(length(location) < 4096),
created_at DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))
);
EOF
# Sets the version of the db to help with upgrades
"${SNAPDIR_SQLITE3_BIN}" "${SNAPDIR_SQLITE3_CATALOG_DB_PATH}" <<-EOF
INSERT INTO snapdir_system (version) VALUES ("${_SNAPDIR_VERSION}");
EOF
}
_snapdir_sqlite3_catalog_help() {
_snapdir_sqlite3_catalog_export_env_defaults
local command="${1:-""}"
if [[ ${command} == "" ]]; then
sed '/# LICENSE: MIT Copyright (c) 2022 Bermi Ferrer/q; 1,2d' "$_SNAPDIR_SQLITE3_CATALOG_BIN_PATH" | sed -E 's|^# ?||g; $d' | more
else
_snapdir_command_help "snapdir_sqlite3_catalog_${command//-/_}" <"$_SNAPDIR_SQLITE3_CATALOG_BIN_PATH" | more
fi
exit 0
}
local command=""
local positional_args=""
local key
local value
local is_boolean
local subcommand_candidate="${1:-"$command"}"
local show_help=false
while [ $# -gt 0 ]; do
case "$1" in
save | revisions | locations | ancestors | log | test)
command="$1"
shift
;;
help | -h | --help)
show_help=true
shift
;;
version | -v | --version)
echo "${_SNAPDIR_VERSION}"
exit 0
;;
# export all --*=* flags as _SNAPDIR_SQLITE3_CATALOG_* env vars
--*=* | -*=*)
key="$(_snapdir_sqlite3_catalog_parse_argument_key "${1%%=*}")"
_snapdir_sqlite3_catalog_validate_option "$key"
export "$key"="${1#*=}"
shift
;;
# export all --* * flags as _SNAPDIR_SQLITE3_CATALOG_* env vars
--*)
is_boolean=$(grep -q -E "^--?(${boolean_args})$" <<<"${1}" && echo true || echo false)
key="$(_snapdir_sqlite3_catalog_parse_argument_key "${1}")"
_snapdir_sqlite3_catalog_validate_option "$key"
shift
value="${1:-true}"
# if key is in boolean_args
if [[ ${is_boolean} == "false" ]] && [[ ${value:0:1} != "-" ]]; then
# since this might be the last arg, this will always be truthy
shift || true
else
value="true"
fi
export "${key}"="${value}"
;;
*)
positional_args="${positional_args}${1} "
shift
;;
esac
done
if [[ ${show_help} == "true" ]]; then
_snapdir_sqlite3_catalog_help "$command"
fi
# if command is not set, show help
if [[ ${command:-""} == "" ]]; then
echo "Uknown command '$subcommand_candidate'. Valid commands are: ${subcommands}" >&2
echo "Try: snapdir-sqlite3-catalog --help" >&2
return 1
fi
_snapdir_sqlite3_catalog_export_env_defaults
# env | grep _snapdir_sqlite3_catalog_ | sort
eval "snapdir_sqlite3_catalog_${command//-/_} $positional_args ${*:2}"
)
_snapdir_sqlite3_catalog_export_env_defaults() {
# Environment variables
set -eEuo pipefail
local default_data_home="${XDG_DATA_HOME:-$HOME/.local/share}"
default_data_home="${default_data_home%/}"
local default_catalog_path="${default_data_home}/snapdir/catalog-${ENVIRONMENT:-production}.sqlite3.db"
SNAPDIR_SQLITE3_CATALOG_DB_PATH="${SNAPDIR_SQLITE3_CATALOG_DB_PATH:-$default_catalog_path}"
export SNAPDIR_SQLITE3_CATALOG_DB_PATH
_snapdir_sqlite3_catalog_ensure_db
}
# ####### ####### ##### ####### #####
# # # # # # # #
# # # # # #
# # ##### ##### # #####
# # # # # #
# # # # # # # #
# # ####### ##### # #####
# note: using subshell – '(' instead of '{' – to avoid leaking helper functions
snapdir_sqlite3_catalog_test() (
# Runs tests for the snapdir-sqlite3-catalog
#
# Usage:
#
# snapdir-sqlite3-catalog test
#
set -eEuo pipefail
# Import test utilities
# shellcheck disable=SC1091 source=./snapdir-test
. "${_SNAPDIR_BIN_DIR}/snapdir-test" "${_SNAPDIR_SQLITE3_CATALOG_BIN_PATH}"
teardown_suite() {
rm -rf "$_SNAPDIR_TEST_TMP_DIR/catalog"
}
test_suite() {
set -eEuo pipefail
local catalog="$_SNAPDIR_SQLITE3_CATALOG_BIN_PATH"
export SNAPDIR_SQLITE3_CATALOG_DB_PATH="${_SNAPDIR_TEST_TMP_DIR}/catalog.db"
local result
_snapdir_sqlite3_catalog_export_env_defaults
# --------------------------------------------------------------------------------
# log events
# --------------------------------------------------------------------------------
describe "log"
check "should prevent invalid id"
result=$("$catalog" log --event "manifest" --location "/foo/bar" --id "invalid" 2>&1 || echo "")
grep -E -q "CHECK.+failed" <<<"$result" || fail "Expected error message for id. Got: '$result'" && pass
check "should save event"
result=$("$catalog" log --event "manifest" --location "s3://foo" --id "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 2>&1 || echo "")
test "$result" == "" || fail "Unexpected output: '$result'"
test "$("$SNAPDIR_SQLITE3_BIN" "$SNAPDIR_SQLITE3_CATALOG_DB_PATH" "select event,id,location from snapdir_event_log")" == "manifest|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|s3://foo" && pass
# --------------------------------------------------------------------------------
"$catalog" log --event "manifest" --location "s3://bar" --id "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"$catalog" log --event "manifest" --location "/local/foo" --id "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"$catalog" log --event "manifest" --location "/local/foo" --id "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"$catalog" log --event "manifest" --location "/local/foo" --id "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
"$catalog" log --event "manifest" --location "s3://bar" --id "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
# --------------------------------------------------------------------------------
local locations
locations="$("$catalog" locations)"
describe "locations"
check "include location names"
grep -q "s3://foo" <<<"$locations" || fail "Expected location 's3://foo'. Got: '$locations'"
grep -q "s3://bar" <<<"$locations" || fail "Expected location 's3://foo'. Got: '$locations'"
grep -q "/local/foo" <<<"$locations" || fail "Expected location 's3://foo'. Got: '$locations'" && pass
check "include latest ids"
grep "s3://foo" <<<"$locations" | grep -q "aaaaaa" || fail "Expected location 's3://foo' to have ID aaaaaa.... Got: '$locations'"
grep "s3://bar" <<<"$locations" | grep -q "cccccc" || fail "Expected location 's3://bar' to have ID cccc.... Got: '$locations'"
grep "/local/foo" <<<"$locations" | grep -q "cccccc" || fail "Expected location '/local/foo' to have ID cccc.... Got: '$locations'" && pass
check "total number of locations"
test "$(echo "$locations" | grep -c .)" == "3" || fail "Found more locations than expected." && pass
describe "ancestors"
check "should return empty list for a root id"
result="$("$catalog" ancestors --id aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)"
test "$result" == "" || fail "Unexpected output: '$result'" && pass
result="$("$catalog" ancestors --id cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)"
check "should include all ancestors"
grep "s3://bar" <<<"$result" | grep -q "aaaaaaa" || fail "Expected ancestor 'aaaaaa...'. Got: '$result'"
grep "/local/foo" <<<"$result" | grep -q "bbbbbbb" || fail "Expected ancestor 'bbbb...'. Got: '$result'"
test "$(echo "$result" | grep -c .)" == "2" || fail "Found more ancestors than expected."
pass
result="$("$catalog" ancestors --location "s3://bar" --id cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)"
check "should include only ancestors in a given location"
grep "s3://bar" <<<"$result" | grep -q "aaaaaaa" || fail "Expected ancestor 'aaaaaa...'. Got: '$result'"
test "$(echo "$result" | grep -c .)" == "1" || fail "Found more ancestors than expected."
pass
describe "revisions"
check "should not return revisions for an invalid location"
result="$("$catalog" revisions --location "/not/avail" 2>&1 || echo "")"
test "$result" == "" || fail "Unexpected output: '$result'" && pass
check "should return revisions for a valid location"
result="$("$catalog" revisions --location "s3://bar" 2>&1 || echo "")"
grep 'id":"ccc' <<<"$result" | grep -q 'previous_id":"aaaaa' || fail "Expected id 'ccc...' with previous_id 'aaa...'. Got: '$result'"
grep 'id":"aaa' <<<"$result" | grep -q 'previous_id":null' || fail "Expected id 'aaa...' with previous_id 'null'. Got: '$result'"
test "$(echo "$result" | grep -c .)" == "2" || fail "Found more revisions than expected."
pass
}
run_tests
# run_tests_without_teardown
)
if [[ "$(uname -s)" == "Darwin" ]]; then
_snapdir_sqlite3_catalog_readlink() {
echo "$(cd "$(dirname "$1")" || echo "" && pwd)/$(basename "$1")"
}
else
_snapdir_sqlite3_catalog_readlink() {
readlink -f "$1"
}
fi
#######
# # # ##### ##### # # ##### #### # # # #####
# ## # # # # # # # # # # # ## # #
##### # # # # # # # # # # # # # # # #
# # # # # ##### # ##### # # # # # # #
# # ## # # # # # # # # # ## #
####### # # # # # # # #### # # # #
# Run if is not sourced
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
# Get the absolute path to ${BASH_SOURCE[0]}
export _SNAPDIR_SQLITE3_CATALOG_BIN_PATH="${_SNAPDIR_SQLITE3_CATALOG_BIN_PATH:-$(_snapdir_sqlite3_catalog_readlink "${BASH_SOURCE[0]}")}"
# import snapdir functions and environment variables,
# we'll need them to resolve location, logging and testing.
_SNAPDIR_BIN_PATH="$(dirname "${_SNAPDIR_SQLITE3_CATALOG_BIN_PATH}")/snapdir"
SNAPDIR_SQLITE3_BIN="${SNAPDIR_SQLITE3_BIN:-$(command -v sqlite3 2>/dev/null || echo "")}"
if ! test -f "$_SNAPDIR_BIN_PATH"; then
if snapdir -v 2>/dev/null >/dev/null; then
_SNAPDIR_BIN_PATH="snapdir"
else
echo "error: Could not find snapdir binary"
exit 1
fi
fi
if [[ $SNAPDIR_SQLITE3_BIN == "" ]]; then
echo "error: sqlite3 is not installed and it's required for the disk catalog." >&2
exit 1
fi
# we don't want snapdir to capture the stdin for the catalog script
# shellcheck disable=SC1090
. "$_SNAPDIR_BIN_PATH" <<<""
_snapdir_sqlite3_catalog_run "${@:1}"
else
_snapdir_sqlite3_catalog_export_env_defaults
fi