]> git.ipfire.org Git - people/stevee/network.git/blame - functions.cli
Fix "network device" command and document it.
[people/stevee/network.git] / functions.cli
CommitLineData
1848564d
MT
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
1d7bc4f3 22function cli_help_requested() {
866de228
MT
23 local argument="${1}"
24
25 if [ -n "${argument}" ]; then
26 if listmatch ${argument} help -h --help; then
1d7bc4f3
MT
27 return ${EXIT_OK}
28 fi
866de228 29 fi
1d7bc4f3
MT
30
31 return ${EXIT_ERROR}
32}
33
ec63256a
MT
34function cli_zone_headline() {
35 local zone=${1}
36
37 cli_status_headline ${zone}
38}
39
9178284d
MT
40function 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
49function cli_headline() {
ec63256a
MT
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
59function 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
71function 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
81function cli_print_bool() {
82 if [ "${1}" = "${EXIT_TRUE}" ]; then
83 echo "true"
84 else
85 echo "false"
86 fi
87}
88
89function cli_print_yesno() {
90 if [ "${1}" = "${EXIT_TRUE}" ]; then
91 echo "yes"
92 else
93 echo "false"
94 fi
95}
96
97function cli_space() {
98 printf "\n"
99}
100
101function 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}"
9178284d 112}
f90e550b
MT
113
114function cli_yesno() {
39f552f5 115 local message="$@ [y/n] "
f90e550b
MT
116 local yesno
117
39f552f5
MT
118 while true; do
119 printf "\n${message}"
120 read yesno
f90e550b 121
39f552f5
MT
122 # Check for "yes".
123 if listmatch ${yesno} y Y yes YES Yes; then
124 return ${EXIT_TRUE}
f90e550b 125
39f552f5
MT
126 # Check for "no".
127 elif listmatch ${yesno} n N no NO No; then
128 return ${EXIT_FALSE}
129 fi
130 done
f90e550b 131}
d76f5107
MT
132
133function cli_get_key() {
134 local key="${1%%=*}"
135 echo "${key/--/}"
136}
137
138function cli_get_val() {
e9ea243e 139 echo "${@##*=}"
d76f5107 140}
de28a630
MT
141
142function 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
154function 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}