#include "command.h" #include #include "config.h" namespace katja { void list::execute() const { for (const auto& package : katja::read_package_database()) { std::cout << package.second.name() << " " << package.second.version() << " (" << package.second.tag() << ")" << std::endl; } } void help::execute() const { std::cout << "Usage:\n" "\tkatja {list|update|help} [OPTIONS]\n\n"; } update::update() : git_binary(boost::process::search_path("git")) { } void update::execute() const { std::filesystem::path workdir{ WORKDIR }; std::filesystem::path repository = workdir / "sbo/repository"; std::filesystem::file_status repository_status = std::filesystem::status(repository); if (std::filesystem::exists(repository_status) && !std::filesystem::is_directory(repository_status)) { throw std::runtime_error("The working directory path \"" + repository.string() + "\" exists, but it isn't a directory."); } else if (!std::filesystem::exists(repository_status)) { git("clone", std::filesystem::path(), "git://git.slackbuilds.org/slackbuilds.git", repository.native()); } git("remote", repository.native(), "update", "--prune"); git("reset", repository.native(), "--hard", "origin/master"); } command_exception::command_exception(const command_exception_t exception_type, std::vector failed_arguments) noexcept : m_exception_type(exception_type), m_failed_arguments(failed_arguments) { } const char *command_exception::what() const noexcept { switch (m_exception_type) { case command_exception_t::no_command: return "No command specified."; case command_exception_t::too_many_arguments: return "Too many arguments given."; case command_exception_t::unknown_command: return "Unknown command."; } assert(false); } const std::vector& command_exception::failed_arguments() const noexcept { return m_failed_arguments; } std::unique_ptr parse_command_line(int argc, char **argv) { if (argc > 2) { std::vector failed_arguments; for (std::size_t i = 2; i < argc; ++i) { failed_arguments.push_back(argv[i]); } throw command_exception(command_exception_t::unknown_command); } else if (argc < 2) { throw command_exception(command_exception_t::no_command); } if (strcmp(argv[1], "list") == 0) { return std::make_unique(); } else if (strcmp(argv[1], "update") == 0) { return std::make_unique(); } else if (strcmp(argv[1], "help") == 0) { return std::make_unique(); } throw command_exception(command_exception_t::unknown_command, { argv[1] }); } }