From e464098cfab081f4f23d201d55d26cd5b56a24b8 Mon Sep 17 00:00:00 2001 From: Jan Kolarik Date: Fri, 31 May 2024 12:05:34 +0000 Subject: [PATCH] repolist: Implement json output --- dnf5/commands/repo/repo_list.cpp | 11 ++++++++++- include/libdnf5-cli/output/repolist.hpp | 1 + libdnf5-cli/CMakeLists.txt | 4 ++++ libdnf5-cli/output/repolist.cpp | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/dnf5/commands/repo/repo_list.cpp b/dnf5/commands/repo/repo_list.cpp index 3a7bc9389..f3bfe5175 100644 --- a/dnf5/commands/repo/repo_list.cpp +++ b/dnf5/commands/repo/repo_list.cpp @@ -19,6 +19,8 @@ along with libdnf. If not, see . #include "repo_list.hpp" +#include + #include #include @@ -39,6 +41,8 @@ void RepoListCommand::set_argument_parser() { all->get_arg()->add_conflict_argument(*enabled->get_arg()); all->get_arg()->add_conflict_argument(*disabled->get_arg()); enabled->get_arg()->add_conflict_argument(*disabled->get_arg()); + + create_json_option(*this); } void RepoListCommand::run() { @@ -82,7 +86,12 @@ void RepoListCommand::print(const libdnf5::repo::RepoQuery & query, bool with_st cli_repos.emplace_back(new libdnf5::cli::output::RepoAdapter(repo)); } - libdnf5::cli::output::print_repolist_table(cli_repos, with_status, libdnf5::cli::output::COL_REPO_ID); + auto & ctx = get_context(); + if (ctx.get_json_output_requested()) { + libdnf5::cli::output::print_repolist_json(cli_repos); + } else { + libdnf5::cli::output::print_repolist_table(cli_repos, with_status, libdnf5::cli::output::COL_REPO_ID); + } } } // namespace dnf5 diff --git a/include/libdnf5-cli/output/repolist.hpp b/include/libdnf5-cli/output/repolist.hpp index f0401d1ff..45680b0d2 100644 --- a/include/libdnf5-cli/output/repolist.hpp +++ b/include/libdnf5-cli/output/repolist.hpp @@ -31,6 +31,7 @@ namespace libdnf5::cli::output { enum { COL_REPO_ID, COL_REPO_NAME, COL_REPO_STATUS }; void print_repolist_table(const std::vector> & repos, bool with_status, size_t sort_column); +void print_repolist_json(const std::vector> & repos); } // namespace libdnf5::cli::output diff --git a/libdnf5-cli/CMakeLists.txt b/libdnf5-cli/CMakeLists.txt index 8cfa9d1e0..8025730db 100644 --- a/libdnf5-cli/CMakeLists.txt +++ b/libdnf5-cli/CMakeLists.txt @@ -41,6 +41,10 @@ pkg_check_modules(SMARTCOLS REQUIRED smartcols) list(APPEND LIBDNF5_CLI_PC_REQUIRES_PRIVATE "${SMARTCOLS_MODULE_NAME}") target_link_libraries(libdnf5-cli PRIVATE ${SMARTCOLS_LIBRARIES}) +pkg_check_modules(JSONC REQUIRED json-c) +include_directories(${JSONC_INCLUDE_DIRS}) +target_link_libraries(libdnf5-cli PRIVATE ${JSONC_LIBRARIES}) + # sort the pkg-config requires and concatenate them into a string list(SORT LIBDNF5_CLI_PC_REQUIRES) diff --git a/libdnf5-cli/output/repolist.cpp b/libdnf5-cli/output/repolist.cpp index d03b97803..ff215f8ee 100644 --- a/libdnf5-cli/output/repolist.cpp +++ b/libdnf5-cli/output/repolist.cpp @@ -21,6 +21,7 @@ along with libdnf. If not, see . #include "libdnf5-cli/tty.hpp" +#include #include namespace libdnf5::cli::output { @@ -75,4 +76,18 @@ void print_repolist_table(const std::vector> & repos, boo scols_unref_table(table); } + +void print_repolist_json([[maybe_unused]] const std::vector> & repos) { + json_object * json_repos = json_object_new_array(); + for (const auto & repo : repos) { + json_object * json_repo = json_object_new_object(); + json_object_object_add(json_repo, "id", json_object_new_string(repo->get_id().c_str())); + json_object_object_add(json_repo, "name", json_object_new_string(repo->get_name().c_str())); + json_object_object_add(json_repo, "is_enabled", json_object_new_boolean(repo->is_enabled())); + json_object_array_add(json_repos, json_repo); + } + std::cout << json_object_to_json_string_ext(json_repos, JSON_C_TO_STRING_PRETTY) << std::endl; + json_object_put(json_repos); +} + } // namespace libdnf5::cli::output