Compare commits

...

2 Commits

Author SHA1 Message Date
e0450de6d9
feat: raw file upload
Some checks failed
continuous-integration/drone/push Build is failing
2024-10-09 15:55:25 +02:00
f36567c546
feat: make upload method public 2024-10-09 15:46:51 +02:00
7 changed files with 144 additions and 11 deletions

View File

@ -137,6 +137,8 @@ add_executable(redmine-cli
src/Redmine-CLI/Command/IssueStatus.cpp src/Redmine-CLI/Command/IssueStatus.cpp
src/Redmine-CLI/Command/Issue.hpp src/Redmine-CLI/Command/Issue.hpp
src/Redmine-CLI/Command/Issue.cpp src/Redmine-CLI/Command/Issue.cpp
src/Redmine-CLI/Command/Upload.hpp
src/Redmine-CLI/Command/Upload.cpp
) )
target_link_libraries(redmine-cli redmine-api-cpp-static loguru CLI11 tableprinter::tableprinter) target_link_libraries(redmine-cli redmine-api-cpp-static loguru CLI11 tableprinter::tableprinter)
target_include_directories(redmine-cli target_include_directories(redmine-cli

View File

@ -60,15 +60,7 @@ namespace Redmine
void post(const std::string &path, const nlohmann::json &data) const; void post(const std::string &path, const nlohmann::json &data) const;
void processGenericErrors_(httplib::Result &res) const; void processGenericErrors_(httplib::Result &res) const;
/**
* @brief upload a file to the redmine server
*
* @param file - the file to upload
* @param filename - the filename used inside redmine
* @return std::string - the redmine internal token (reference) for the uploaded file
*/
std::string upload_(const std::filesystem::path &file, const std::string &filename) const;
public: public:
/** /**
@ -140,6 +132,15 @@ namespace Redmine
* @return std::vector<Redmine::Issue> * @return std::vector<Redmine::Issue>
*/ */
std::vector<Redmine::Issue> getIssues(const Redmine::Filter& filter = {}) const; std::vector<Redmine::Issue> getIssues(const Redmine::Filter& filter = {}) const;
/**
* @brief upload a file to the redmine server
*
* @param file - the file to upload
* @param filename - the filename used inside redmine
* @return std::string - the redmine internal token (reference) for the uploaded file
*/
std::string upload(const std::filesystem::path &file, const std::string &filename) const;
/*@}*/ /*@}*/

View File

@ -0,0 +1,45 @@
/*
* 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/Upload.hpp>
#include <Redmine-CLI/Redmine.hpp>
#include "Redmine/API.hpp"
#include <string>
#include <tableprinter/tableprinter.hpp>
Command::Upload::Upload(RedmineCLI::Redmine *r)
:
redmine_(r),
output_(TEXT)
{
upload_ = redmine_->getCLI().add_subcommand("upload", "raw file upload");
upload_->add_option("-f,--file", filePath_, "the file to upload")->required();
upload_->add_option("-n,--name", fileName_, "the filename within redmine")->required();
upload_->callback([&](){uploadFile_();});
}
void Command::Upload::uploadFile_() const
{
Redmine::API client{redmine_->getUrl(), redmine_->getToken()};
std::string token = client.upload(filePath_, fileName_);
std::cout << token << std::endl;
}

View File

@ -0,0 +1,80 @@
#pragma once
/*
* 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 <CLI/App.hpp>
#include <Redmine/API.hpp>
#include <cstdint>
#include <string>
#include <vector>
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
#include <CLI/CLI.hpp>
// forward declaration
namespace RedmineCLI
{
class Redmine;
};
namespace Command
{
/**
* @brief class for processing the CLI11 parsed command lines
*
*/
class Upload
{
private:
/// the main redmine cli application context
RedmineCLI::Redmine *redmine_;
/// enum for identifying supported output types
enum outputType : std::uint16_t
{
/// output is just text
TEXT,
/// output is json
JSON
};
/// requested output type
outputType output_;
std::map<std::string, outputType> outputMap{{"text", TEXT}, {"json", JSON}};
/// the file path for uploading files
std::string filePath_;
/// the filename for uploading files
std::string fileName_;
CLI::App* upload_;
void uploadFile_() const;
public:
/**
* @brief Construct a new CLIProcessor object
*
*/
explicit Upload(RedmineCLI::Redmine *r);
}; // class Upload
}; // namespace Command

View File

@ -41,4 +41,5 @@ version_(nullptr)
project_ = std::make_unique<Command::Project>(this); project_ = std::make_unique<Command::Project>(this);
issueStatus_ = std::make_unique<Command::IssueStatus>(this); issueStatus_ = std::make_unique<Command::IssueStatus>(this);
issue_ = std::make_unique<Command::Issue>(this); issue_ = std::make_unique<Command::Issue>(this);
upload_ = std::make_unique<Command::Upload>(this);
} }

View File

@ -19,6 +19,7 @@
#include "Redmine-CLI/Command/IssueStatus.hpp" #include "Redmine-CLI/Command/IssueStatus.hpp"
#include "Redmine-CLI/Command/MyAccount.hpp" #include "Redmine-CLI/Command/MyAccount.hpp"
#include "Redmine-CLI/Command/Project.hpp" #include "Redmine-CLI/Command/Project.hpp"
#include "Redmine-CLI/Command/Upload.hpp"
#include "Redmine-CLI/Command/Version.hpp" #include "Redmine-CLI/Command/Version.hpp"
#include <CLI/App.hpp> #include <CLI/App.hpp>
#include <CLI/Option.hpp> #include <CLI/Option.hpp>
@ -68,6 +69,9 @@ namespace RedmineCLI
/// pointer to the issue subcommand /// pointer to the issue subcommand
std::unique_ptr<Command::Issue> issue_; std::unique_ptr<Command::Issue> issue_;
/// pointer to the upload subcommand
std::unique_ptr<Command::Upload> upload_;
public: public:
/** /**

View File

@ -145,7 +145,7 @@ void Redmine::API::put(const std::string &path, const nlohmann::json &data) con
DLOG_S(INFO) << "post successful"; DLOG_S(INFO) << "post successful";
} }
std::string Redmine::API::upload_(const std::filesystem::path &file, const std::string &filename) const std::string Redmine::API::upload(const std::filesystem::path &file, const std::string &filename) const
{ {
httplib::Client client{redmineApiURL_}; httplib::Client client{redmineApiURL_};
client.set_basic_auth(authToken_, ""); client.set_basic_auth(authToken_, "");
@ -234,7 +234,7 @@ bool Redmine::API::ready() const
void Redmine::API::uploadFileToProject(const std::uint64_t projectId, const std::string &filePath, const std::string &fileName, const std::string &description, const std::uint32_t version) const void Redmine::API::uploadFileToProject(const std::uint64_t projectId, const std::string &filePath, const std::string &fileName, const std::string &description, const std::uint32_t version) const
{ {
std::string token = upload_(filePath, fileName); std::string token = upload(filePath, fileName);
nlohmann::json file; nlohmann::json file;
file["token"] = token; file["token"] = token;