From 99b8149ae13730a0e87507a5375dabf22bc3f3b7 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Thu, 20 Oct 2022 09:38:59 +0200 Subject: machinectl: allow --max-addresses=0 Sometimes the addresses are not important, so allow skipping them in output. --- man/machinectl.xml | 9 ++++----- src/machine/machinectl.c | 37 ++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/man/machinectl.xml b/man/machinectl.xml index 7abe2adf73..9f3e0921a4 100644 --- a/man/machinectl.xml +++ b/man/machinectl.xml @@ -800,11 +800,10 @@ - When used with the command, limits the number of ip - addresses output for every machine. Defaults to 1. All addresses can be requested with - all as argument to . If the argument to - is less than the actual number of addresses, - follows the last address. + When used with the command, limits the number of IP + addresses shown for every machine. Defaults to 1. All addresses can be requested with + all. If the limit is 0, the address column is not shown. Otherwise, if the machine + has more addresses than shown, follows the last address. diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index eac26bc4d3..14085f7397 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -60,8 +60,6 @@ #include "verbs.h" #include "web-util.h" -#define ALL_ADDRESSES -1 - static char **arg_property = NULL; static bool arg_all = false; static BusPrintPropertyFlags arg_print_flags = 0; @@ -83,7 +81,7 @@ static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; static const char* arg_format = NULL; static const char *arg_uid = NULL; static char **arg_setenv = NULL; -static int arg_max_addresses = 1; +static unsigned arg_max_addresses = 1; STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_setenv, strv_freep); @@ -260,7 +258,6 @@ static int show_table(Table *table, const char *word) { } static int list_machines(int argc, char *argv[], void *userdata) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(table_unrefp) Table *table = NULL; @@ -273,12 +270,13 @@ static int list_machines(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Could not get machines: %s", bus_error_message(&error, r)); - table = table_new("machine", "class", "service", "os", "version", "addresses"); + table = table_new("machine", "class", "service", "os", "version", + arg_max_addresses > 0 ? "addresses" : NULL); if (!table) return log_oom(); table_set_ersatz_string(table, TABLE_ERSATZ_DASH); - if (!arg_full && arg_max_addresses != ALL_ADDRESSES) + if (!arg_full && arg_max_addresses > 0 && arg_max_addresses < UINT_MAX) table_set_cell_height_max(table, arg_max_addresses); if (arg_full) @@ -310,23 +308,23 @@ static int list_machines(int argc, char *argv[], void *userdata) { &os, &version_id); - (void) call_get_addresses( - bus, - name, - 0, - "", - "\n", - &addresses); - r = table_add_many(table, TABLE_STRING, empty_to_null(name), TABLE_STRING, empty_to_null(class), TABLE_STRING, empty_to_null(service), TABLE_STRING, empty_to_null(os), - TABLE_STRING, empty_to_null(version_id), - TABLE_STRING, empty_to_null(addresses)); + TABLE_STRING, empty_to_null(version_id)); if (r < 0) return table_log_add_error(r); + + if (arg_max_addresses > 0) { + (void) call_get_addresses(bus, name, 0, "", "\n", &addresses); + + r = table_add_many(table, + TABLE_STRING, empty_to_null(addresses)); + if (r < 0) + return table_log_add_error(r); + } } r = sd_bus_message_exit_container(reply); @@ -2717,13 +2715,10 @@ static int parse_argv(int argc, char *argv[]) { case ARG_MAX_ADDRESSES: if (streq(optarg, "all")) - arg_max_addresses = ALL_ADDRESSES; - else if (safe_atoi(optarg, &arg_max_addresses) < 0) + arg_max_addresses = UINT_MAX; + else if (safe_atou(optarg, &arg_max_addresses) < 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid number of addresses: %s", optarg); - else if (arg_max_addresses <= 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Number of IPs cannot be negative or zero: %s", optarg); break; case '?': -- cgit v1.2.3 From 4e9183059a5738cd19a8bee76189889087c0610e Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Thu, 20 Oct 2022 07:48:02 +0200 Subject: shell-completion/zsh: silence error when machinectl is not installed This fixes a few unrelated issues: - when ENABLE_MACHINED is false, machinectl is not installed, but _sd_machines is still used in a few places that want to complete -M and such. Also, bash completion calls machinectl in various places. Make missing machinectl mean "no machines" in this case, so that no error is generated in the callers. - machinectl list --full would print multiple lines of output per machine, breaking grep, issue introduced in e2268fa43742ece4a5cdc2e93f731b2bb2fcc883. Using --max-addresses=1 would fix the issue, but let's use --max-addresses=0 because we now can. - the lists used in various places were slightly different for no good reason. - don't use a subshell if not necessary. The code for bash still uses the same combined list of images and running machines for various commands. The zsh code uses images for start/clone, and running machines for the rest. Maybe something to fix in the future. Replaces #25048. --- shell-completion/bash/busctl | 5 +++-- shell-completion/bash/journalctl | 5 +++-- shell-completion/bash/loginctl | 5 +++-- shell-completion/bash/machinectl | 5 +++-- shell-completion/bash/portablectl | 5 +++-- shell-completion/bash/systemctl.in | 5 +++-- shell-completion/bash/systemd-analyze | 4 +++- shell-completion/bash/systemd-cgls | 4 +++- shell-completion/bash/systemd-cgtop | 4 +++- shell-completion/bash/systemd-nspawn | 4 +++- shell-completion/bash/systemd-run | 4 +++- shell-completion/bash/timedatectl | 4 +++- shell-completion/zsh/_sd_machines | 5 ++++- 13 files changed, 40 insertions(+), 19 deletions(-) diff --git a/shell-completion/bash/busctl b/shell-completion/bash/busctl index ee51c84c0b..cebd25a871 100644 --- a/shell-completion/bash/busctl +++ b/shell-completion/bash/busctl @@ -25,8 +25,9 @@ __contains_word () { __get_machines() { local a b - machinectl list --full --no-legend --no-pager 2>/dev/null | - { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __get_busnames() { diff --git a/shell-completion/bash/journalctl b/shell-completion/bash/journalctl index a1cfdb035c..067b05396d 100644 --- a/shell-completion/bash/journalctl +++ b/shell-completion/bash/journalctl @@ -27,8 +27,9 @@ __contains_word () { __get_machines() { local a b - (machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; echo ".host") | \ - { while read a b; do echo " $a"; done; } | sort -u; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __syslog_priorities=(emerg alert crit err warning notice info debug) diff --git a/shell-completion/bash/loginctl b/shell-completion/bash/loginctl index 7af848fdd1..b307e428be 100644 --- a/shell-completion/bash/loginctl +++ b/shell-completion/bash/loginctl @@ -31,8 +31,9 @@ __get_all_seats () { loginctl --no-legend list-seats | { while read -r a b __get_machines() { local a b - machinectl list --full --no-legend --no-pager 2>/dev/null | - { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _loginctl () { diff --git a/shell-completion/bash/machinectl b/shell-completion/bash/machinectl index ebac999897..b28769b0b6 100644 --- a/shell-completion/bash/machinectl +++ b/shell-completion/bash/machinectl @@ -25,8 +25,9 @@ __contains_word() { __get_machines() { local a b - (machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; echo ".host") | \ - { while read a b; do echo " $a"; done; } | sort -u; + { machinectl list-images --full --no-legend --no-pager 2>/dev/null; machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _machinectl() { diff --git a/shell-completion/bash/portablectl b/shell-completion/bash/portablectl index 2baafb7486..71789277b0 100644 --- a/shell-completion/bash/portablectl +++ b/shell-completion/bash/portablectl @@ -25,8 +25,9 @@ __contains_word () { __get_machines() { local a b - machinectl list --full --no-legend --no-pager 2>/dev/null | - { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _portablectl() { diff --git a/shell-completion/bash/systemctl.in b/shell-completion/bash/systemctl.in index a3e423b845..f935960c77 100644 --- a/shell-completion/bash/systemctl.in +++ b/shell-completion/bash/systemctl.in @@ -115,8 +115,9 @@ __get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while re __get_machines() { local a b - { machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; } | \ - { while read a b; do echo " $a"; done; } + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _systemctl () { diff --git a/shell-completion/bash/systemd-analyze b/shell-completion/bash/systemd-analyze index fe2c1d122c..b1baec9978 100644 --- a/shell-completion/bash/systemd-analyze +++ b/shell-completion/bash/systemd-analyze @@ -27,7 +27,9 @@ __contains_word () { __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __get_units_all() { diff --git a/shell-completion/bash/systemd-cgls b/shell-completion/bash/systemd-cgls index 19307f9ed1..8dda5a82dd 100644 --- a/shell-completion/bash/systemd-cgls +++ b/shell-completion/bash/systemd-cgls @@ -25,7 +25,9 @@ __contains_word() { __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __get_units_have_cgroup() { diff --git a/shell-completion/bash/systemd-cgtop b/shell-completion/bash/systemd-cgtop index ca0a51e949..731d9c924b 100644 --- a/shell-completion/bash/systemd-cgtop +++ b/shell-completion/bash/systemd-cgtop @@ -25,7 +25,9 @@ __contains_word() { __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _systemd_cgtop() { diff --git a/shell-completion/bash/systemd-nspawn b/shell-completion/bash/systemd-nspawn index fbc953f56e..cc3d2f6598 100644 --- a/shell-completion/bash/systemd-nspawn +++ b/shell-completion/bash/systemd-nspawn @@ -35,7 +35,9 @@ __get_slices() { __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __get_env() { diff --git a/shell-completion/bash/systemd-run b/shell-completion/bash/systemd-run index 24853b28b4..c18228d00f 100644 --- a/shell-completion/bash/systemd-run +++ b/shell-completion/bash/systemd-run @@ -26,7 +26,9 @@ __get_slice_units () { __systemctl $1 list-units --all -t slice \ __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } _systemd_run() { diff --git a/shell-completion/bash/timedatectl b/shell-completion/bash/timedatectl index 962fd6e37d..768b4a5e82 100644 --- a/shell-completion/bash/timedatectl +++ b/shell-completion/bash/timedatectl @@ -27,7 +27,9 @@ __contains_word () { __get_machines() { local a b - machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; }; + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo " $a"; done; } | \ + sort -u } __get_interfaces(){ diff --git a/shell-completion/zsh/_sd_machines b/shell-completion/zsh/_sd_machines index cc0d1e2b4e..b960745f0d 100644 --- a/shell-completion/zsh/_sd_machines +++ b/shell-completion/zsh/_sd_machines @@ -3,7 +3,10 @@ (( $+functions[__sd_machines_get_machines] )) || __sd_machines_get_machines () { - machinectl --full --no-legend --no-pager list | {while read -r a b; do echo $a; done;}; + + { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \ + { while read a b; do echo "$a"; done; } | \ + sort -u } local -a _machines -- cgit v1.2.3 From 9e7cc6f0d26b217b86dabadf8072802f63b40256 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Thu, 20 Oct 2022 09:45:24 +0200 Subject: shell-completion/zsh: rename helper for clarity --- shell-completion/zsh/_machinectl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell-completion/zsh/_machinectl b/shell-completion/zsh/_machinectl index 4b4d044ec8..2b675bd77e 100644 --- a/shell-completion/zsh/_machinectl +++ b/shell-completion/zsh/_machinectl @@ -1,15 +1,15 @@ #compdef machinectl # SPDX-License-Identifier: LGPL-2.1-or-later -(( $+functions[__machinectl_get_machines] )) || - __machinectl_get_machines () { +(( $+functions[__machinectl_get_images] )) || + __machinectl_get_images () { machinectl --no-legend list-images | {while read -r a b; do echo $a; done;} } -(( $+functions[_machinectl_machines] )) || - _machinectl_machines() { +(( $+functions[_machinectl_images] )) || + _machinectl_images() { local -a _machines - _machines=("${(fo)$(__machinectl_get_machines)}") + _machines=("${(fo)$(__machinectl_get_images)}") typeset -U _machines if [[ -n "$_machines" ]]; then _describe 'machines' _machines @@ -64,9 +64,9 @@ list*|cancel-transfer|pull-tar|pull-raw) msg="no options" ;; clone) - _machinectl_machines ;; + _machinectl_images ;; start) - _machinectl_machines ;; + _machinectl_images ;; *) _sd_machines esac -- cgit v1.2.3