]> git.ipfire.org Git - network.git/blob - functions.cli
Fix "network device" command and document it.
[network.git] / functions.cli
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 function cli_help_requested() {
23 local argument="${1}"
24
25 if [ -n "${argument}" ]; then
26 if listmatch ${argument} help -h --help; then
27 return ${EXIT_OK}
28 fi
29 fi
30
31 return ${EXIT_ERROR}
32 }
33
34 function cli_zone_headline() {
35 local zone=${1}
36
37 cli_status_headline ${zone}
38 }
39
40 function cli_status_headline() {
41 local zone=${1}
42
43 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
44 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
45
46 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
47 }
48
49 function cli_headline() {
50 local level=${1}
51 shift
52
53 local message="$@"
54 local ident=$(cli_ident ${level})
55
56 printf "${ident}${COLOUR_BOLD}$@${COLOUR_NORMAL}\n"
57 }
58
59 function cli_print() {
60 local level=${1}
61 local format=${2}
62 shift 2
63
64 local ident=$(cli_ident $(( ${level} + 1 )))
65
66 local out
67 printf -v out "${ident}${format}\n" "$@"
68 printf "${out}"
69 }
70
71 function cli_print_fmt1() {
72 local level=${1}
73 shift
74
75 local space=$(( 30 - (${level} * 4) ))
76 local format="%-${space}s %s"
77
78 cli_print ${level} "${format}" "$@"
79 }
80
81 function cli_print_bool() {
82 if [ "${1}" = "${EXIT_TRUE}" ]; then
83 echo "true"
84 else
85 echo "false"
86 fi
87 }
88
89 function cli_print_yesno() {
90 if [ "${1}" = "${EXIT_TRUE}" ]; then
91 echo "yes"
92 else
93 echo "false"
94 fi
95 }
96
97 function cli_space() {
98 printf "\n"
99 }
100
101 function cli_ident() {
102 local level=${1}
103 shift
104
105 local ident=""
106 while [ ${level} -gt 1 ]; do
107 ident="${ident} "
108 level=$(( ${level} - 1 ))
109 done
110
111 echo "${ident}"
112 }
113
114 function cli_yesno() {
115 local message="$@ [y/n] "
116 local yesno
117
118 while true; do
119 printf "\n${message}"
120 read yesno
121
122 # Check for "yes".
123 if listmatch ${yesno} y Y yes YES Yes; then
124 return ${EXIT_TRUE}
125
126 # Check for "no".
127 elif listmatch ${yesno} n N no NO No; then
128 return ${EXIT_FALSE}
129 fi
130 done
131 }
132
133 function cli_get_key() {
134 local key="${1%%=*}"
135 echo "${key/--/}"
136 }
137
138 function cli_get_val() {
139 echo "${@##*=}"
140 }
141
142 function cli_usage() {
143 local command="$@"
144 local basename="$(basename ${0})"
145
146 if ! isset command; then
147 command="${basename} help"
148 fi
149
150 echo "The given command was not understood by ${basename}." >&2
151 echo "Please run '${command}' for detailed help." >&2
152 }
153
154 function cli_show_man() {
155 local manpage=${1}
156 assert isset manpage
157
158 if ! binary_exists man; then
159 error "The man package is not installed on this system."
160 error "Please install 'man' in order to view the help."
161 exit ${EXIT_ERROR}
162 fi
163
164 man ${manpage}
165 }