]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.cli
Add support for pptp dialin.
[people/stevee/network.git] / functions.cli
index 21bb3c5229e9c09a9a07bac535652fd2def6d18a..3389b4106898d22c15a399978061e963b746d23d 100644 (file)
 #                                                                             #
 ###############################################################################
 
-function cli_config() {
-       if cli_help_requested $@; then
-               cli_usage root-config
-               exit ${EXIT_OK}
-       fi
+IDENT="  "
 
-       if [ -n "${1}" ]; then
-               network_config_set $@
-       else
-               network_config_print
+function cli_help_requested() {
+       local argument="${1}"
+
+       if [ -n "${argument}" ]; then
+               if listmatch ${argument} help -h --help; then
+                       return ${EXIT_OK}
+               fi
        fi
+
+       return ${EXIT_ERROR}
 }
 
-function cli_device() {
-       local action=${1}
-       shift
+function cli_run_help() {
+       local command="$@"
 
-       local device
-       local devices=$@
+       print "Run \"${command} help\" to get more information."
+       return ${EXIT_OK}
+}
+
+function cli_device_headline() {
+       local device=${1}
+       assert isset device
 
-       if [ -z "${devices}" ]; then
-               devices=$(devices_get_all)
+       local long=0
+       shift
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --long)
+                               long=1
+                               ;;
+               esac
+               shift
+       done
+
+       local type
+       if zone_exists ${device}; then
+               type="zone"
+       elif port_exists ${device}; then
+               type="port"
+       else
+               type="unknown"
        fi
 
-       case "${action}" in
-               discover)
-                       echo "# XXX need to implement --raw here"
-                       for device in ${devices}; do
-                               cli_device_discover ${device} $@
-                       done
+       local headline_prefix
+       case "${type}" in
+               zone)
+                       headline_prefix="Zone ${device}"
                        ;;
+               port)
+                       headline_prefix="Port ${device} ($(device_get_type ${device}))"
+                       ;;
+               *)
+                       headline_prefix="Device ${device} ($(device_get_type ${device}))"
+                       ;;
+       esac
 
-               show|"")
-                       for device in ${devices}; do
-                               cli_device_print ${device}
-                       done
+       # Print the hook for all zones.
+       if [ "${type}" = "zone" ]; then
+               headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
+       fi
+       cli_headline 1 "${headline_prefix}"
+
+       # Print the device status.
+       local status
+       case "$(device_get_status ${device})" in
+               ${STATUS_UP})
+                       status=${MSG_DEVICE_STATUS_UP}
+                       ;;
+               ${STATUS_DOWN})
+                       status=${MSG_DEVICE_STATUS_DOWN}
+                       ;;
+               ${STATUS_NOCARRIER})
+                       status=${MSG_DEVICE_STATUS_NOCARRIER}
                        ;;
                *)
-                       cli_usage device
-                       ;;                              
+                       status=${MSG_DEVICE_STATUS_UNKNOWN}
+                       ;;
        esac
+       cli_print_fmt1 1 "Status" "${status}"
+       if enabled long; then
+               cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
+       fi
+       if device_is_up ${device}; then
+               cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
+       fi
+       if enabled long; then
+               device_is_promisc ${device} &>/dev/null
+               cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
+       fi
+
+       cli_space
+
+       # Print the device stats.
+       device_is_up ${device} && cli_device_stats 2 ${device}
+
+       if enabled long; then
+               # Virtual devices.
+               device_is_vlan ${device} && cli_device_vlan ${device}
+
+               # Bonded devices.
+               device_is_bonded ${device} && cli_device_bonded ${device}
+
+               # Bonding devices.
+               device_is_bonding ${device} && cli_device_bonding ${device}
+       fi
+}
+
+function cli_device_stats() {
+       local level=${1}
+       local device=${2}
+
+       # This section will print statistical data from the device.
+       local packets bytes errors
+
+       cli_headline ${level} "Statistics"
+       local format="%-10s %9d packets %6s (%d errors)"
+
+       # RX
+       packets=$(device_get_rx_packets ${device})
+       bytes=$(device_get_rx_bytes ${device})
+       errors=$(device_get_rx_errors ${device})
+
+       cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
+
+       # TX
+       packets=$(device_get_tx_packets ${device})
+       bytes=$(device_get_tx_bytes ${device})
+       errors=$(device_get_tx_errors ${device})
+
+       cli_print ${level} "${format}" "Sent"     "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
+       cli_space
 }
 
-function cli_device_print() {
+function cli_device_vlan() {
        local device=${1}
 
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+       cli_headline 2 "VLAN"
 
-       echo "${device}"
-       echo "  Type: $(device_get_type ${device})"
-       echo "  Addr: $(device_get_address ${device})"
-       echo
+       cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
+       cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
+       cli_space
 }
 
-function cli_device_discover() {
+function cli_device_bonded() {
        local device=${1}
-       shift
 
-       local device_type=$(device_get_type ${device})
-       if [ "${device_type}" != "real" ]; then
-               return ${EXIT_OK}
+       cli_headline 2 "Bonding information"
+
+       local master=$(bonding_slave_get_master ${port})
+       cli_print_fmt1 2 "Master" "${master}"
+
+       local mode=$(bonding_get_mode ${master})
+       if [ "${mode}" = "active-backup" ]; then
+               local active_slaves=$(bonding_get_slaves ${master} --active)
+               local active="false"
+               if list_match "${device}" ${active_slaves}; then
+                       active="true"
+               fi
+               cli_print_fmt1 2 "Active slave" "$(cli_print_yesno ${active})"
        fi
 
-       local raw
+       cli_space
+}
+
+function cli_device_bonding() {
+       local device=${1}
+       assert isset device
 
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --raw)
-                               raw=1
+       cli_headline 2 "Bonding information"
+
+       local mode=$(bonding_get_mode ${device})
+
+       cli_print_fmt1 2 "Mode" "${mode}"
+       if [ "${mode}" = "802.3ad" ]; then
+               local lacp_rate=$(bonding_get_lacp_rate ${device})
+               cli_print_fmt1 2 "LACP rate" "${lacp_rate}"
+       fi
+       cli_space
+
+       local slave slave_suffix
+       local active_slaves=$(bonding_get_slaves ${device} --active)
+       for slave in $(bonding_get_slaves ${device}); do
+               # Print the device status.
+               local status
+               case "$(device_get_status ${slave})" in
+                       ${STATUS_UP})
+                               status=${MSG_DEVICE_STATUS_UP}
+                               ;;
+                       ${STATUS_DOWN})
+                               status=${MSG_DEVICE_STATUS_DOWN}
+                               ;;
+                       ${STATUS_NOCARRIER})
+                               status=${MSG_DEVICE_STATUS_NOCARRIER}
+                               ;;
+                       *)
+                               status=${MSG_DEVICE_STATUS_UNKNOWN}
                                ;;
                esac
-               shift
+
+               if list_match "${slave}" ${active_slaves}; then
+                       slave_suffix="(active)"
+               else
+                       slave_suffix=""
+               fi
+               cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
        done
+       cli_space
+}
 
-       local up
-       device_is_up ${device} && up=1
-       device_set_up ${device}
+function cli_headline() {
+       local level=${1}
+       local format=${2}
+       shift 2
 
-       enabled raw || echo "${device}"
+       local ident=$(cli_ident ${level})
 
-       local hook
        local out
-       local ret
-       for hook in $(hooks_get_all); do
-               out=$(hook_exec ${hook} discover ${device})
-               ret=$?
-
-               [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
-
-               if enabled raw; then
-                       case "${ret}" in
-                               ${DISCOVER_OK})
-                                       echo "${hook}: OK"
-                                       local line
-                                       while read line; do
-                                               echo "${hook}: ${line}"
-                                       done <<<"${out}"
-                                       ;;
-
-                               ${DISCOVER_ERROR})
-                                       echo "${hook}: FAILED"
-                                       ;;
-                       esac
-               else
-                       case "${ret}" in
-                               ${DISCOVER_OK})
-                                       echo "  ${hook} was successful."
-                                       local line
-                                       while read line; do
-                                               echo "  ${line}"
-                                       done <<<"${out}"
-                                       ;;
-
-                               ${DISCOVER_ERROR})
-                                       echo "  ${hook} failed."
-                                       ;;
-                       esac
-               fi
-       done
+       printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
+       printf "${out}"
+}
 
-       echo # New line
+function cli_statusline() {
+       local level=${1}
+       shift
 
-       [ "${up}" = "1" ] || device_set_down ${device}
+       local head=${1}
+       shift
+
+       cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
 }
 
-function cli_zone() {
-       local action
-       local zone
+function cli_print() {
+       local level=${1}
+       local format=${2}
+       shift 2
 
-       if zone_name_is_valid ${1}; then
-               zone=${1}
-               action=${2}
-               shift 2
+       local ident=$(cli_ident $(( ${level} + 1 )))
 
-               case "${action}" in
-                       config|down|edit|port|show|status|up)
-                               zone_${action} ${zone} $@
-                               ;;
-               esac
-       else
-               action=${1}
-               shift
+       local out
+       printf -v out "${ident}${format}\n" "$@"
+       printf "${out}"
+}
 
-               case "${action}" in
-                       create|remove)
-                               zone_${action} $@
-                               ;;
-                       *)
-                               error "Unrecognized argument: '${action}'"
-                               ;;
-               esac
+function cli_print_fmt1() {
+       local level=${1}
+       shift
+
+       local space=$(( 34 - (${level} * ${#IDENT}) ))
+       local format="%-${space}s %s"
+
+       cli_print ${level} "${format}" "$@"
+}
+
+function cli_print_bool() {
+       if [ "${1}" = "${EXIT_TRUE}" ]; then
+               echo "true"
+       else
+               echo "false"
        fi
 }
 
-function cli_start() {
-       if cli_help_requested $@; then
-               cli_usage root-start
-               exit ${EXIT_OK}
+function cli_print_yesno() {
+       if [ "${1}" = "${EXIT_TRUE}" ]; then
+               echo "yes"
+       else
+               echo "false"
        fi
+}
 
-       local zones=$(zones_get $@)
+function cli_print_enabled() {
+       enabled ${1}
 
-       local zone
-       for zone in ${zones}; do
-               zone_up ${zone}
-       done
+       cli_print_bool $?
 }
 
-function cli_stop() {
-       if cli_help_requested $@; then
-               cli_usage root-stop
-               exit ${EXIT_OK}
-       fi
-
-       local zones=$(zones_get $@)
+function cli_print_warning() {
+       local level=${1}
+       shift
 
-       local zone
-       for zone in ${zones}; do
-               zone_down ${zone}
-       done
+       cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
 }
 
-function cli_restart() {
-       if cli_help_requested $@; then
-               cli_usage root-restart
-               exit ${EXIT_OK}
-       fi
+function cli_space() {
+       printf "\n"
+}
 
-       cli_stop $@
+function cli_ident() {
+       local level=${1}
+       assert isinteger level
 
-       # Give the system some time to calm down
-       sleep ${TIMEOUT_RESTART}
+       local ident=""
+       while [ ${level} -gt 1 ]; do
+               ident="${ident}${IDENT}"
+               level=$(( ${level} - 1 ))
+       done
 
-       cli_start $@
+       print "${ident}"
 }
 
-function cli_help_requested() {
-       local argument
-       for argument in $@; do
-               if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
-                       return ${EXIT_OK}
+function cli_yesno() {
+       local message="$@ [y/n] "
+       local yesno
+
+       while true; do
+               printf "\n${message}"
+               read yesno
+
+               # Check for "yes".
+               if listmatch ${yesno} y Y yes YES Yes; then
+                       return ${EXIT_TRUE}
+
+               # Check for "no".
+               elif listmatch ${yesno} n N no NO No; then
+                       return ${EXIT_FALSE}
                fi
        done
+}
 
-       return ${EXIT_ERROR}
+function cli_get_key() {
+       local key="${1%%=*}"
+       echo "${key/--/}"
+}
+
+function cli_get_val() {
+       echo "${@#*=}"
+}
+
+function cli_get_bool() {
+       local value="$(cli_get_val "$@")"
+
+       if enabled value; then
+               print "true"
+               return ${EXIT_TRUE}
+       fi
+
+       print "false"
+       return ${EXIT_FALSE}
 }
 
 function cli_usage() {
-       local what=${1}
-
-       case "${what}" in
-               root)
-                       echo "${0}: [command] <options ...>"
-                       echo
-                       echo "  start  - ..."
-                       echo "  stop   - ..."
-                       echo "  restart - ..."
-                       echo
-                       echo "  config - ..."
-                       echo
-                       echo "  device - ..."
-                       echo "  show   - ???"
-                       echo "  zone   - ..."
-                       echo
-                       ;;
-               root-config)
-                       echo    "${0}: ${what#root-} [KEY=VAL, ...]"
-                       echo
-                       echo    "  This command allows setting of global configuration parameters."
-                       echo
-                       echo    "  If no additional arguments are passed it will list the current configuration."
-                       echo
-                       echo    "  You can overwrite the settings like the following:"
-                       echo
-                       echo    "    ${0} ${what#root-} DEBUG=1 ..."
-                       echo
-                       ;;
-               root-start|root-stop|root-restart)
-                       echo    "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
-                       echo
-                       echo    "  This commands ${what#root-}s all zones by default."
-                       echo    "  One can pass several parameters to only process a subset of all"
-                       echo    "  available zones:"
-                       echo
-                       echo -e "    ${BOLD}--local-only${NORMAL}"
-                       echo    "    Process all local zones which includes every zone without red."
-                       echo
-                       echo -e "    ${BOLD}--remote-only${NORMAL}"
-                       echo    "    Process all remote zones which means only the red ones."
-                       echo
-                       echo -e "    ${BOLD}--all${NORMAL}"
-                       echo    "    Process all zones. This is the default parameter."
-                       echo
-                       echo    "    Additionally, you can pass one or more zone names which will"
-                       echo    "    be processed."
-                       echo
-                       ;;
-               usage)
-                       echo
-                       echo "  Run '${0} help' to get information how to use this tool."
-                       echo
-                       ;;
-               *)
-                       error "No help available for this command '${what}'."
-                       echo
-                       ;;
-       esac
+       local command="$@"
+       local basename="$(basename ${0})"
+
+       if ! isset command; then
+               command="${basename} help"
+       fi
+
+       echo "The given command was not understood by ${basename}." >&2
+       echo "Please run '${command}' for detailed help." >&2
+}
+
+function cli_show_man() {
+       local manpage=${1}
+       assert isset manpage
+
+       if ! binary_exists man; then
+               error "The man package is not installed on this system."
+               error "Please install 'man' in order to view the help."
+               exit ${EXIT_ERROR}
+       fi
 
-       echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
+       man ${manpage}
 }