]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- reuse code 1075/head
authorArvin Schnell <aschnell@suse.de>
Fri, 19 Dec 2025 08:41:24 +0000 (09:41 +0100)
committerArvin Schnell <aschnell@suse.de>
Fri, 19 Dec 2025 08:41:49 +0000 (09:41 +0100)
client/snbk/Makefile.am
client/snbk/cmd-delete.cc
client/snbk/cmd-restore.cc
client/snbk/cmd-transfer.cc
client/snbk/utils.cc [new file with mode: 0644]
client/snbk/utils.h [new file with mode: 0644]

index d229e1b664d25c1bf8b7da536e79fcd987349633..55e03022879c4f71e53b8e845b0dfab674e070cd 100644 (file)
@@ -21,7 +21,8 @@ snbk_SOURCES =                                                \
        Shell.cc                Shell.h                 \
        CmdBtrfs.cc             CmdBtrfs.h              \
        CmdLs.cc                CmdLs.h                 \
-       JsonFile.cc             JsonFile.h
+       JsonFile.cc             JsonFile.h              \
+       utils.cc                utils.h
 
 snbk_LDADD =                           \
        ../../snapper/libsnapper.la     \
index 7aac8698d24d1cb4c7e3082056e9824d5a8a0a52..64893e3b64807f33949f6725abc94f02c51842d3 100644 (file)
@@ -31,6 +31,7 @@
 #include "BackupConfig.h"
 #include "GlobalOptions.h"
 #include "TheBigThing.h"
+#include "utils.h"
 
 
 namespace snapper
@@ -52,21 +53,9 @@ namespace snapper
     command_delete(const GlobalOptions& global_options, GetOpts& get_opts, BackupConfigs& backup_configs,
                   ProxySnappers* snappers)
     {
-       static const regex num_regex("[0-9]+", regex::extended);
-
        ParsedOpts opts = get_opts.parse("delete", GetOpts::no_options);
 
-       vector<unsigned int> nums;
-
-       while (get_opts.has_args())
-       {
-           string arg = get_opts.pop_arg();
-
-           if (!regex_match(arg, num_regex))
-               SN_THROW(Exception(_("Failed to parse number.")));
-
-           nums.push_back(stoi(arg));
-       }
+       vector<unsigned int> nums = parse_nums(get_opts);
 
        unsigned int errors = 0;
 
index 345328a71866149e088527a77db25a8a059e3bd4..8b041573bf6ffea63f3d79215c30206434ddfbcb 100644 (file)
@@ -31,7 +31,7 @@
 #include "BackupConfig.h"
 #include "GlobalOptions.h"
 #include "TheBigThing.h"
-#include "cmd.h"
+#include "utils.h"
 
 
 namespace snapper
@@ -59,28 +59,16 @@ namespace snapper
     command_restore(const GlobalOptions& global_options, GetOpts& get_opts,
                    BackupConfigs& backup_configs, ProxySnappers* snappers)
     {
-       static const regex num_regex("[0-9]+", regex::extended);
-
        ParsedOpts opts = get_opts.parse("restore", GetOpts::no_options);
 
-       vector<unsigned int> nums;
-
-       while (get_opts.has_args())
-       {
-           string arg = get_opts.pop_arg();
-
-           if (!regex_match(arg, num_regex))
-               SN_THROW(Exception(_("Failed to parse number.")));
-
-           nums.push_back(stoi(arg));
-       }
-
        if (backup_configs.size() != 1)
        {
            SN_THROW(OptionsException(_("A backup-config must be specified to run this "
                                        "command.")));
        }
 
+       vector<unsigned int> nums = parse_nums(get_opts);
+
        unsigned int errors = 0;
 
        for (const BackupConfig& backup_config : backup_configs)
index d4242d37438e2ee005808e8b51692f89256c9826..82e90e0b6b27a9c40ef3b4be5f31cb1afdd584fa 100644 (file)
@@ -31,7 +31,7 @@
 #include "BackupConfig.h"
 #include "GlobalOptions.h"
 #include "TheBigThing.h"
-#include "cmd.h"
+#include "utils.h"
 
 
 namespace snapper
@@ -53,21 +53,9 @@ namespace snapper
     command_transfer(const GlobalOptions& global_options, GetOpts& get_opts, BackupConfigs& backup_configs,
                     ProxySnappers* snappers)
     {
-       static const regex num_regex("[0-9]+", regex::extended);
-
        ParsedOpts opts = get_opts.parse("transfer", GetOpts::no_options);
 
-       vector<unsigned int> nums;
-
-       while (get_opts.has_args())
-       {
-           string arg = get_opts.pop_arg();
-
-           if (!regex_match(arg, num_regex))
-               SN_THROW(Exception(_("Failed to parse number.")));
-
-           nums.push_back(stoi(arg));
-       }
+       vector<unsigned int> nums = parse_nums(get_opts);
 
        unsigned int errors = 0;
 
diff --git a/client/snbk/utils.cc b/client/snbk/utils.cc
new file mode 100644 (file)
index 0000000..b79eb00
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) [2024-2025] SUSE LLC
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#include <regex>
+
+#include "../utils/GetOpts.h"
+#include "../utils/text.h"
+
+
+namespace snapper
+{
+
+    vector<unsigned int>
+    parse_nums(GetOpts& get_opts)
+    {
+       static const regex num_regex("[0-9]+", regex::extended);
+
+       vector<unsigned int> nums;
+
+       while (get_opts.has_args())
+       {
+           string arg = get_opts.pop_arg();
+
+           if (!regex_match(arg, num_regex))
+               SN_THROW(Exception(_("Failed to parse number.")));
+
+           nums.push_back(stoi(arg));
+       }
+
+       return nums;
+    }
+
+}
diff --git a/client/snbk/utils.h b/client/snbk/utils.h
new file mode 100644 (file)
index 0000000..a50e28b
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) [2024-2025] SUSE LLC
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#include "../utils/GetOpts.h"
+
+
+namespace snapper
+{
+
+    vector<unsigned int>
+    parse_nums(GetOpts& get_opts);
+
+}