diff --git a/src/Redmine-CLI/Command/IssueStatus.cpp b/src/Redmine-CLI/Command/IssueStatus.cpp index c0e9da8..5f090dc 100644 --- a/src/Redmine-CLI/Command/IssueStatus.cpp +++ b/src/Redmine-CLI/Command/IssueStatus.cpp @@ -22,32 +22,77 @@ #include "Redmine/API.hpp" #include "Redmine/User.hpp" #include "nlohmann/json_fwd.hpp" +#include "tableprinter/tableprinter.hpp" #define LOGURU_WITH_STREAMS 1 #include Command::IssueStatus::IssueStatus(RedmineCLI::Redmine *r) : -redmine_(r) +redmine_(r), +output_(TEXT) { + CLI::App* issuestatus = redmine_->getCLI().add_subcommand("issuestatus", + "Commands for working with Issue Stati") + ->require_subcommand(); - issueStatus_ = redmine_->getCLI().add_subcommand("issuestatus", - "Request the available issue status list from the server"); - issueStatus_->callback([&](){get_();}); + + list_ = issuestatus->add_subcommand("list", "Get the list of possible issue stati"); + list_->callback([&](){getList_();}); + list_->needs(redmine_->getTokenOption())->needs(redmine_->getUrlOption()); + list_->add_option("-o,--output", output_, "The Output Format to use. Supported: TEXT, JSON")->transform(CLI::CheckedTransformer(outputMap, CLI::ignore_case)); + +} +void Command::IssueStatus::printListJson_(const std::vector &list) const +{ + nlohmann::json data; + for (const auto& status : list) + { + nlohmann::json stat; + stat["id"] = status.getId(); + stat["name"] = status.getName(); + stat["closesIssue"] = status.isClosed(); + data.push_back(stat); + } + std::cout << data.dump(2) << std::endl; +} + +void Command::IssueStatus::printListText_(const std::vector &list) const +{ + tableprinter::printer p + { + { + { tableprinter::name { "ID" } , tableprinter::width { 8 } } , + { tableprinter::name { "Name" } , tableprinter::width { 20 } } , + { tableprinter::name { "closesIssue" } , tableprinter::width { 14 } } + } , + { std::cout } + }; + + p.sanity_check(); + p.print_headers(); + + for (auto status : list) + { + p.print(status.getId(), status.getName(), status.isClosed() ? "true" : "false"); + } } - -void Command::IssueStatus::get_() const +void Command::IssueStatus::getList_() const { Redmine::API client{redmine_->getUrl(), redmine_->getToken()}; std::vector list = client.getIssueStatusList(); - for (auto it = list.begin(); it!= list.end(); ++it) + if (output_ == JSON) { - std::cout << it->to_string() << std::endl; + printListJson_(list); + } + else if (output_ == TEXT) + { + printListText_(list); } } diff --git a/src/Redmine-CLI/Command/IssueStatus.hpp b/src/Redmine-CLI/Command/IssueStatus.hpp index 22b10ca..5b1621c 100644 --- a/src/Redmine-CLI/Command/IssueStatus.hpp +++ b/src/Redmine-CLI/Command/IssueStatus.hpp @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include "Redmine/IssueStatus.hpp" #include #include #include @@ -53,14 +54,33 @@ namespace Command /// output is just text TEXT, /// output is yaml - YAML + JSON }; - std::map outputMap{{"text", TEXT}, {"yaml", YAML}}; + std::map outputMap{{"text", TEXT}, {"json", JSON}}; - CLI::App* issueStatus_; + /// requested output type + outputType output_; + + /// pointer to the list subcommand + CLI::App* list_; /// the actual implementation of the information get - void get_() const; + void getList_() const; + + /** + * @brief print the list of issue stati as text + * + * @param list + */ + void printListText_(const std::vector &list) const; + + /** + * @brief print the list of issue stati as json + * + * @param list + */ + void printListJson_(const std::vector &list) const; + public: /**