Merge pull request #2962 from keszybz/value-option

Add `--value` option to systemctl and loginctl to only print values
This commit is contained in:
Lennart Poettering 2016-04-06 10:47:37 +02:00
commit 382b56622a
8 changed files with 170 additions and 92 deletions

View file

@ -93,6 +93,16 @@
shown.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--value</option></term>
<listitem>
<para>When printing properties with <command>show</command>,
only print the value, and skip the property name and
<literal>=</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-a</option></term>
<term><option>--all</option></term>

View file

@ -136,6 +136,13 @@
(<literal>.</literal>).</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--value</option></term>
<listitem><para>When printing properties with <command>show</command>, only print the value,
and skip the property name and <literal>=</literal>.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-l</option></term>
<term><option>--full</option></term>

View file

@ -232,6 +232,16 @@
</listitem>
</varlistentry>
<varlistentry>
<term><option>--value</option></term>
<listitem>
<para>When printing properties with <command>show</command>,
only print the value, and skip the property name and
<literal>=</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--show-types</option></term>

View file

@ -48,6 +48,7 @@
static char **arg_property = NULL;
static bool arg_all = false;
static bool arg_value = false;
static bool arg_full = false;
static bool arg_no_pager = false;
static bool arg_legend = true;
@ -679,6 +680,14 @@ static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line)
return 0;
}
#define property(name, fmt, ...) \
do { \
if (arg_value) \
printf(fmt "\n", __VA_ARGS__); \
else \
printf("%s=" fmt "\n", name, __VA_ARGS__); \
} while(0)
static int print_property(const char *name, sd_bus_message *m, const char *contents) {
int r;
@ -702,7 +711,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
if (arg_all || !isempty(s))
printf("%s=%s\n", name, s);
property(name, "%s", s);
return 0;
@ -718,8 +727,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return -EINVAL;
}
printf("%s=" UID_FMT "\n", name, uid);
property(name, UID_FMT, uid);
return 0;
}
@ -735,14 +743,16 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
if (r < 0)
return bus_log_parse_error(r);
printf("%s=", name);
if (!arg_value)
printf("%s=", name);
while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
printf("%s%s", space ? " " : "", s);
space = true;
}
printf("\n");
if (space || !arg_value)
printf("\n");
if (r < 0)
return bus_log_parse_error(r);
@ -757,7 +767,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
break;
}
r = bus_print_property(name, m, arg_all);
r = bus_print_property(name, m, arg_value, arg_all);
if (r < 0)
return bus_log_parse_error(r);
@ -1330,6 +1340,7 @@ static int help(int argc, char *argv[], void *userdata) {
" -M --machine=CONTAINER Operate on local container\n"
" -p --property=NAME Show only properties by this name\n"
" -a --all Show all properties, including empty ones\n"
" --value When showing properties, only print the value\n"
" -l --full Do not ellipsize output\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
@ -1371,6 +1382,7 @@ static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_VALUE,
ARG_NO_PAGER,
ARG_NO_LEGEND,
ARG_KILL_WHO,
@ -1382,6 +1394,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "version", no_argument, NULL, ARG_VERSION },
{ "property", required_argument, NULL, 'p' },
{ "all", no_argument, NULL, 'a' },
{ "value", no_argument, NULL, ARG_VALUE },
{ "full", no_argument, NULL, 'l' },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
@ -1427,6 +1440,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_all = true;
break;
case ARG_VALUE:
arg_value = true;
break;
case 'l':
arg_full = true;
break;

View file

@ -61,6 +61,7 @@
static char **arg_property = NULL;
static bool arg_all = false;
static bool arg_value = false;
static bool arg_full = false;
static bool arg_no_pager = false;
static bool arg_legend = true;
@ -129,15 +130,14 @@ static int list_machines(int argc, char *argv[], void *userdata) {
pager_open(arg_no_pager, false);
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"ListMachines",
&error,
&reply,
NULL);
r = sd_bus_call_method(bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"ListMachines",
&error,
&reply,
NULL);
if (r < 0) {
log_error("Could not get machines: %s", bus_error_message(&error, -r));
return r;
@ -232,15 +232,14 @@ static int list_images(int argc, char *argv[], void *userdata) {
pager_open(arg_no_pager, false);
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"ListImages",
&error,
&reply,
"");
r = sd_bus_call_method(bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"ListImages",
&error,
&reply,
"");
if (r < 0) {
log_error("Could not get images: %s", bus_error_message(&error, -r));
return r;
@ -680,7 +679,7 @@ static int show_machine_properties(sd_bus *bus, const char *path, bool *new_line
*new_line = true;
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
if (r < 0)
log_error_errno(r, "Could not get properties: %m");
@ -713,15 +712,14 @@ static int show_machine(int argc, char *argv[], void *userdata) {
for (i = 1; i < argc; i++) {
const char *path = NULL;
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachine",
&error,
&reply,
"s", argv[i]);
r = sd_bus_call_method(bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachine",
&error,
&reply,
"s", argv[i]);
if (r < 0) {
log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
return r;
@ -929,7 +927,7 @@ static int show_image_properties(sd_bus *bus, const char *path, bool *new_line)
*new_line = true;
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
if (r < 0)
log_error_errno(r, "Could not get properties: %m");
@ -2183,15 +2181,14 @@ static int list_transfers(int argc, char *argv[], void *userdata) {
pager_open(arg_no_pager, false);
r = sd_bus_call_method(
bus,
"org.freedesktop.import1",
"/org/freedesktop/import1",
"org.freedesktop.import1.Manager",
"ListTransfers",
&error,
&reply,
NULL);
r = sd_bus_call_method(bus,
"org.freedesktop.import1",
"/org/freedesktop/import1",
"org.freedesktop.import1.Manager",
"ListTransfers",
&error,
&reply,
NULL);
if (r < 0) {
log_error("Could not get transfers: %s", bus_error_message(&error, -r));
return r;
@ -2356,6 +2353,7 @@ static int help(int argc, char *argv[], void *userdata) {
" -p --property=NAME Show only properties by this name\n"
" -q --quiet Suppress output\n"
" -a --all Show all properties, including empty ones\n"
" --value When showing properties, only print the value\n"
" -l --full Do not ellipsize output\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
@ -2418,6 +2416,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
ARG_NO_LEGEND,
ARG_VALUE,
ARG_KILL_WHO,
ARG_READ_ONLY,
ARG_MKDIR,
@ -2434,6 +2433,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "version", no_argument, NULL, ARG_VERSION },
{ "property", required_argument, NULL, 'p' },
{ "all", no_argument, NULL, 'a' },
{ "value", no_argument, NULL, ARG_VALUE },
{ "full", no_argument, NULL, 'l' },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
@ -2485,6 +2485,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_all = true;
break;
case ARG_VALUE:
arg_value = true;
break;
case 'l':
arg_full = true;
break;

View file

@ -712,7 +712,15 @@ int bus_connect_user_systemd(sd_bus **_bus) {
return 0;
}
int bus_print_property(const char *name, sd_bus_message *property, bool all) {
#define print_property(name, fmt, ...) \
do { \
if (value) \
printf(fmt "\n", __VA_ARGS__); \
else \
printf("%s=" fmt "\n", name, __VA_ARGS__); \
} while(0)
int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
char type;
const char *contents;
int r;
@ -740,7 +748,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (!escaped)
return -ENOMEM;
printf("%s=%s\n", name, escaped);
print_property(name, "%s", escaped);
}
return 1;
@ -753,7 +761,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (r < 0)
return r;
printf("%s=%s\n", name, yes_no(b));
print_property(name, "%s", yes_no(b));
return 1;
}
@ -773,14 +781,14 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
t = format_timestamp(timestamp, sizeof(timestamp), u);
if (t || all)
printf("%s=%s\n", name, strempty(t));
print_property(name, "%s", strempty(t));
} else if (strstr(name, "USec")) {
char timespan[FORMAT_TIMESPAN_MAX];
printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u, 0));
print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
} else
printf("%s=%llu\n", name, (unsigned long long) u);
print_property(name, "%"PRIu64, u);
return 1;
}
@ -792,7 +800,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (r < 0)
return r;
printf("%s=%lld\n", name, (long long) i);
print_property(name, "%"PRIi64, i);
return 1;
}
@ -805,9 +813,9 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
return r;
if (strstr(name, "UMask") || strstr(name, "Mode"))
printf("%s=%04o\n", name, u);
print_property(name, "%04o", u);
else
printf("%s=%u\n", name, (unsigned) u);
print_property(name, "%"PRIu32, u);
return 1;
}
@ -819,7 +827,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (r < 0)
return r;
printf("%s=%i\n", name, (int) i);
print_property(name, "%"PRIi32, i);
return 1;
}
@ -830,7 +838,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (r < 0)
return r;
printf("%s=%g\n", name, d);
print_property(name, "%g", d);
return 1;
}
@ -846,7 +854,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
_cleanup_free_ char *escaped = NULL;
if (first)
if (first && !value)
printf("%s=", name);
escaped = xescape(str, "\n ");
@ -860,7 +868,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (r < 0)
return r;
if (first && all)
if (first && all && !value)
printf("%s=", name);
if (!first || all)
puts("");
@ -882,7 +890,8 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (all || n > 0) {
unsigned int i;
printf("%s=", name);
if (!value)
printf("%s=", name);
for (i = 0; i < n; i++)
printf("%02x", u[i]);
@ -903,7 +912,8 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
if (all || n > 0) {
unsigned int i;
printf("%s=", name);
if (!value)
printf("%s=", name);
for (i = 0; i < n; i++)
printf("%08x", u[i]);
@ -920,7 +930,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool all) {
return 0;
}
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all) {
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
@ -960,7 +970,7 @@ int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, ch
if (r < 0)
return r;
r = bus_print_property(name, reply, all);
r = bus_print_property(name, reply, value, all);
if (r < 0)
return r;
if (r == 0) {

View file

@ -78,8 +78,8 @@ int bus_connect_user_systemd(sd_bus **_bus);
int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus);
int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
int bus_print_property(const char *name, sd_bus_message *property, bool all);
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all);
int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all);
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all);
int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);

View file

@ -103,6 +103,7 @@ static bool arg_no_pager = false;
static bool arg_no_wtmp = false;
static bool arg_no_wall = false;
static bool arg_no_reload = false;
static bool arg_value = false;
static bool arg_show_types = false;
static bool arg_ignore_inhibitors = false;
static bool arg_dry = false;
@ -4116,6 +4117,14 @@ skip:
return 0;
}
#define print_prop(name, fmt, ...) \
do { \
if (arg_value) \
printf(fmt "\n", __VA_ARGS__); \
else \
printf("%s=" fmt "\n", name, __VA_ARGS__); \
} while(0)
static int print_property(const char *name, sd_bus_message *m, const char *contents) {
int r;
@ -4143,9 +4152,9 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
if (u > 0)
printf("%s=%"PRIu32"\n", name, u);
print_prop(name, "%"PRIu32, u);
else if (arg_all)
printf("%s=\n", name);
print_prop(name, "%s", "");
return 0;
@ -4157,7 +4166,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
if (arg_all || !isempty(s))
printf("%s=%s\n", name, s);
print_prop(name, "%s", s);
return 0;
@ -4169,7 +4178,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
if (arg_all || !isempty(a) || !isempty(b))
printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
print_prop(name, "%s \"%s\"", strempty(a), strempty(b));
return 0;
} else if (streq_ptr(name, "SystemCallFilter")) {
@ -4196,8 +4205,10 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
bool first = true;
char **i;
fputs(name, stdout);
fputc('=', stdout);
if (!arg_value) {
fputs(name, stdout);
fputc('=', stdout);
}
if (!whitelist)
fputc('~', stdout);
@ -4229,7 +4240,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0)
printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
print_prop("EnvironmentFile", "%s (ignore_errors=%s)\n", path, yes_no(ignore));
if (r < 0)
return bus_log_parse_error(r);
@ -4248,7 +4259,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
printf("%s=%s\n", type, path);
print_prop(type, "%s", path);
if (r < 0)
return bus_log_parse_error(r);
@ -4266,7 +4277,10 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
printf("Listen%s=%s\n", type, path);
if (arg_value)
puts(path);
else
printf("Listen%s=%s\n", type, path);
if (r < 0)
return bus_log_parse_error(r);
@ -4287,10 +4301,9 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
while ((r = sd_bus_message_read(m, "(stt)", &base, &value, &next_elapse)) > 0) {
char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
printf("%s={ value=%s ; next_elapse=%s }\n",
base,
format_timespan(timespan1, sizeof(timespan1), value, 0),
format_timespan(timespan2, sizeof(timespan2), next_elapse, 0));
print_prop(base, "{ value=%s ; next_elapse=%s }",
format_timespan(timespan1, sizeof(timespan1), value, 0),
format_timespan(timespan2, sizeof(timespan2), next_elapse, 0));
}
if (r < 0)
return bus_log_parse_error(r);
@ -4314,18 +4327,18 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
tt = strv_join(info.argv, " ");
printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }\n",
name,
strna(info.path),
strna(tt),
yes_no(info.ignore),
strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
info.pid,
sigchld_code_to_string(info.code),
info.status,
info.code == CLD_EXITED ? "" : "/",
strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
print_prop(name,
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }",
strna(info.path),
strna(tt),
yes_no(info.ignore),
strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
info.pid,
sigchld_code_to_string(info.code),
info.status,
info.code == CLD_EXITED ? "" : "/",
strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
free(info.path);
strv_free(info.argv);
@ -4346,7 +4359,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0)
printf("%s=%s %s\n", name, strna(path), strna(rwm));
print_prop(name, "%s %s", strna(path), strna(rwm));
if (r < 0)
return bus_log_parse_error(r);
@ -4365,7 +4378,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0)
printf("%s=%s %" PRIu64 "\n", name, strna(path), weight);
print_prop(name, "%s %"PRIu64, strna(path), weight);
if (r < 0)
return bus_log_parse_error(r);
@ -4384,7 +4397,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0)
printf("%s=%s %" PRIu64 "\n", name, strna(path), bandwidth);
print_prop(name, "%s %"PRIu64, strna(path), bandwidth);
if (r < 0)
return bus_log_parse_error(r);
@ -4398,7 +4411,7 @@ static int print_property(const char *name, sd_bus_message *m, const char *conte
break;
}
r = bus_print_property(name, m, arg_all);
r = bus_print_property(name, m, arg_value, arg_all);
if (r < 0)
return bus_log_parse_error(r);
@ -6243,6 +6256,7 @@ static void systemctl_help(void) {
" --job-mode=MODE Specify how to deal with already queued jobs, when\n"
" queueing a new job\n"
" --show-types When showing sockets, explicitly show their type\n"
" --value When showing properties, only print the value\n"
" -i --ignore-inhibitors\n"
" When shutting down or sleeping, ignore inhibitors\n"
" --kill-who=WHO Who to send signal to\n"
@ -6494,6 +6508,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
ARG_SHOW_TYPES,
ARG_IRREVERSIBLE,
ARG_IGNORE_DEPENDENCIES,
ARG_VALUE,
ARG_VERSION,
ARG_USER,
ARG_SYSTEM,
@ -6535,6 +6550,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
{ "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */
{ "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
{ "ignore-inhibitors", no_argument, NULL, 'i' },
{ "value", no_argument, NULL, ARG_VALUE },
{ "user", no_argument, NULL, ARG_USER },
{ "system", no_argument, NULL, ARG_SYSTEM },
{ "global", no_argument, NULL, ARG_GLOBAL },
@ -6686,6 +6702,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
arg_show_types = true;
break;
case ARG_VALUE:
arg_value = true;
break;
case ARG_JOB_MODE:
arg_job_mode = optarg;
break;