scripts/utils.sh: add a function for setting a variable default
[project/netifd.git] / scripts / utils.sh
1 append() {
2         local var="$1"
3         local value="$2"
4         local sep="${3:- }"
5
6         eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
7 }
8
9 add_default_handler() {
10         case "$(type $1 2>/dev/null)" in
11                 *function*) return;;
12                 *) eval "$1() { return; }"
13         esac
14 }
15
16 set_default() {
17         local __s_var="$1"
18         local __s_val="$2"
19         eval "export -- \"$__s_var=\${$__s_var:-\$__s_val}\""
20 }
21
22 _config_add_generic() {
23         local type="$1"; shift
24
25         for name in "$@"; do
26                 json_add_array ""
27                 json_add_string "" "$name"
28                 json_add_int "" "$type"
29                 json_close_array
30         done
31 }
32
33 config_add_int() {
34         _config_add_generic 5 "$@"
35 }
36
37 config_add_array() {
38         _config_add_generic 1 "$@"
39 }
40
41 config_add_string() {
42         _config_add_generic 3 "$@"
43 }
44
45 config_add_boolean() {
46         _config_add_generic 7 "$@"
47 }