]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homectl: use table to parse --export-format=
authorDavid Tardon <dtardon@redhat.com>
Fri, 26 Sep 2025 13:17:12 +0000 (15:17 +0200)
committerDavid Tardon <dtardon@redhat.com>
Thu, 2 Oct 2025 14:13:40 +0000 (16:13 +0200)
src/home/homectl.c

index 6c83765766b84265ce2b5295b56c5e8331f92f63..a73d94744ad7ede646cd31dc8a677a1845831eee 100644 (file)
@@ -56,6 +56,7 @@
 #include "rlimit-util.h"
 #include "runtime-scope.h"
 #include "stat-util.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
 #include "userdb.h"
 #include "verbs.h"
 
+typedef enum {
+        EXPORT_FORMAT_FULL,          /* export the full record */
+        EXPORT_FORMAT_STRIPPED,      /* strip "state" + "binding", but leave signature in place */
+        EXPORT_FORMAT_MINIMAL,       /* also strip signature */
+        _EXPORT_FORMAT_MAX,
+        _EXPORT_FORMAT_INVALID = -EINVAL,
+} ExportFormat;
+
 static PagerFlags arg_pager_flags = 0;
 static bool arg_legend = true;
 static bool arg_ask_password = true;
@@ -97,11 +106,7 @@ static bool arg_recovery_key = false;
 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
 static bool arg_and_resize = false;
 static bool arg_and_change_password = false;
-static enum {
-        EXPORT_FORMAT_FULL,          /* export the full record */
-        EXPORT_FORMAT_STRIPPED,      /* strip "state" + "binding", but leave signature in place */
-        EXPORT_FORMAT_MINIMAL,       /* also strip signature */
-} arg_export_format = EXPORT_FORMAT_FULL;
+static ExportFormat arg_export_format = EXPORT_FORMAT_FULL;
 static uint64_t arg_capability_bounding_set = UINT64_MAX;
 static uint64_t arg_capability_ambient_set = UINT64_MAX;
 static char *arg_blob_dir = NULL;
@@ -131,6 +136,14 @@ STATIC_DESTRUCTOR_REGISTER(arg_key_name, freep);
 
 static const BusLocator *bus_mgr;
 
+static const char *export_format_table[_EXPORT_FORMAT_MAX] = {
+        [EXPORT_FORMAT_FULL]     = "full",
+        [EXPORT_FORMAT_STRIPPED] = "stripped",
+        [EXPORT_FORMAT_MINIMAL]  = "minimal",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(export_format, ExportFormat);
+
 static bool identity_properties_specified(void) {
         return
                 arg_identity ||
@@ -4732,18 +4745,12 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_EXPORT_FORMAT:
-                        if (streq(optarg, "full"))
-                                arg_export_format = EXPORT_FORMAT_FULL;
-                        else if (streq(optarg, "stripped"))
-                                arg_export_format = EXPORT_FORMAT_STRIPPED;
-                        else if (streq(optarg, "minimal"))
-                                arg_export_format = EXPORT_FORMAT_MINIMAL;
-                        else if (streq(optarg, "help")) {
-                                puts("full\n"
-                                     "stripped\n"
-                                     "minimal");
-                                return 0;
-                        }
+                        if (streq(optarg, "help"))
+                                return DUMP_STRING_TABLE(export_format, ExportFormat, _EXPORT_FORMAT_MAX);
+
+                        arg_export_format = export_format_from_string(optarg);
+                        if (arg_export_format < 0)
+                                return log_error_errno(arg_export_format, "Invalid export format: %s", optarg);
 
                         break;