-
Notifications
You must be signed in to change notification settings - Fork 20
/
push2github
executable file
·132 lines (97 loc) · 3.82 KB
/
push2github
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
#!/bin/bash
# This script uses yajl - http://lloyd.github.com/yajl/
# The cron setup: */15 * * * * cd /home/slopjong/OpenSpaceDirectory; ./push2github
#################################
### helpers #####################
space_added_updated() {
# What space was added/updated?
# Assumption: directory changes at most once every 15 minutes otherwise only the first
# changed space will appear in the commit message
echo $(git diff directory.json | egrep -o "\+[[:space:]]+.+\:[[:space:]]" | tail -n1 | cut -d'"' -f2)
}
commit() {
local space="$(space_added_updated)"
if [ "$space" = "" ]
then
echo "Nothing to commit"
else
git commit directory.json -m "Space added or updated: ${space}"
echo "Changes committed"
fi
}
push() {
git push && git push spaceapi master
}
is_directory_valid() {
# TODO: check how the -q of json_verify can be used within a pipe
local verification_result=$(echo $1 | json_verify | grep -o invalid)
if [ "$verification_result" = "invalid" ]
then
echo false
else
echo true
fi
}
reformat_and_save_directory() {
echo $1 | json_reformat > directory.json
}
#################################
### main code ###################
main() {
local directory="$(curl -s http://openspace.slopjong.de/directory.json)"
if $(is_directory_valid "$directory");
then
echo "The directory is valid"
# TODO: use reformat_and_save_directory()
echo $directory | json_reformat > directory.json && commit && push
else
echo "The directory is invalid"
fi
}
#################################
### test code ###################
# TODO: use shunit2?
#
# https://shunit2.googlecode.com/svn/trunk/source/2.1/doc/shunit2.html
test_space_added_updated() {
echo "Run test_space_added_updated"
echo "---------------------------------"
# simulate a new added space
local directory="$(curl -s http://openspace.slopjong.de/directory.json)"
directory=$(echo $directory | sed "s/{/{\"test-space\":\"http:\/\/example.com\",/g")
reformat_and_save_directory "$directory"
local space="$(space_added_updated)"
echo -n "test(\"\$space\" = \"test-space\") => "
[[ "$space" = "test-space" ]] && echo "passed";
}
test_is_directory_valid() {
echo "Run test_is_directory_valid"
echo "---------------------------------"
local valid_json="{}"
local invalid_json="{asdfasf}"
echo -n "test \"is_directory_valid(\"$valid_json\") = true\" => "
[[ "$(is_directory_valid $valid_json)" = true ]] && echo "passed" || echo "failed";
echo -n "test \"is_directory_valid(\"$invalid_json\") = false\" => "
[[ "$(is_directory_valid $invalid_json)" = false ]] && echo "passed" || echo "failed";
echo "testing false positive and negative (passed means there's no false positive and negative):"
echo -n "test \"is_directory_valid(\"$invalid_json\") = true\" => "
[[ "$(is_directory_valid $invalid_json)" = true ]] && echo "failed" || echo "passed";
echo -n "test \"is_directory_valid(\"$valid_json\") = false\" => "
[[ "$(is_directory_valid $valid_json)" = false ]] && echo "failed" || echo "passed";
}
#################################
### now run main or the tests ###
# to run the tests execute "run_tests=true ./push2github" in the shell
if ${run_tests:=false}
then
echo "Running the tests ..."
echo "================================================="
test_space_added_updated
echo "================================================="
test_is_directory_valid
echo "================================================="
else
main
fi
####################################################################
# - http://www.linuxjournal.com/content/return-values-bash-functions