style: improved output formating of IssueStatus list
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dominik Meyer 2024-08-09 22:58:57 +02:00
parent 9b640ac751
commit 2990294fd2
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
2 changed files with 77 additions and 12 deletions

View File

@ -22,32 +22,77 @@
#include "Redmine/API.hpp" #include "Redmine/API.hpp"
#include "Redmine/User.hpp" #include "Redmine/User.hpp"
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "tableprinter/tableprinter.hpp"
#define LOGURU_WITH_STREAMS 1 #define LOGURU_WITH_STREAMS 1
#include <loguru.hpp> #include <loguru.hpp>
Command::IssueStatus::IssueStatus(RedmineCLI::Redmine *r) 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"); list_ = issuestatus->add_subcommand("list", "Get the list of possible issue stati");
issueStatus_->callback([&](){get_();}); 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<Redmine::IssueStatus> &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<Redmine::IssueStatus> &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::getList_() const
void Command::IssueStatus::get_() const
{ {
Redmine::API client{redmine_->getUrl(), redmine_->getToken()}; Redmine::API client{redmine_->getUrl(), redmine_->getToken()};
std::vector<Redmine::IssueStatus> list = client.getIssueStatusList(); std::vector<Redmine::IssueStatus> 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);
} }
} }

View File

@ -15,6 +15,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "Redmine/IssueStatus.hpp"
#include <CLI/App.hpp> #include <CLI/App.hpp>
#include <Redmine/API.hpp> #include <Redmine/API.hpp>
#include <atomic> #include <atomic>
@ -53,14 +54,33 @@ namespace Command
/// output is just text /// output is just text
TEXT, TEXT,
/// output is yaml /// output is yaml
YAML JSON
}; };
std::map<std::string, outputType> outputMap{{"text", TEXT}, {"yaml", YAML}}; std::map<std::string, outputType> 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 /// 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<Redmine::IssueStatus> &list) const;
/**
* @brief print the list of issue stati as json
*
* @param list
*/
void printListJson_(const std::vector<Redmine::IssueStatus> &list) const;
public: public:
/** /**