]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.cli
Add support for pptp dialin.
[people/ms/network.git] / functions.cli
index 579a263fc986202f4bd804924e44d0e3f6cf1522..3389b4106898d22c15a399978061e963b746d23d 100644 (file)
@@ -19,6 +19,8 @@
 #                                                                             #
 ###############################################################################
 
+IDENT="  "
+
 function cli_help_requested() {
        local argument="${1}"
 
@@ -31,10 +33,28 @@ function cli_help_requested() {
        return ${EXIT_ERROR}
 }
 
+function cli_run_help() {
+       local command="$@"
+
+       print "Run \"${command} help\" to get more information."
+       return ${EXIT_OK}
+}
+
 function cli_device_headline() {
        local device=${1}
        assert isset device
 
+       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"
@@ -50,31 +70,62 @@ function cli_device_headline() {
                        headline_prefix="Zone ${device}"
                        ;;
                port)
-                       headline_prefix="Port ${device}"
+                       headline_prefix="Port ${device} ($(device_get_type ${device}))"
                        ;;
                *)
-                       headline_prefix="Device ${device}"
+                       headline_prefix="Device ${device} ($(device_get_type ${device}))"
                        ;;
        esac
 
-       cli_headline 1 "${headline_prefix}:"
-
        # Print the hook for all zones.
        if [ "${type}" = "zone" ]; then
-               cli_print_fmt1 1 "Hook" "$(zone_get_hook ${device})"
+               headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
        fi
+       cli_headline 1 "${headline_prefix}"
 
        # Print the device status.
-       local status=$(device_get_status ${device})
-       cli_print_fmt1 1 "Status" "${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL}"
+       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}
+                       ;;
+               *)
+                       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() {
@@ -103,25 +154,102 @@ function cli_device_stats() {
        cli_space
 }
 
-function cli_status_headline() {
-       echo "XXX THIS FUNCTION IS DEPRECATED"
+function cli_device_vlan() {
+       local device=${1}
+
+       cli_headline 2 "VLAN"
+
+       cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
+       cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
+       cli_space
+}
+
+function cli_device_bonded() {
+       local device=${1}
+
+       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
+
+       cli_space
+}
+
+function cli_device_bonding() {
+       local device=${1}
+       assert isset device
+
+       cli_headline 2 "Bonding information"
 
-       local zone=${1}
+       local mode=$(bonding_get_mode ${device})
 
-       local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
-       zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
+       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
 
-       echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
+       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
+
+               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
 }
 
 function cli_headline() {
        local level=${1}
-       shift
+       local format=${2}
+       shift 2
 
-       local message="$@"
        local ident=$(cli_ident ${level})
 
-       printf "${ident}${COLOUR_BOLD}$@${COLOUR_NORMAL}\n"
+       local out
+       printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
+       printf "${out}"
+}
+
+function cli_statusline() {
+       local level=${1}
+       shift
+
+       local head=${1}
+       shift
+
+       cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
 }
 
 function cli_print() {
@@ -140,7 +268,7 @@ function cli_print_fmt1() {
        local level=${1}
        shift
 
-       local space=$(( 30 - (${level} * 4) ))
+       local space=$(( 34 - (${level} * ${#IDENT}) ))
        local format="%-${space}s %s"
 
        cli_print ${level} "${format}" "$@"
@@ -162,11 +290,17 @@ function cli_print_yesno() {
        fi
 }
 
+function cli_print_enabled() {
+       enabled ${1}
+
+       cli_print_bool $?
+}
+
 function cli_print_warning() {
        local level=${1}
        shift
 
-       cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@"
+       cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
 }
 
 function cli_space() {
@@ -175,15 +309,15 @@ function cli_space() {
 
 function cli_ident() {
        local level=${1}
-       shift
+       assert isinteger level
 
        local ident=""
        while [ ${level} -gt 1 ]; do
-               ident="${ident}    "
+               ident="${ident}${IDENT}"
                level=$(( ${level} - 1 ))
        done
 
-       echo "${ident}"
+       print "${ident}"
 }
 
 function cli_yesno() {
@@ -211,7 +345,19 @@ function cli_get_key() {
 }
 
 function cli_get_val() {
-       echo "${@##*=}"
+       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() {