Skip to content

Commit 8570b98

Browse files
committed
dissect: add helper call for unifying three loops
1 parent ae54abe commit 8570b98

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

src/dissect/dissect.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ static int strv_pair_to_json(char **l, JsonVariant **ret) {
369369
return json_variant_new_array_strv(ret, jl);
370370
}
371371

372+
static void strv_pair_print(char **l, const char *prefix) {
373+
char **p, **q;
374+
375+
assert(prefix);
376+
377+
STRV_FOREACH_PAIR(p, q, l) {
378+
if (p == l)
379+
printf("%s %s=%s\n", prefix, *p, *q);
380+
else
381+
printf("%*s %s=%s\n", (int) strlen(prefix), "", *p, *q);
382+
}
383+
}
384+
372385
static int action_dissect(DissectedImage *m, LoopDevice *d) {
373386
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
374387
_cleanup_(table_unrefp) Table *t = NULL;
@@ -412,38 +425,18 @@ static int action_dissect(DissectedImage *m, LoopDevice *d) {
412425
if (!sd_id128_is_null(m->machine_id))
413426
printf("Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->machine_id));
414427

415-
if (!strv_isempty(m->machine_info)) {
416-
char **p, **q;
417-
418-
STRV_FOREACH_PAIR(p, q, m->machine_info)
419-
printf("%s %s=%s\n",
420-
p == m->machine_info ? "Mach. Info:" : " ",
421-
*p, *q);
422-
}
423-
424-
if (!strv_isempty(m->os_release)) {
425-
char **p, **q;
426-
427-
STRV_FOREACH_PAIR(p, q, m->os_release)
428-
printf("%s %s=%s\n",
429-
p == m->os_release ? "OS Release:" : " ",
430-
*p, *q);
431-
}
432-
433-
if (!strv_isempty(m->extension_release)) {
434-
char **p, **q;
435-
436-
STRV_FOREACH_PAIR(p, q, m->extension_release)
437-
printf("%s %s=%s\n",
438-
p == m->extension_release ? "Extension Release:" : " ",
439-
*p, *q);
440-
}
428+
strv_pair_print(m->machine_info,
429+
"Mach. Info:");
430+
strv_pair_print(m->os_release,
431+
"OS Release:");
432+
strv_pair_print(m->extension_release,
433+
" Ext. Rel.:");
441434

442435
if (m->hostname ||
443436
!sd_id128_is_null(m->machine_id) ||
444437
!strv_isempty(m->machine_info) ||
445-
!strv_isempty(m->extension_release) ||
446-
!strv_isempty(m->os_release))
438+
!strv_isempty(m->os_release) ||
439+
!strv_isempty(m->extension_release))
447440
putc('\n', stdout);
448441
} else {
449442
_cleanup_(json_variant_unrefp) JsonVariant *mi = NULL, *osr = NULL, *exr = NULL;

0 commit comments

Comments
 (0)