Skip to content

Commit 7026a77

Browse files
committed
machinectl: tweak address output in "machinectl status"
With this change we'll not show an "Addresses" field for machines that we don't know any addresses for. This changes print_addresses() to never suffix its output with a newline, leaving that to the caller. That's a good idea since depending on who the caller is, different rules apply: if no addresses are found, then the list view still wants a newline, but the status view does not. This also changes the function to return the number of found addresses, which can be used to decide when to add a newline or not.
1 parent 3401419 commit 7026a77

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/machine/machinectl.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,10 @@ static int list_machines(int argc, char *argv[], void *userdata) {
327327
(int) max_version_id, strdash_if_empty(machines[j].version_id));
328328

329329
r = print_addresses(bus, machines[j].name, 0, "", prefix, arg_addrs);
330-
if (r == -EOPNOTSUPP)
331-
printf("-\n");
330+
if (r <= 0) /* error or no addresses defined? */
331+
fputs("-\n", stdout);
332+
else
333+
fputc('\n', stdout);
332334
}
333335

334336
if (arg_legend) {
@@ -520,6 +522,7 @@ static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *p
520522
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
521523
_cleanup_free_ char *addresses = NULL;
522524
bool truncate = false;
525+
unsigned n = 0;
523526
int r;
524527

525528
assert(bus);
@@ -567,7 +570,7 @@ static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *p
567570
else
568571
strcpy(buf_ifi, "");
569572

570-
if(!strextend(&addresses, prefix, inet_ntop(family, a, buffer, sizeof(buffer)), buf_ifi, NULL))
573+
if (!strextend(&addresses, prefix, inet_ntop(family, a, buffer, sizeof(buffer)), buf_ifi, NULL))
571574
return log_oom();
572575
} else
573576
truncate = true;
@@ -581,6 +584,8 @@ static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *p
581584

582585
if (n_addr > 0)
583586
n_addr -= 1;
587+
588+
n++;
584589
}
585590
if (r < 0)
586591
return bus_log_parse_error(r);
@@ -589,8 +594,10 @@ static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *p
589594
if (r < 0)
590595
return bus_log_parse_error(r);
591596

592-
fprintf(stdout, "%s%s\n", addresses, truncate ? "..." : "");
593-
return 0;
597+
if (n > 0)
598+
fprintf(stdout, "%s%s", addresses, truncate ? "..." : "");
599+
600+
return (int) n;
594601
}
595602

596603
static int print_os_release(sd_bus *bus, const char *method, const char *name, const char *prefix) {
@@ -738,10 +745,11 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
738745
fputc('\n', stdout);
739746
}
740747

741-
print_addresses(bus, i->name, ifi,
742-
"\t Address: ",
743-
"\n\t ",
744-
ALL_IP_ADDRESSES);
748+
if (print_addresses(bus, i->name, ifi,
749+
"\t Address: ",
750+
"\n\t ",
751+
ALL_IP_ADDRESSES) > 0)
752+
fputc('\n', stdout);
745753

746754
print_os_release(bus, "GetMachineOSRelease", i->name, "\t OS: ");
747755

0 commit comments

Comments
 (0)