[arch-projects] [netctl][PATCH] Support the 'is-active' command

Jouke Witteveen j.witteveen at gmail.com
Thu Jun 9 19:58:17 UTC 2016


Status getting commands are now uniform between netctl and netctl-auto.
Both support 'is-active' and 'is-enabled'. The 'current' command is
dropped from netctl-auto. Its use case is covered by the 'list' command.

Suggested by Evgeniy Alekseev <i at arcanis.me>
---
 contrib/bash-completion |  8 +++---
 contrib/zsh-completion  |  6 +++--
 docs/netctl-auto.1.txt  | 11 +++++---
 docs/netctl.1.txt       |  4 +++
 src/netctl-auto         | 72 ++++++++++++++++++++++++++++++++++---------------
 src/netctl.in           |  3 ++-
 6 files changed, 72 insertions(+), 32 deletions(-)

diff --git a/contrib/bash-completion b/contrib/bash-completion
index 4f73fab..2ab6acb 100644
--- a/contrib/bash-completion
+++ b/contrib/bash-completion
@@ -23,10 +23,10 @@ _netctl()
 
     case $COMP_CWORD in
       1)
-        COMPREPLY=( $(compgen -W "--help --version list store restore stop-all start stop restart switch-to status enable disable reenable is-enabled edit" -- "$cur") )
+        COMPREPLY=( $(compgen -W "--help --version list store restore stop-all start stop restart switch-to is-active status enable disable reenable is-enabled edit" -- "$cur") )
       ;;
       2)
-        [[ ${COMP_WORDS[COMP_CWORD-1]} = @(start|stop|restart|switch-to|status|enable|disable|reenable|is-enabled|edit) ]] &&
+        [[ ${COMP_WORDS[COMP_CWORD-1]} = @(start|stop|restart|switch-to|is-active|status|enable|disable|reenable|is-enabled|edit) ]] &&
           mapfile -t COMPREPLY < <(IFS=$'\n'; compgen -W "$(_netctl_profiles)" -- "$cur")
       ;;
     esac
@@ -40,10 +40,10 @@ _netctl_auto()
 
     case $COMP_CWORD in
       1)
-        COMPREPLY=( $(compgen -W "--help --version list current switch-to enable disable enable-all disable-all" -- "$cur") )
+        COMPREPLY=( $(compgen -W "--help --version list switch-to is-active enable disable enable-all disable-all is-enabled" -- "$cur") )
       ;;
       2)
-        [[ ${COMP_WORDS[COMP_CWORD-1]} = @(switch-to|enable|disable) ]] &&
+        [[ ${COMP_WORDS[COMP_CWORD-1]} = @(switch-to|is-active|enable|disable|is-enabled) ]] &&
           mapfile -t COMPREPLY < <(IFS=$'\n'; compgen -W "$(_netctl_profiles)" -- "$cur")
       ;;
     esac
diff --git a/contrib/zsh-completion b/contrib/zsh-completion
index 05c506f..f583526 100644
--- a/contrib/zsh-completion
+++ b/contrib/zsh-completion
@@ -11,7 +11,7 @@ _wireless_interfaces() {
 
 (( $+function[_netctl_command] )) ||
 _netctl_command() {
-    [[ $words[1] = (start|stop|restart|switch-to|status|enable|disable|reenable|is-enabled|edit) ]] &&
+    [[ $words[1] = (start|stop|restart|switch-to|is-active|status|enable|disable|reenable|is-enabled|edit) ]] &&
       compadd "${(f)$(find -L /etc/netctl -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name '*.conf' -not -name '*.service' -printf "%f\n")}"
 }
 
@@ -27,6 +27,7 @@ _netctl_commands() {
         'stop:Stop a profile'
         'restart:Restart a profile'
         'switch-to:Switch to a profile'
+        'is-active:Check whether a profile is active'
         'status:Show runtime status of a profile'
         'enable:Enable the systemd unit for a profile'
         'disable:Disable the systemd unit for a profile'
@@ -41,12 +42,13 @@ _netctl-auto_commands() {
     local -a _commands
     _commands=(
         'list:List available profiles'
-        'current:List currently active profiles'
         'switch-to:Switch to a profile, enable it if necessary'
+        'is-active:Check whether a profile is active'
         'enable:Enable a profile for automatic selection'
         'disable:Disable a profile temporarily for automatic selection'
         'enable-all:Enable all profiles for automatic selection'
         'disable-all:Disable all profiles temporarily for automatic selection'
+        'is-enabled:Check whether a profile is enabled'
       )
     _describe "netctl-auto commands" _commands
 }
diff --git a/docs/netctl-auto.1.txt b/docs/netctl-auto.1.txt
index 6feaeb4..3e038a2 100644
--- a/docs/netctl-auto.1.txt
+++ b/docs/netctl-auto.1.txt
@@ -29,9 +29,6 @@ The following commands are understood:
     selection. Active profiles will be marked with a `*', disabled
     profiles will be marked with a `!'.
 
-*current*::
-    Report currently active profiles.
-
 *switch-to [+PROFILE+]*::
     Switch to the network profile specified on the command line. The
     specified profile will be enabled if necessary, the state of all
@@ -39,6 +36,10 @@ The following commands are understood:
     *netctl-auto* to use the specified profile. If a disconnect occurs,
     *netctl-auto* may select an alternative profile.
 
+*is-active [+PROFILE+]*::
+    Check whether the network profile specified on the command line is
+    active. Prints the current state.
+
 *enable [+PROFILE+]*::
     Enable a previously disabled network profile for automatic
     selection. Every time the *netctl-auto* service is started, all
@@ -56,6 +57,10 @@ The following commands are understood:
 *disable-all*::
     Disable all profiles for automatic selection.
 
+*is-enabled [+PROFILE+]*::
+    Check whether the specified profile is enabled for the *netctl-auto*
+    service that is currently running. Prints the current enable status.
+
 *start [+INTERFACE+]*::
     Start automatic profile selection on the specified interface. +
     This command should not be invoked directly, use the following
diff --git a/docs/netctl.1.txt b/docs/netctl.1.txt
index dc9f3bf..4fe87ca 100644
--- a/docs/netctl.1.txt
+++ b/docs/netctl.1.txt
@@ -51,6 +51,10 @@ The following commands are understood:
     Starts the network profile specified on the command line after
     stopping all profiles that refer to the same interface.
 
+*is-active [+PROFILE+]*::
+    Check whether the network profile specified on the command line is
+    active. Prints the current state.
+
 *status [+PROFILE+]*::
     Show terse runtime status information about a profile, followed by
     its most recent log data from the journal.
diff --git a/src/netctl-auto b/src/netctl-auto
index b84c0de..1a0bb4c 100755
--- a/src/netctl-auto
+++ b/src/netctl-auto
@@ -14,13 +14,14 @@ Usage: netctl-auto {COMMAND} ...
                    [--help|--version]
 
 Commands:
-  list                 List available profiles (active='*', disabled='!')
-  current              List currently active profiles
-  switch-to [PROFILE]  Switch to a profile, enable it if necessary
-  enable [PROFILE]     Enable a profile for automatic selection
-  disable [PROFILE]    Disable a profile temporarily for automatic selection
-  enable-all           Enable all profiles for automatic selection
-  disable-all          Disable all profiles temporarily for automatic selection
+  list                  List available profiles (active='*', disabled='!')
+  switch-to [PROFILE]   Switch to a profile, enable it if necessary
+  is-active [PROFILE]   Check whether a profile is active
+  enable [PROFILE]      Enable a profile for automatic selection
+  disable [PROFILE]     Disable a profile for automatic selection
+  enable-all            Enable all profiles for automatic selection
+  disable-all           Disable all profiles for automatic selection
+  is-enabled [PROFILE]  Check whether a profile is enabled
 END
 }
 
@@ -63,13 +64,13 @@ list_wpa_profiles() {
 get_wpa_network_id() {
     local interface id flag profile
     while read -r interface id flag profile; do
-        if [[ "$1" == "$profile" ]]; then
+        if [[ $profile == "$1" ]]; then
             echo "$interface" "$id"
             return 0
         fi
     done < <(list_wpa_profiles)
 
-    report_error "Profile '$1' does not exist or is not available" >&2
+    report_error "Profile '$1' does not exist or is not available"
     return 1
 }
 
@@ -146,23 +147,50 @@ switch_to() {
     reenable_networks
 }
 
-## List currently active profiles
-current() {
-    local interface
-    for interface in $(list_netctl_auto_interfaces); do
-        local state=$(wpa_get_state "$interface")
-        if [[ "$state" == "COMPLETED" ]]; then
-            wpa_call "$interface" status | sed -n 's/^id_str=//p'
+## Check whether a profile is active
+# $1: profile name
+is_active() {
+    local interface id flag profile
+    while read -r interface id flag profile; do
+        if [[ $profile == "$1" ]]; then
+            if [[ $flag == "a" ]]; then
+                echo "active"
+                return 0
+            else
+                echo "inactive"
+                return 1
+            fi
         fi
-    done
+    done < <(list_wpa_profiles)
+    echo "unknown profile: '$1'"
+    return 1
+}
+
+## Check whether a profile is enabled
+# $1: profile name
+is_enabled() {
+    local interface id flag profile
+    while read -r interface id flag profile; do
+        if [[ $profile == "$1" ]]; then
+            if [[ $flag != "d" ]]; then
+                echo "enabled"
+                return 0
+            else
+                echo "disabled"
+                return 1
+            fi
+        fi
+    done < <(list_wpa_profiles)
+    echo "unknown profile: '$1'"
+    return 1
 }
 
 ## List all available profiles and their status
 list() {
     local interface id flag profile
-    while read -r interface id flag profile; do
+    list_wpa_profiles | while read -r interface id flag profile; do
         echo "$(echo $flag | tr 'aed' '* !')" "$profile"
-    done < <(list_wpa_profiles)
+    done
 }
 
 ## Start and generate config file for the WPA supplicant, start wpa_actiond
@@ -236,7 +264,7 @@ case $# in
         report_notice "netctl version $NETCTL_VERSION";;
       --help)
         usage;;
-      list|current)
+      list)
         "$1";;
       enable-all|disable-all)
         profile_enable_disable "$1";;
@@ -247,8 +275,8 @@ case $# in
     case $1 in
       enable|disable)
         profile_enable_disable "$1" "$2";;
-      switch-to)
-        switch_to "$2";;
+      switch-to|is-active|is-enabled)
+        "${1//-/_}" "$2";;
       start|stop)
         if [[ -t 0 ]]; then
             exit_error "Use 'systemctl $1 netctl-auto@$2' to $1 netctl-auto."
diff --git a/src/netctl.in b/src/netctl.in
index 2953ecf..7b1696a 100644
--- a/src/netctl.in
+++ b/src/netctl.in
@@ -17,6 +17,7 @@ Commands:
   stop [PROFILE]        Stop a profile
   restart [PROFILE]     Restart a profile
   switch-to [PROFILE]   Switch to a profile
+  is-active [PROFILE]   Check whether a profile is active
   status [PROFILE]      Show runtime status of a profile
   enable [PROFILE]      Enable the systemd unit for a profile
   disable [PROFILE]     Disable the systemd unit for a profile
@@ -163,7 +164,7 @@ case $# in
     esac;;
   2)
     case $1 in
-      start|stop|restart|status|is-enabled)
+      start|stop|restart|is-active|status|is-enabled)
         sd_call "$1" "$2";;
       switch-to)
         ensure_root "$(basename "$0")"
-- 
2.8.3


More information about the arch-projects mailing list