From: Arvin Schnell Date: Fri, 19 Dec 2025 08:41:24 +0000 (+0100) Subject: - reuse code X-Git-Tag: v0.13.1~60^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1075%2Fhead;p=thirdparty%2Fsnapper.git - reuse code --- diff --git a/client/snbk/Makefile.am b/client/snbk/Makefile.am index d229e1b6..55e03022 100644 --- a/client/snbk/Makefile.am +++ b/client/snbk/Makefile.am @@ -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 \ diff --git a/client/snbk/cmd-delete.cc b/client/snbk/cmd-delete.cc index 7aac8698..64893e3b 100644 --- a/client/snbk/cmd-delete.cc +++ b/client/snbk/cmd-delete.cc @@ -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 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 nums = parse_nums(get_opts); unsigned int errors = 0; diff --git a/client/snbk/cmd-restore.cc b/client/snbk/cmd-restore.cc index 345328a7..8b041573 100644 --- a/client/snbk/cmd-restore.cc +++ b/client/snbk/cmd-restore.cc @@ -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 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 nums = parse_nums(get_opts); + unsigned int errors = 0; for (const BackupConfig& backup_config : backup_configs) diff --git a/client/snbk/cmd-transfer.cc b/client/snbk/cmd-transfer.cc index d4242d37..82e90e0b 100644 --- a/client/snbk/cmd-transfer.cc +++ b/client/snbk/cmd-transfer.cc @@ -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 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 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 index 00000000..b79eb00c --- /dev/null +++ b/client/snbk/utils.cc @@ -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 + +#include "../utils/GetOpts.h" +#include "../utils/text.h" + + +namespace snapper +{ + + vector + parse_nums(GetOpts& get_opts) + { + static const regex num_regex("[0-9]+", regex::extended); + + vector 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 index 00000000..a50e28b4 --- /dev/null +++ b/client/snbk/utils.h @@ -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 + parse_nums(GetOpts& get_opts); + +}