Skip to content

Commit

Permalink
repolist: Implement json output
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-kolarik committed May 31, 2024
1 parent 03b0b49 commit edeb153
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion dnf5/commands/repo/repo_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "repo_list.hpp"

#include <dnf5/shared_options.hpp>
#include <libdnf5-cli/output/adapters/repo.hpp>
#include <libdnf5-cli/output/repolist.hpp>

Expand All @@ -39,6 +40,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() {
Expand Down Expand Up @@ -82,7 +85,12 @@ void RepoListCommand::print(const libdnf5::repo::RepoQuery & query, bool with_st
cli_repos.emplace_back(new libdnf5::cli::output::RepoAdapter<libdnf5::repo::RepoWeakPtr>(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
1 change: 1 addition & 0 deletions include/libdnf5-cli/output/repolist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<IRepo>> & repos, bool with_status, size_t sort_column);
void print_repolist_json(const std::vector<std::unique_ptr<IRepo>> & repos);

} // namespace libdnf5::cli::output

Expand Down
4 changes: 4 additions & 0 deletions libdnf5-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions libdnf5-cli/output/repolist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "libdnf5-cli/tty.hpp"

#include <json.h>
#include <libsmartcols/libsmartcols.h>

namespace libdnf5::cli::output {
Expand Down Expand Up @@ -75,4 +76,18 @@ void print_repolist_table(const std::vector<std::unique_ptr<IRepo>> & repos, boo
scols_unref_table(table);
}


void print_repolist_json([[maybe_unused]] const std::vector<std::unique_ptr<IRepo>> & 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

0 comments on commit edeb153

Please sign in to comment.