From: José Iván López González Date: Wed, 30 Oct 2019 23:19:29 +0000 (+0000) Subject: Add GetConfig command X-Git-Tag: v0.8.6~10^2~8 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=dd677f49ffe08548a6ab2f5044aefd91206863cd;p=thirdparty%2Fsnapper.git Add GetConfig command --- diff --git a/client/Command/GetConfig.cc b/client/Command/GetConfig.cc new file mode 100644 index 00000000..f235e081 --- /dev/null +++ b/client/Command/GetConfig.cc @@ -0,0 +1,115 @@ +/* + * Copyright (c) [2019] 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 "client/utils/text.h" +#include "client/Command/GetConfig.h" +#include "client/Command/GetConfig/Options.h" +#include "client/Command/GetConfig/ConfigData/Table.h" +#include "client/Command/GetConfig/ConfigData/Csv.h" +#include "client/Command/GetConfig/ConfigData/Json.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + string Command::GetConfig::help() + { + return + string( + _(" Get configs:\n" + "\tsnapper get-config\n" + "\n") + ) + Options::help_text(); + } + + + Command::GetConfig::GetConfig(const GlobalOptions& global_options, + GetOpts& options_parser, ProxySnappers& snappers) : + Command(global_options, options_parser, snappers), _options(new Options(options_parser)) + {} + + + Command::GetConfig::GetConfig::~GetConfig() + {} + + + const Command::GetConfig::Options& Command::GetConfig::options() const + { + return *_options.get(); + } + + + vector Command::GetConfig::errors() const + { + return options().errors(); + } + + + void Command::GetConfig::run() + { + if (_options_parser.hasArgs()) + { + cerr << _("Command 'get-config' does not take arguments.") << endl; + exit(EXIT_FAILURE); + } + + if (options().has_errors()) + { + cerr << options().errors().front() << endl; + exit(EXIT_FAILURE); + } + + string output; + + switch (global_options().output_format()) + { + case GlobalOptions::OutputFormat::TABLE: + { + ConfigData::Table table(*this, global_options().table_style()); + output = table.output(); + } + break; + + case GlobalOptions::OutputFormat::CSV: + { + ConfigData::Csv csv(*this, global_options().separator()); + output = csv.output(); + } + break; + + case GlobalOptions::OutputFormat::JSON: + { + ConfigData::Json json(*this); + output = json.output() + "\n"; + } + break; + } + + cout << output; + } + + } +} diff --git a/client/Command/GetConfig.h b/client/Command/GetConfig.h new file mode 100644 index 00000000..6ef727b6 --- /dev/null +++ b/client/Command/GetConfig.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIG_H +#define SNAPPER_CLI_COMMAND_GET_CONFIG_H + +#include + +#include "client/Command.h" +#include "client/proxy.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig : public Command + { + + public: + + class Options; + + class ColumnsOption; + + class ConfigData; + + static std::string help(); + + GetConfig(const GlobalOptions& global_options, GetOpts& options_parser, + ProxySnappers& snappers); + + const Options& options() const; + + virtual std::vector errors() const override; + + virtual void run() override; + + virtual ~GetConfig(); + + private: + + std::unique_ptr _options; + }; + + } +} + +#endif diff --git a/client/Command/GetConfig/ConfigData.cc b/client/Command/GetConfig/ConfigData.cc new file mode 100644 index 00000000..2b18e697 --- /dev/null +++ b/client/Command/GetConfig/ConfigData.cc @@ -0,0 +1,53 @@ +/* + * Copyright (c) [2019] 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 "client/Command/GetConfig/ConfigData.h" +#include "client/Command/GetConfig/Options.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + Command::GetConfig::ConfigData::ConfigData(const Command::GetConfig& command) : + _command(command) + {} + + + Command::GetConfig::ConfigData::~ConfigData() + {} + + + map Command::GetConfig::ConfigData::values() const + { + const string config_name = _command.global_options().config(); + + const ProxySnapper* snapper = _command.snappers().getSnapper(config_name); + + const ProxyConfig config = snapper->getConfig(); + + return config.getAllValues(); + } + + } +} diff --git a/client/Command/GetConfig/ConfigData.h b/client/Command/GetConfig/ConfigData.h new file mode 100644 index 00000000..670ef9b2 --- /dev/null +++ b/client/Command/GetConfig/ConfigData.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIGS_CONFIG_DATA_H +#define SNAPPER_CLI_COMMAND_GET_CONFIGS_CONFIG_DATA_H + +#include +#include + +#include "client/Command/GetConfig.h" +#include "client/proxy.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig::ConfigData + { + + public: + + class Table; + + class Csv; + + class Json; + + ConfigData(const Command::GetConfig& command); + + virtual ~ConfigData(); + + virtual std::string output() const = 0; + + protected: + + std::map values() const; + + private: + + const GetConfig& _command; + }; + + } +} + +#endif diff --git a/client/Command/GetConfig/ConfigData/Csv.cc b/client/Command/GetConfig/ConfigData/Csv.cc new file mode 100644 index 00000000..a31bd37b --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Csv.cc @@ -0,0 +1,71 @@ +/* + * Copyright (c) [2019] 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 "client/Command/GetConfig/ConfigData/Csv.h" +#include "client/Command/GetConfig/Options.h" +#include "client/utils/CsvFormatter.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + Command::GetConfig::ConfigData::Csv::Csv( + const Command::GetConfig& command, const string separator) : + ConfigData(command), _separator(separator) + {} + + + string Command::GetConfig::ConfigData::Csv::output() const + { + vector columns; + + vector> rows; + + for (const string& column : _command.options().columns()) + columns.emplace_back(column); + + for (const map::value_type& value : values()) + { + vector row; + + for (const string& column : _command.options().columns()) + { + if (column == Options::Columns::KEY) + row.push_back(value.first); + else + row.push_back(value.second); + } + + rows.push_back(row); + } + + CsvFormatter formatter(columns, rows, _separator); + + return formatter.output(); + } + + } +} diff --git a/client/Command/GetConfig/ConfigData/Csv.h b/client/Command/GetConfig/ConfigData/Csv.h new file mode 100644 index 00000000..54bf04e1 --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Csv.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_CSV_H +#define SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_CSV_H + +#include + +#include "client/Command/GetConfig/ConfigData.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig::ConfigData::Csv : public Command::GetConfig::ConfigData + { + + public: + + Csv(const GetConfig& command, const std::string separator); + + virtual std::string output() const override; + + private: + + const std::string _separator; + }; + + } +} + +#endif diff --git a/client/Command/GetConfig/ConfigData/Json.cc b/client/Command/GetConfig/ConfigData/Json.cc new file mode 100644 index 00000000..823d11a6 --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Json.cc @@ -0,0 +1,45 @@ +/* + * Copyright (c) [2019] 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 "client/Command/GetConfig/ConfigData/Json.h" +#include "client/utils/JsonFormatter.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + string Command::GetConfig::ConfigData::Json::output() const + { + JsonFormatter::Data data; + + for (const map::value_type& value : values()) + data.emplace_back(value.first, value.second); + + JsonFormatter formatter(data); + + return formatter.output(); + } + + } +} diff --git a/client/Command/GetConfig/ConfigData/Json.h b/client/Command/GetConfig/ConfigData/Json.h new file mode 100644 index 00000000..403a5bc4 --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Json.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_JSON_H +#define SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_JSON_H + +#include "client/Command/GetConfig/ConfigData.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig::ConfigData::Json : public Command::GetConfig::ConfigData + { + + public: + + using Command::GetConfig::ConfigData::ConfigData; + + virtual std::string output() const override; + + }; + + } +} + +#endif diff --git a/client/Command/GetConfig/ConfigData/Makefile.am b/client/Command/GetConfig/ConfigData/Makefile.am new file mode 100644 index 00000000..c0f4774c --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Makefile.am @@ -0,0 +1,17 @@ +# +# Makefile.am for snapper/client/Command/GetConfig/ConfigData +# + +AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS) + +noinst_LTLIBRARIES = libcommandgetconfigconfigdata.la + +libcommandgetconfigconfigdata_la_SOURCES = \ + Table.cc Table.h \ + Csv.cc Csv.h \ + Json.cc Json.h + +libcommandgetconfigconfigdata_la_LIBADD = \ + ../../../../snapper/libsnapper.la \ + ../../../utils/libutils.la \ + ../../../../dbus/libdbus.la diff --git a/client/Command/GetConfig/ConfigData/Table.cc b/client/Command/GetConfig/ConfigData/Table.cc new file mode 100644 index 00000000..00e62d61 --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Table.cc @@ -0,0 +1,81 @@ +/* + * Copyright (c) [2019] 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 "client/Command/GetConfig/ConfigData/Table.h" +#include "client/Command/GetConfig/Options.h" +#include "client/utils/TableFormatter.h" +#include "client/utils/text.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + Command::GetConfig::ConfigData::Table::Table( + const Command::GetConfig& command, TableLineStyle style) : + ConfigData(command), _style(style) + {} + + + string Command::GetConfig::ConfigData::Table::output() const + { + vector> columns; + + vector> rows; + + for (const string& column : _command.options().columns()) + columns.emplace_back(label_for(column), TableAlign::LEFT); + + for (const map::value_type& value : values()) + { + vector row; + + for (const string& column : _command.options().columns()) + { + if (column == Options::Columns::KEY) + row.push_back(value.first); + else + row.push_back(value.second); + } + + rows.push_back(row); + } + + TableFormatter formatter(columns, rows, _style); + + return formatter.output(); + } + + + string Command::GetConfig::ConfigData::Table::label_for(const string& column) const + { + if (column == Options::Columns::KEY) + return _("Key"); + + if (column == Options::Columns::VALUE) + return _("Value"); + + return ""; + } + } +} diff --git a/client/Command/GetConfig/ConfigData/Table.h b/client/Command/GetConfig/ConfigData/Table.h new file mode 100644 index 00000000..a50fc46e --- /dev/null +++ b/client/Command/GetConfig/ConfigData/Table.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_TABLE_H +#define SNAPPER_CLI_COMMAND_GET_CONFIG_CONFIG_DATA_TABLE_H + +#include + +#include "client/Command/GetConfig/ConfigData.h" +#include "client/utils/Table.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig::ConfigData::Table : public Command::GetConfig::ConfigData + { + + public: + + Table(const GetConfig& command, TableLineStyle style); + + virtual std::string output() const override; + + private: + + std::string label_for(const string& column) const; + + TableLineStyle _style; + + }; + + } +} + +#endif diff --git a/client/Command/GetConfig/Makefile.am b/client/Command/GetConfig/Makefile.am new file mode 100644 index 00000000..10d0d1e0 --- /dev/null +++ b/client/Command/GetConfig/Makefile.am @@ -0,0 +1,19 @@ +# +# Makefile.am for snapper/client/Command/GetConfig +# + +SUBDIRS = ConfigData + +AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS) + +noinst_LTLIBRARIES = libcommandgetconfig.la + +libcommandgetconfig_la_SOURCES = \ + Options.cc Options.h \ + ConfigData.cc ConfigData.h + +libcommandgetconfig_la_LIBADD = \ + ../../../snapper/libsnapper.la \ + ../../utils/libutils.la \ + ../../../dbus/libdbus.la \ + ConfigData/libcommandgetconfigconfigdata.la diff --git a/client/Command/GetConfig/Options.cc b/client/Command/GetConfig/Options.cc new file mode 100644 index 00000000..006d5612 --- /dev/null +++ b/client/Command/GetConfig/Options.cc @@ -0,0 +1,103 @@ +/* + * Copyright (c) [2019] 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 "client/Command/GetConfig/Options.h" +#include "client/utils/text.h" + +using namespace std; + +namespace snapper +{ + namespace cli + { + + namespace + { + + const option OPTIONS[] = { + { "columns", required_argument, 0, 0}, + { 0, 0, 0, 0 } + }; + + } + + + const string Command::GetConfig::Options::Columns::KEY = "key"; + const string Command::GetConfig::Options::Columns::VALUE = "value"; + + + const vector Command::GetConfig::Options::ALL_COLUMNS = { + Columns::KEY, + Columns::VALUE + }; + + + string Command::GetConfig::Options::help_text() + { + return _(" Options for 'get-config' command:\n" + "\t--columns \t\tColumns to show separated by comma.\n" + "\t\t\t\t\tPossible columns: key, value.\n" + "\t\t\t\t\tColumns are not selected when JSON format is used.\n"); + } + + + Command::GetConfig::Options::Options(GetOpts& parser) : + cli::Options(parser), _columns_option(ALL_COLUMNS) + { + parse_options(); + + _columns_option.set_raw_columns(columns_raw()); + } + + + void Command::GetConfig::Options::parse_options() + { + _options = _parser.parse("get-config", OPTIONS); + } + + + vector Command::GetConfig::Options::columns() const + { + if (has_option("columns")) + return _columns_option.selected_columns(); + + return { Columns::KEY, Columns::VALUE }; + } + + + vector Command::GetConfig::Options::errors() const + { + vector detected_errors; + + if (!_columns_option.wrong_columns().empty()) + detected_errors.push_back(_columns_option.error()); + + return detected_errors; + } + + + string Command::GetConfig::Options::columns_raw() const + { + return has_option("columns") ? get_option("columns")->second : ""; + } + + } +} diff --git a/client/Command/GetConfig/Options.h b/client/Command/GetConfig/Options.h new file mode 100644 index 00000000..30bb7517 --- /dev/null +++ b/client/Command/GetConfig/Options.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) [2019] 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. + */ + +#ifndef SNAPPER_CLI_COMMAND_GET_CONFIG_OPTIONS_H +#define SNAPPER_CLI_COMMAND_GET_CONFIG_OPTIONS_H + +#include "client/Options.h" +#include "client/Command/GetConfig.h" +#include "client/Command/ColumnsOption.h" + +namespace snapper +{ + namespace cli + { + + class Command::GetConfig::Options : public cli::Options + { + + public: + + struct Columns + { + static const std::string KEY; + static const std::string VALUE; + }; + + static const std::vector ALL_COLUMNS; + + static std::string help_text(); + + Options(GetOpts& parser); + + std::vector columns() const; + + virtual std::vector errors() const override; + + private: + + void parse_options(); + + string columns_raw() const; + + Command::ColumnsOption _columns_option; + + }; + + } +} + +#endif