--- /dev/null
+/*
+ * 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 <iostream>
+
+#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<string> 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;
+ }
+
+ }
+}
--- /dev/null
+/*
+ * 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 <memory>
+
+#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<std::string> errors() const override;
+
+ virtual void run() override;
+
+ virtual ~GetConfig();
+
+ private:
+
+ std::unique_ptr<Options> _options;
+ };
+
+ }
+}
+
+#endif
--- /dev/null
+/*
+ * 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<string, string> 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();
+ }
+
+ }
+}
--- /dev/null
+/*
+ * 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 <string>
+#include <map>
+
+#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<std::string, std::string> values() const;
+
+ private:
+
+ const GetConfig& _command;
+ };
+
+ }
+}
+
+#endif
--- /dev/null
+/*
+ * 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 <vector>
+
+#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<string> columns;
+
+ vector<vector<string>> rows;
+
+ for (const string& column : _command.options().columns())
+ columns.emplace_back(column);
+
+ for (const map<string, string>::value_type& value : values())
+ {
+ vector<string> 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();
+ }
+
+ }
+}
--- /dev/null
+/*
+ * 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 <string>
+
+#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
--- /dev/null
+/*
+ * 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<string, string>::value_type& value : values())
+ data.emplace_back(value.first, value.second);
+
+ JsonFormatter formatter(data);
+
+ return formatter.output();
+ }
+
+ }
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+#
+# 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
--- /dev/null
+/*
+ * 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<pair<string, TableAlign>> columns;
+
+ vector<vector<string>> rows;
+
+ for (const string& column : _command.options().columns())
+ columns.emplace_back(label_for(column), TableAlign::LEFT);
+
+ for (const map<string, string>::value_type& value : values())
+ {
+ vector<string> 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 "";
+ }
+ }
+}
--- /dev/null
+/*
+ * 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 <string>
+
+#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
--- /dev/null
+#
+# 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
--- /dev/null
+/*
+ * 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<string> Command::GetConfig::Options::ALL_COLUMNS = {
+ Columns::KEY,
+ Columns::VALUE
+ };
+
+
+ string Command::GetConfig::Options::help_text()
+ {
+ return _(" Options for 'get-config' command:\n"
+ "\t--columns <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<string> Command::GetConfig::Options::columns() const
+ {
+ if (has_option("columns"))
+ return _columns_option.selected_columns();
+
+ return { Columns::KEY, Columns::VALUE };
+ }
+
+
+ vector<string> Command::GetConfig::Options::errors() const
+ {
+ vector<string> 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 : "";
+ }
+
+ }
+}
--- /dev/null
+/*
+ * 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<std::string> ALL_COLUMNS;
+
+ static std::string help_text();
+
+ Options(GetOpts& parser);
+
+ std::vector<std::string> columns() const;
+
+ virtual std::vector<std::string> errors() const override;
+
+ private:
+
+ void parse_options();
+
+ string columns_raw() const;
+
+ Command::ColumnsOption _columns_option;
+
+ };
+
+ }
+}
+
+#endif