-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli
executable file
·161 lines (135 loc) · 2.71 KB
/
cli
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
#!/bin/sh
if [ -f /usr/share/libubox/jshn.sh ]; then
. /usr/share/libubox/jshn.sh
else
. /usr/local/share/libubox/jshn.sh
fi
set_path() {
json_init
json_add_array path
cur="$1"
while [ "${cur%%${sep}*}" != "$cur" ]; do
[ -n "${cur%%${sep}*}" ] && json_add_string "" "${cur%%${sep}*}"
cur="${cur#*${sep}}"
done
[ -n "$cur" ] && json_add_string "" "$cur"
json_close_array path
}
ubus_status() {
local _json_no_warning=1
local code message
json_select error || return 0
json_get_vars code message
echo "Error $code: $message"
}
ubus_call() {
local cmd="$1"
local data="$(json_dump)"
local data="$(ubus call "scald.$model" $cmd "$data")"
if [ -n "$data" ]; then
json_load "$data"
ubus_status
else
json_init
fi
}
do_info() {
set_path "$1"
ubus_call info
json_get_keys parameters parameters
for par in $parameters; do
echo "${indent}$par"
done
}
list_entry() {
set_path "$1"
ubus_call list
json_get_values objs objects
for obj in $objs; do
echo "$indent$obj:"
indent=" $indent"
do_info "${entry}${sep}${obj}"
[ "$recursive" ] && list_entry "${entry}${sep}${obj}"
indent="$cur_indent"
done
}
cmd_list() {
[ -n "$1" ] || list_entry ""
while [ -n "$1" ]; do
list_entry "$1"
shift
done
}
cmd_get() {
local path="$1"
local param="$2"
[ -n "$path" -a -n "$param" ] || usage
set_path "$path"
json_add_string name "$param"
ubus_call get
json_get_var val "value"
echo "$val"
}
cmd_set() {
local path="$1"
local param="$2"
local value="$3"
[ -n "$path" -a -n "$param" ] || usage
set_path "$path"
json_add_string name "$param"
json_add_string value "$value"
ubus_call set
}
cmd_add() {
local path="$1"
local name="$2"
[ -n "$path" -a -n "$name" ] || usage
set_path "$path"
json_add_string name "$name"
ubus_call add
}
cmd_remove() {
local path="$1"
[ -n "$path" ] || usage
set_path "$path"
ubus_call remove
}
cmd_commit() {
json_init
ubus_call commit
}
usage() {
cat <<EOF
Usage: $0 [<options>] <command> [<arguments>]
Options:
-r: Recursively list objects
-s <char>: Use <char> as object separator
(default: '.')
Commands:
list <model> [<object>...] List objects
get <model> <object> <param> Get a parameter value
set <model> <object> <param> <value> Set a parameter value
add <model> <object> <name> Add an object instance
remove <model> <object> Remove an object instance
commit <model> Commit all changes
EOF
exit 1
}
indent=
sep="."
while [ -n "$1" ]; do
command="$1"; shift
case "$command" in
-r) recursive=1;;
-s) sep="$1"; shift;;
-*) echo "Unknown option: $command"; break;;
list|get|set|add|remove|commit)
model="$1"; shift
[ -n "$model" ] || usage
"cmd_$command" "$@"
exit
;;
*) break;;
esac
done
usage