1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5-cli/output/advisoryinfo.hpp"
#include "key_value_table.hpp"
#include "utils/string.hpp"
#include <json.h>
#include <iostream>
namespace libdnf5::cli::output {
class AdvisoryInfo::Impl : public KeyValueTable {
public:
void add_advisory(IAdvisory & advisory);
};
class AdvisoryInfoJSON::Impl {
public:
Impl() { json_advisories = json_object_new_array(); };
~Impl() { json_object_put(json_advisories); };
void add_advisory(IAdvisory & advisory);
void print() {
std::cout << json_object_to_json_string_ext(json_advisories, JSON_C_TO_STRING_PRETTY) << std::endl;
};
json_object * json_advisories;
};
void AdvisoryInfo::Impl::add_advisory(IAdvisory & advisory) {
add_line("Name", advisory.get_name(), "bold");
add_line("Title", advisory.get_title());
add_line("Severity", advisory.get_severity());
add_line("Type", advisory.get_type());
add_line("Status", advisory.get_status());
add_line("Vendor", advisory.get_vendor());
add_line("Issued", libdnf5::utils::string::format_epoch(advisory.get_buildtime()));
add_lines("Description", libdnf5::utils::string::split(advisory.get_description(), "\n"));
add_line("Message", advisory.get_message());
add_line("Rights", advisory.get_rights());
// References
for (auto & reference : advisory.get_references()) {
auto group_reference = add_line("Reference", "");
add_line("Title", reference->get_title(), "bold", group_reference);
add_line("Id", reference->get_id(), nullptr, group_reference);
add_line("Type", reference->get_type_cstring(), nullptr, group_reference);
add_line("Url", reference->get_url(), nullptr, group_reference);
}
// Collections
for (auto & collection : advisory.get_collections()) {
auto group_collection = add_line("Collection", "");
// Modules
auto modules = collection->get_modules();
if (!modules.empty()) {
auto module_iter = modules.begin();
add_line("Modules", (*module_iter)->get_nsvca(), nullptr, group_collection);
module_iter++;
while (module_iter != modules.end()) {
add_line("", (*module_iter)->get_nsvca(), nullptr, group_collection);
module_iter++;
}
}
// Packages
auto packages = collection->get_packages();
if (!packages.empty()) {
auto package_iter = packages.begin();
add_line("Packages", (*package_iter)->get_nevra(), nullptr, group_collection);
package_iter++;
while (package_iter != packages.end()) {
add_line("", (*package_iter)->get_nevra(), nullptr, group_collection);
package_iter++;
}
}
}
}
// [NOTE] When editing JSON output format, do not forget to update the docs at doc/commands/advisory.8.rst
void AdvisoryInfoJSON::Impl::add_advisory(IAdvisory & advisory) {
json_object * json_advisory = json_object_new_object();
json_object_object_add(json_advisory, "Name", json_object_new_string(advisory.get_name().c_str()));
json_object_object_add(json_advisory, "Title", json_object_new_string(advisory.get_title().c_str()));
json_object_object_add(json_advisory, "Severity", json_object_new_string(advisory.get_severity().c_str()));
json_object_object_add(json_advisory, "Type", json_object_new_string(advisory.get_type().c_str()));
json_object_object_add(json_advisory, "Status", json_object_new_string(advisory.get_status().c_str()));
json_object_object_add(json_advisory, "Vendor", json_object_new_string(advisory.get_vendor().c_str()));
json_object_object_add(
json_advisory, "Issued", json_object_new_int64(static_cast<int64_t>(advisory.get_buildtime())));
json_object_object_add(json_advisory, "Description", json_object_new_string(advisory.get_description().c_str()));
json_object_object_add(json_advisory, "Message", json_object_new_string(advisory.get_message().c_str()));
json_object_object_add(json_advisory, "Rights", json_object_new_string(advisory.get_rights().c_str()));
// References
json_object * json_references = json_object_new_array();
for (auto & reference : advisory.get_references()) {
json_object * json_reference = json_object_new_object();
json_object_object_add(json_reference, "Title", json_object_new_string(reference->get_title().c_str()));
json_object_object_add(json_reference, "Id", json_object_new_string(reference->get_id().c_str()));
json_object_object_add(json_reference, "Type", json_object_new_string(reference->get_type_cstring()));
json_object_object_add(json_reference, "Url", json_object_new_string(reference->get_url().c_str()));
json_object_array_add(json_references, json_reference);
}
json_object_object_add(json_advisory, "references", json_references);
// Collections
json_object * json_collections = json_object_new_object();
for (auto & collection : advisory.get_collections()) {
// Modules
auto modules = collection->get_modules();
if (!modules.empty()) {
json_object * json_modules = json_object_new_array();
for (auto & module : modules) {
json_object_array_add(json_modules, json_object_new_string(module->get_nsvca().c_str()));
}
json_object_object_add(json_collections, "modules", json_modules);
}
// Packages
auto packages = collection->get_packages();
if (!packages.empty()) {
json_object * json_pkgs = json_object_new_array();
for (auto & package : packages) {
json_object_array_add(json_pkgs, json_object_new_string(package->get_nevra().c_str()));
}
json_object_object_add(json_collections, "packages", json_pkgs);
}
json_object_object_add(json_advisory, "collections", json_collections);
}
json_object_array_add(json_advisories, json_advisory);
}
AdvisoryInfo::AdvisoryInfo() : p_impl{new AdvisoryInfo::Impl} {}
AdvisoryInfoJSON::AdvisoryInfoJSON() : p_impl{new AdvisoryInfoJSON::Impl} {}
AdvisoryInfo::~AdvisoryInfo() = default;
AdvisoryInfoJSON::~AdvisoryInfoJSON() = default;
void AdvisoryInfo::add_advisory(IAdvisory & advisory) {
p_impl->add_advisory(advisory);
}
void AdvisoryInfoJSON::add_advisory(IAdvisory & advisory) {
p_impl->add_advisory(advisory);
}
void AdvisoryInfo::print() {
p_impl->print();
}
void AdvisoryInfoJSON::print() {
p_impl->print();
}
} // namespace libdnf5::cli::output
|