-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.sh
More file actions
62 lines (47 loc) · 833 Bytes
/
node.sh
File metadata and controls
62 lines (47 loc) · 833 Bytes
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
function get_id {
existing_ids=$(jq -r '.nodes[] | .id' <$file)
for id in $(seq 1 10000); do
if ! [[ "$existing_ids" =~ $id ]]; then
echo $id
success=t
break
fi
done
if test -z $success; then
echo "get_id failed!!"
fi
}
text=
valence=
while test $# -gt 0; do
case $1 in
-)
valence=-1
;;
+)
valence=1
;;
*)
if test "$text"; then
text="$text "$1
else
text=$1
fi
;;
esac
shift
done
if test -z "$text"; then
printf "$help_text"
exit
fi
if test -z "$valence"; then
valence=0
fi
# look for available ID
id=$(get_id)
echo $id
jq --arg id $id --arg text "$text" --arg valence $valence \
'.nodes += [{id: $id, text: $text, valence: $valence}]' \
<$file >${file}_tmp \
&& mv ${file}_tmp $file