178 lines
4.9 KiB
C++
178 lines
4.9 KiB
C++
/*
|
|
* Copyright (C) 2024 Dominik Meyer
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <Redmine-CLI/Command/MyAccount.hpp>
|
|
#include <Redmine-CLI/Redmine.hpp>
|
|
#include "Redmine/API.hpp"
|
|
#include "Redmine/User.hpp"
|
|
#include "nlohmann/json_fwd.hpp"
|
|
|
|
|
|
Command::MyAccount::MyAccount(RedmineCLI::Redmine *r)
|
|
:
|
|
redmine_(r),
|
|
output_(TEXT)
|
|
{
|
|
|
|
CLI::App* myAccount = redmine_->getCLI().add_subcommand("myaccount",
|
|
"Api Calls on the account of the token owner");
|
|
|
|
|
|
|
|
myAccountGet = myAccount->add_subcommand("get", "Get the current information of the token owner");
|
|
myAccountGet->needs(redmine_->getTokenOption())->needs(redmine_->getUrlOption());
|
|
myAccountGet->callback([&](){get_();});
|
|
myAccountGet->add_option("-f,--fields", fields_, "The fields of the user object to print. Supported: firstname, lastname, login, id, mail");
|
|
myAccountGet->add_option("-o,--output", output_, "The Output Format to use. Supported: TEXT, YAML")->transform(CLI::CheckedTransformer(outputMap, CLI::ignore_case));
|
|
|
|
myAccountSet = myAccount->add_subcommand("set", "Set the current information of the token owner");
|
|
myAccountSet->needs(redmine_->getTokenOption())->needs(redmine_->getUrlOption());
|
|
myAccountSet->add_option("-f,--firstname", firstname_, "the new firstname");
|
|
myAccountSet->add_option("-l,--lastname", lastname_, "the new lastname");
|
|
myAccountSet->add_option("-a,--login", login_, "the new login");
|
|
myAccountSet->add_option("-m,--mail", mail_, "the new email address");
|
|
myAccountSet->callback([&](){set_();});
|
|
}
|
|
|
|
void Command::MyAccount::set_() const
|
|
{
|
|
Redmine::API client{redmine_->getUrl(), redmine_->getToken()};
|
|
|
|
Redmine::User user = client.getMyAccount();
|
|
|
|
if (! firstname_.empty())
|
|
{
|
|
user.setFirstName(firstname_);
|
|
}
|
|
|
|
if (! lastname_.empty())
|
|
{
|
|
user.setLastName(lastname_);
|
|
}
|
|
|
|
if (! login_.empty())
|
|
{
|
|
user.setLogin(login_);
|
|
}
|
|
|
|
if (! id_.empty())
|
|
{
|
|
user.setId(id_);
|
|
}
|
|
|
|
if (! mail_.empty())
|
|
{
|
|
user.setMail(mail_);
|
|
}
|
|
|
|
client.setMyAccount(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::MyAccount::get_() const
|
|
{
|
|
|
|
Redmine::API client{redmine_->getUrl(), redmine_->getToken()};
|
|
|
|
Redmine::User user = client.getMyAccount();
|
|
|
|
bool hasFirstname = std::find(fields_.begin(), fields_.end(),"firstname")
|
|
!= fields_.end() || fields_.empty();
|
|
|
|
bool hasLastname = std::find(fields_.begin(), fields_.end(),"lastname")
|
|
!= fields_.end() || fields_.empty();
|
|
|
|
bool hasLogin = std::find(fields_.begin(), fields_.end(),"login")
|
|
!= fields_.end() || fields_.empty();
|
|
|
|
bool hasId = std::find(fields_.begin(), fields_.end(),"id")
|
|
!= fields_.end() || fields_.empty();
|
|
|
|
bool hasMail = std::find(fields_.begin(), fields_.end(),"mail")
|
|
!= fields_.end() || fields_.empty();
|
|
|
|
|
|
nlohmann::json data;
|
|
|
|
if (hasFirstname)
|
|
{
|
|
if (output_ == TEXT)
|
|
{
|
|
std::cout << "Firstname : " << user.getFirstName() << std::endl;
|
|
}
|
|
else if (output_ == YAML)
|
|
{
|
|
data["firstname"]=user.getFirstName();
|
|
}
|
|
}
|
|
|
|
if (hasLastname)
|
|
{
|
|
if (output_ == TEXT)
|
|
{
|
|
std::cout << "Lastname : " << user.getLastName() << std::endl;
|
|
}
|
|
else if (output_ == YAML)
|
|
{
|
|
data["lastname"]=user.getLastName();
|
|
}
|
|
}
|
|
|
|
if (hasLogin)
|
|
{
|
|
if (output_ == TEXT)
|
|
{
|
|
std::cout << "Login : " << user.getLogin() << std::endl;
|
|
}
|
|
else if (output_ == YAML)
|
|
{
|
|
data["login"]=user.getLogin();
|
|
}
|
|
}
|
|
|
|
if (hasId)
|
|
{
|
|
if (output_ == TEXT)
|
|
{
|
|
std::cout << "Id : " << user.getId() << std::endl;
|
|
}
|
|
else if (output_ == YAML)
|
|
{
|
|
data["id"]=user.getId();
|
|
}
|
|
}
|
|
|
|
if (hasMail)
|
|
{
|
|
if (output_ == TEXT)
|
|
{
|
|
std::cout << "Email : " << user.getEmail() << std::endl;
|
|
}
|
|
else if (output_ == YAML)
|
|
{
|
|
data["mail"]=user.getEmail();
|
|
}
|
|
}
|
|
|
|
if (output_ == YAML)
|
|
{
|
|
std::cout << data.dump(1);
|
|
}
|
|
|
|
} |