[libosinfo PATCH 3/3] osinfo-query: Print os variants information

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Os variants information has never been added to osinfo-query and it may
be useful for some apps to match the output of osinfo-query with
osinfo-detect (that's what Cockpit has been doing).

In order to do so, we have to:
- Add a new "variants" entry to be displayed and this new entry is not
  displayed by default;
- Take advantage of "real_prop" patch and present this as "variants"
  while actually looking for "name" (on OsinfoVariant);
- Take advantage of osinfo_os_list_func and search in order to have the
  list of os variants and display then.

There's a drawback for this approach (which has also been documented in
the man page), which is not being able to filter or sort for variants.

https://gitlab.com/libosinfo/libosinfo/issues/24

Signed-off-by: Fabiano Fidêncio <fidencio@xxxxxxxxxx>
---
 tools/osinfo-query.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/tools/osinfo-query.c b/tools/osinfo-query.c
index dff2bd7..dfb897e 100644
--- a/tools/osinfo-query.c
+++ b/tools/osinfo-query.c
@@ -75,6 +75,8 @@ static struct OsinfoLabel os_labels[] = {
       N_("Code name"), FALSE, 10, NULL },
     { OSINFO_ENTITY_PROP_ID, NULL,
       N_("ID"), TRUE, 40, NULL },
+    { "variants", OSINFO_OS_VARIANT_PROP_NAME,
+      N_("OS Variants"), FALSE, 90, (osinfo_os_list_func)osinfo_os_get_variant_list },
     { NULL, NULL, NULL, 0, 0, NULL}
 };
 
@@ -239,7 +241,10 @@ static gboolean print_entity_text(OsinfoEntity *entity,
     for (i = 0; labels[i].prop != NULL; i++) {
         gsize pad;
         gchar *padstr;
-        const gchar *val = osinfo_entity_get_param_value(entity, labels[i].prop);
+        gchar *str = NULL;
+        const gchar *val = NULL;
+        const gchar *prop;
+
         if (!labels[i].enabled)
             continue;
 
@@ -249,6 +254,37 @@ static gboolean print_entity_text(OsinfoEntity *entity,
             g_print(" | ");
         first = FALSE;
 
+        prop = labels[i].real_prop != NULL ? labels[i].real_prop : labels[i].prop;
+        if (labels[i].listOsFunc == NULL) {
+            val = osinfo_entity_get_param_value(entity, prop);
+        } else {
+            if (OSINFO_IS_OS(entity)) {
+                OsinfoList *list = labels[i].listOsFunc(OSINFO_OS(entity));
+                gsize j, list_len;
+
+                list_len = osinfo_list_get_length(list);
+                for (j = 0; j < list_len; j++) {
+                    OsinfoEntity *e = osinfo_list_get_nth(list, j);
+                    const gchar *value = osinfo_entity_get_param_value(e, prop);
+
+                    if (j == 0) {
+                        str = g_strdup_printf("%s", value);
+                    } else {
+                        gchar *tmp;
+
+                        /* Don't add duplicated entries */
+                        if (strstr(str, value) != NULL)
+                            continue;
+
+                        tmp = g_strdup_printf("%s, %s", str, value);
+                        g_free(str);
+                        str = tmp;
+                    }
+                }
+                val = str;
+            }
+        }
+
         if (val && (strlen(val) > labels[i].width))
             pad = 0;
         else
@@ -263,6 +299,7 @@ static gboolean print_entity_text(OsinfoEntity *entity,
         g_print("%s%s",
                 val ? val : "", padstr);
         g_free(padstr);
+        g_free(str);
     }
     g_print("\n");
 
@@ -598,6 +635,11 @@ The OS code name
 
 The OS identifier
 
+=item B<variants>
+
+The OS variants. Note, though, that no filter or sort can be applied to this
+property.
+
 =back
 
 
-- 
2.20.1

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux