Skip to content

Commit f0a7dab

Browse files
committed
Merge pull request #1 from tilfin/feature/option-and-dryrun
Implement getopts and dry run ENV
2 parents 64e467c + 353782f commit f0a7dab

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

backup

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,41 @@
55
#
66
##############################
77

8+
#
9+
# Usage and parse options
10+
#
11+
usage() {
12+
echo "Usage: $0 [-n] [-f configfile]" 1>&2
13+
exit 1
14+
}
15+
16+
while getopts f:nh OPT
17+
do
18+
case $OPT in
19+
f) cfgfile=$OPTARG
20+
;;
21+
n) DRY_RUN=1
22+
echo "Dry run!"
23+
export DRY_RUN
24+
;;
25+
h) usage
26+
;;
27+
\?) usage
28+
;;
29+
esac
30+
done
31+
32+
shift $((OPTIND - 1))
33+
34+
#
35+
# Move basedir
36+
#
837
BASEDIR=${0%/*}
938
BASEDIR=$(cd $BASEDIR && pwd)
1039

1140
#
1241
# Load config file and setup env
1342
#
14-
cfgfile=$1
1543
if [ "$cfgfile" == "" ]; then
1644
cfgfile=$BASEDIR/backup.conf
1745
fi

sync

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
echo "srcdir : $1"
66
echo "bucket : $2"
77
echo "destdir: $3"
8+
if [ "$DRY_RUN" ]; then
9+
echo "dry run backup"
10+
fi
811
exit $?

test/bats

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ teardown() {
2626
}
2727

2828
@test "invoking backup with valid config file" {
29-
run backup $confdir/valid.conf
29+
run backup -f $confdir/valid.conf
3030
[ "$status" -eq 0 ]
3131
[ "${lines[2]}" = "[OK] S01command" ]
3232
[ "${lines[3]}" = "Start to synchronize files to cloud." ]
@@ -37,25 +37,51 @@ teardown() {
3737
}
3838

3939
@test "invoking backup with config file that does not exist" {
40-
run backup notexists.conf
40+
run backup -f notexists.conf
4141
[ "$status" -eq 1 ]
4242
[ "${lines[0]}" = "Config file not found!" ]
4343
}
4444

4545
@test "invoking backup with config where buckets is not defined " {
46-
run backup $confdir/no-bucket.conf
46+
run backup -f $confdir/no-bucket.conf
4747
[ "$status" -eq 1 ]
4848
[ "${lines[0]}" = "Buckets not defined!" ]
4949
}
5050

5151
@test "invoking backup with config where backup temp dir is not defined " {
52-
run backup $confdir/no-tmpdir.conf
52+
run backup -f $confdir/no-tmpdir.conf
5353
[ "$status" -eq 1 ]
5454
[ "${lines[0]}" = "Backup temp dir not defined!" ]
5555
}
5656

5757
@test "invoking backup with config where backup temp dirname is invalid" {
58-
run backup $confdir/invalid-tmpdir.conf
58+
run backup -f $confdir/invalid-tmpdir.conf
5959
[ "$status" -eq 1 ]
6060
[ "${lines[0]}" = "Backup temp dirname must end with 'temp' or 'tmp'!" ]
6161
}
62+
63+
@test "invoking dry run backup" {
64+
run backup -n
65+
[ "$status" -eq 0 ]
66+
[ "${lines[0]}" = "Dry run!" ]
67+
[ "${lines[3]}" = "[OK] S01command" ]
68+
[ "${lines[4]}" = "Start to synchronize files to cloud." ]
69+
[ "${lines[5]}" = "srcdir : /tmp/backup-temp" ]
70+
[ "${lines[6]}" = "bucket : storage" ]
71+
[ "${lines[7]}" = "destdir: destination" ]
72+
[ "${lines[8]}" = "dry run backup" ]
73+
[ "${lines[9]}" = "[OK] Sync" ]
74+
}
75+
76+
@test "invoking dry run backup with config file" {
77+
run backup -n -f $confdir/valid.conf
78+
[ "$status" -eq 0 ]
79+
[ "${lines[0]}" = "Dry run!" ]
80+
[ "${lines[3]}" = "[OK] S01command" ]
81+
[ "${lines[4]}" = "Start to synchronize files to cloud." ]
82+
[ "${lines[5]}" = "srcdir : /tmp/backup-temp" ]
83+
[ "${lines[6]}" = "bucket : storage" ]
84+
[ "${lines[7]}" = "destdir: destination" ]
85+
[ "${lines[8]}" = "dry run backup" ]
86+
[ "${lines[9]}" = "[OK] Sync" ]
87+
}

0 commit comments

Comments
 (0)