Basic version working.

This commit is contained in:
2023-10-24 18:49:20 -07:00
parent fd93a937a4
commit d9d0929bb7
12 changed files with 733 additions and 170 deletions

122
src/Debian.cc Normal file
View File

@@ -0,0 +1,122 @@
///
/// \file Debian.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-24
/// \brief Functionality for interacting with Debian packages on Gitea.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <scsl/Commander.h>
#include <scsl/SimpleConfig.h>
#include <scsl/StringUtil.h>
#include "Debian.h"
#include "Utility.h"
namespace {
std::string
debianEndpoint(std::string endpoint)
{
if (!endpoint.empty() && endpoint.at(0) != '/') {
endpoint.insert(endpoint.cbegin(), '/');
}
std::cerr << "owner: " << scsl::SimpleConfig::GetGlobal("owner", defaultOwner) << "\n";
auto urlBase = "https://" + scsl::SimpleConfig::GetGlobal("server");
return urlBase + "/api/packages/" +
scsl::SimpleConfig::GetGlobal("owner", defaultOwner) +
"/debian/pool/" +
scsl::SimpleConfig::GetGlobal("distribution", defaultDistribution) +
"/" +
scsl::SimpleConfig::GetGlobal("component", defaultComponent) +
endpoint;
}
std::string
endpointForPackage(std::string package)
{
const std::filesystem::path pkgAsPath(package);
auto stem = pkgAsPath.stem();
auto parts = scsl::scstring::SplitN(stem, "_", 3);
if (parts.size() != 3) {
return "";
}
return parts[0] + "/" + parts[1] + "/" + parts[2];
}
bool
putDebian(std::vector<std::string> args)
{
auto ok = true;
for (auto &arg: args) {
auto url = debianEndpoint("upload");
auto result = PutFile(url, arg);
std::cout << "\t[*] PUT " << std::filesystem::path(arg).filename() << ": ";
std::cout << ResultToString(result) << "\n";
ok &= ResultSuccess(result);
}
return ok;
}
bool
deleteDebian(std::vector<std::string> args)
{
auto ok = true;
for (auto &arg: args) {
auto endpoint = endpointForPackage(arg);
if (endpoint.empty()) {
std::cerr << arg << ": invalid filename\n";
ok &= false;
continue;
}
auto url = debianEndpoint(endpoint);
std::cerr << "URL: " << url << "\n";
auto result = DeleteFile(url);
std::cout << "\t[*] DELETE " << std::filesystem::path(arg).filename() << ": ";
std::cout << ResultToString(result) << "\n";
ok &= ResultSuccess(result);
}
return ok;
}
}// anonymous namespace
bool
debianPackager(std::vector<std::string> args)
{
scsl::Commander commander;
commander.Register(scsl::Subcommand("del", 1, deleteDebian));
commander.Register(scsl::Subcommand("put", 1, putDebian));
auto command = args[0];
args.erase(args.cbegin());
return commander.Run(command, args) == scsl::Subcommand::Status::OK;
}

117
src/Generic.cc Normal file
View File

@@ -0,0 +1,117 @@
///
/// \file Generic.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-24
/// \brief Functionality for interacting with generic packages on
/// Gitea.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
#include <scsl/Commander.h>
#include <scsl/SimpleConfig.h>
#include <scsl/StringUtil.h>
#include "Utility.h"
namespace {
std::string
genericEndpoint(std::string endpoint)
{
if (!endpoint.empty() && endpoint.at(0) != '/') {
endpoint.insert(endpoint.cbegin(), '/');
}
std::cerr << "owner: " << scsl::SimpleConfig::GetGlobal("owner", defaultOwner) << "\n";
auto urlBase = "https://" + scsl::SimpleConfig::GetGlobal("server");
return urlBase + "/api/packages/" +
scsl::SimpleConfig::GetGlobal("owner", defaultOwner) +
"/generic" + endpoint;
}
std::string
endpointForPackage(std::string package)
{
const std::filesystem::path pkgAsPath(package);
auto stem = pkgAsPath.stem();
auto parts = scsl::scstring::SplitN(stem, "-", 3);
if (parts.size() < 2) {
return "";
}
return parts[0] + "/" + parts[1] + "/" + pkgAsPath.filename().string();
}
bool
putGeneric(std::vector<std::string> args)
{
auto ok = true;
for (auto &arg: args) {
auto url = genericEndpoint(endpointForPackage(arg));
auto result = PutFile(url, arg);
std::cout << "\t[*] PUT " << std::filesystem::path(arg).filename() << ": ";
std::cout << ResultToString(result) << "\n";
ok &= ResultSuccess(result);
}
return ok;
}
bool
deleteGeneric(std::vector<std::string> args)
{
auto ok = true;
for (auto &arg: args) {
auto endpoint = endpointForPackage(arg);
if (endpoint.empty()) {
std::cerr << arg << ": invalid filename\n";
ok &= false;
continue;
}
auto url = genericEndpoint(endpoint);
std::cerr << "URL: " << url << "\n";
auto result = DeleteFile(url);
std::cout << "\t[*] DELETE " << std::filesystem::path(arg).filename() << ": ";
std::cout << ResultToString(result) << "\n";
ok &= ResultSuccess(result);
}
return ok;
}
}// anonymous namespace
bool
genericPackager(std::vector<std::string> args)
{
scsl::Commander commander;
commander.Register(scsl::Subcommand("del", 1, deleteGeneric));
commander.Register(scsl::Subcommand("put", 1, putGeneric));
auto command = args[0];
args.erase(args.cbegin());
return commander.Run(command, args) == scsl::Subcommand::Status::OK;
}

180
src/Utility.cc Normal file
View File

@@ -0,0 +1,180 @@
///
/// \file Utility.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-24
/// \brief Common utility functions for packaging.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <curlpp/Easy.hpp>
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>
#include <filesystem>
#include <fstream>
#include <list>
#include <scsl/SimpleConfig.h>
#include "Utility.h"
namespace {
std::string
getUserPass()
{
return scsl::SimpleConfig::GetGlobal("http_user") + ":" +
scsl::SimpleConfig::GetGlobal("http_password");
}
}
Result
PutFile(std::string url, std::string path)
{
const curlpp::Cleanup clearerr;
static std::string userPass;
auto verbose = false;
curlpp::Easy request;
if (scsl::SimpleConfig::GetGlobal("verbose") == "true") {
verbose = true;
}
if (userPass.empty()) {
userPass = getUserPass();
}
try {
std::list<std::string> header;
header.emplace_back("Content-Type: application/octet-stream");
if (!std::filesystem::exists(path)) {
std::cerr << "[!] " << path << " does not exist!\n";
return Result::CurlFailed;
}
auto fileSize = static_cast<int64_t>(std::filesystem::file_size(path));
std::ifstream uploadFile(path);
if (!uploadFile.good()) {
std::cerr << "[!] " << path << " does not exist!\n";
return Result::CurlFailed;
}
request.setOpt(new curlpp::options::Url(url));
if (verbose) {
request.setOpt(new curlpp::options::Verbose(true));
} request.setOpt(new curlpp::options::UserPwd(userPass));
request.setOpt(new curlpp::Options::ReadStream(&uploadFile));
request.setOpt(new curlpp::Options::InfileSize(fileSize));
request.setOpt(new curlpp::Options::Upload(true));
request.perform();
} catch (curlpp::LogicError &e) {
std::cerr << e.what() << "\n";
return Result::CurlFailed;
} catch (curlpp::RuntimeError &e) {
std::cerr << e.what() << "\n";
return Result::CurlFailed;
}
auto httpStatus = curlpp::infos::ResponseCode::get(request);
switch (httpStatus) {
case 201:
return Result::Created;
case 400:
return Result::BadPackage;
case 409:
return Result::DuplicatePackage;
default:
return Result::CurlFailed;
}
}
Result
DeleteFile(std::string url)
{
const curlpp::Cleanup clearerr;
static std::string userPass;
bool verbose = false;
curlpp::Easy request;
if (scsl::SimpleConfig::GetGlobal("verbose") == "true") {
verbose = true;
}
if (userPass.empty()) {
userPass = getUserPass();
}
try {
request.setOpt(new curlpp::options::Url(url));
if (verbose) {
request.setOpt(new curlpp::options::Verbose(true));
}
request.setOpt(new curlpp::options::UserPwd(userPass));
request.setOpt(new curlpp::Options::CustomRequest ("DELETE"));
request.perform();
} catch (curlpp::LogicError &e) {
std::cerr << e.what() << "\n";
return Result::CurlFailed;
} catch (curlpp::RuntimeError &e) {
std::cerr << e.what() << "\n";
return Result::CurlFailed;
}
auto httpStatus = curlpp::infos::ResponseCode::get(request);
switch (httpStatus) {
case 204:
return Result::Deleted;
case 404:
return Result::NotFound;
default:
return Result::CurlFailed;
}
}
std::string
ResultToString(Result result)
{
switch (result) {
case Result::Created:
return "OK: created";
case Result::BadPackage:
return "FAILED: bad package";
case Result::DuplicatePackage:
return "FAILED: duplicate package?";
case Result::Deleted:
return "OK: deleted";
case Result::NotFound:
return "FAILED: not found";
case Result::CurlFailed:
return "FAILED: curl failed";
default:
return "FAILED: unhandled error";
}
}
bool
ResultSuccess(Result result)
{
return (result == Result::Created || result == Result::Deleted);
}

View File

@@ -4,34 +4,46 @@
/// \date 2023-10-18
/// \brief Packaging tools for shimmering-clarity.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <filesystem>
#include <iostream>
#include <string>
#include <curlpp/Easy.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>
#include <fstream>
#include <scsl/Commander.h>
#include <scsl/Flags.h>
#include <scsl/SimpleConfig.h>
#include "Debian.h"
#include "Generic.h"
#include "Utility.h"
const auto defaultOwner = std::string("sc");
const auto defaultDistribution = std::string("ubuntu");
const auto defaultComponent = std::string("main");
void
usage(std::ostream &outs, int exc)
{
outs << "sc3dev-packager: manage packages on gitea\n";
outs << "usage:\n";
outs << "\tsc3dev-packager [--component component] [--owner owner] \n";
outs << "\t\t[--distribution distribution] debian command packages ...\n";
outs << "\tsc3dev-packager [--owner owner] generic command packages ...\n";
outs << "\tsc3dev-packager [--component component] [--distribution distribution]\n";
outs << "\t\t debian command packages ...\n";
outs << "\tsc3dev-packager generic command packages ...\n";
outs << "\nGlobal flags:\n";
outs << "\t[--owner owner]\n";
outs << "\t[--verbose]\n";
outs << "\nCommands:\n";
outs << "\t- delete: remove a package\n";
outs << "\t- put: upload a package\n";
@@ -43,8 +55,8 @@ usage(std::ostream &outs, int exc)
std::string
getDefaultConfigFile()
{
auto userEnv = getenv("USER");
auto user = std::string(userEnv);
auto *userEnv = getenv("USER");
auto user = std::string(userEnv);
if (user == "root") {
return std::filesystem::path("/etc/sc3dev/packager.conf");
@@ -58,124 +70,29 @@ getDefaultConfigFile()
auto userHome = std::filesystem::path(userEnv);
userHome /= std::filesystem::path(".config");
userHome /= std::filesystem::path("sc3dev-packager.conf");
userHome /= std::filesystem::path("sc3dev");
userHome /= std::filesystem::path("packager.conf");
return userHome;
}
static std::string
debianEndpoint(std::string endpoint)
{
if (!endpoint.empty() && endpoint.at(0) != '/') {
endpoint.insert(endpoint.cbegin(), '/');
}
auto urlBase = scsl::SimpleConfig::GetGlobal("server");
return urlBase + "/api/packages/" +
scsl::SimpleConfig::GetGlobal("owner", defaultOwner) +
"/debian/pool/" +
scsl::SimpleConfig::GetGlobal("distribution", defaultDistribution) +
"/" +
scsl::SimpleConfig::GetGlobal("component", defaultComponent) +
endpoint;
}
static std::string
getUserPass()
{
return scsl::SimpleConfig::GetGlobal("http_user") + ":" +
scsl::SimpleConfig::GetGlobal("http_password");
}
static bool
putDebian(std::vector<std::string> args)
{
auto ok = true;
std::cout << "[+] put Debian package\n";
for (auto &arg: args) {
std::cout << "\t[*] " << arg << "\n";
}
auto url = debianEndpoint("upload");
auto userPass = getUserPass();
for (auto &arg: args) {
try {
std::list<std::string> header;
header.emplace_back("Content-Type: application/octet-stream");
curlpp::Cleanup cleaner;
curlpp::Easy request;
if (!std::filesystem::exists(arg)) {
std::cerr << "[!] " << arg << " does not exist!\n";
ok &= false;
continue;
}
auto fileSize = static_cast<long>(std::filesystem::file_size(arg));
std::ifstream debFile(arg);
if (!debFile.good()) {
std::cerr << "[!] " << arg << " does not exist!\n";
ok &= false;
continue;
}
request.setOpt(new curlpp::options::Url(url));
request.setOpt(new curlpp::options::Verbose(true));
request.setOpt(new curlpp::options::UserPwd(userPass));
request.setOpt(new curlpp::Options::ReadStream(&debFile));
request.setOpt(new curlpp::Options::InfileSize(fileSize));
request.setOpt(new curlpp::Options::Upload(true));
request.perform();
} catch (curlpp::LogicError &e) {
std::cout << e.what() << "\n";
ok &= false;
} catch (curlpp::RuntimeError &e) {
std::cerr << e.what() << "\n";
ok &= false;
}
std::cout << "[+] " << std::filesystem::path(arg).filename() << ": OK\n";
}
return false;
}
static bool
debianPackager(std::vector<std::string> args)
{
scsl::Commander commander;
commander.Register(scsl::Subcommand("put", 1, putDebian));
std::cout << "[+] Debian packager invoked\n";
for (auto &arg: args) {
std::cout << "\t[*] " << arg << "\n";
}
auto command = args[0];
args.erase(args.cbegin());
return commander.Run(command, args) == scsl::Subcommand::Status::OK;
}
int
main(int argc, char *argv[])
{
int retc;
scsl::Flags *flags;
int retc = -1;
scsl::Flags *flags = nullptr;
std::string configFile = getDefaultConfigFile();
std::string owner;
std::string distribution;
std::string component;
std::string version;
bool verbose;
flags = new scsl::Flags("sc3dev-packager", "package tooling for shimmering clarity");
flags->Register("--config", configFile, "path to a configuration file");
flags->Register("--component", "main", "Debian package component");
flags->Register("--distribution", "ubuntu", "Debian package distribution");
flags->Register("--owner", "sc", "package repository owner");
flags->Register("--version", scsl::FlagType::String, "version to operate on (only matters for delete)");
flags->Register("--component", defaultComponent, "Debian package component");
flags->Register("--distribution", defaultDistribution, "Debian package distribution");
flags->Register("--owner", defaultOwner, "package repository owner");
flags->Register("--verbose", false, "use verbose CURL endpoints");
auto status = flags->Parse(argc, argv);
if (status != scsl::Flags::ParseStatus::OK) {
@@ -186,7 +103,8 @@ main(int argc, char *argv[])
}
scsl::Commander commander;
commander.Register(scsl::Subcommand("debian", 1, debianPackager));
commander.Register(scsl::Subcommand("debian", 2, debianPackager));
commander.Register(scsl::Subcommand("generic", 2, genericPackager));
flags->GetString("--config", configFile);
if (std::filesystem::exists(std::filesystem::path(configFile))) {
@@ -208,8 +126,8 @@ main(int argc, char *argv[])
scsl::SimpleConfig::PutGlobal("component", component);
}
if (flags->GetString("--version", version)) {
scsl::SimpleConfig::PutGlobal("version", version);
if (flags->GetBool("--verbose", verbose)) {
scsl::SimpleConfig::PutGlobal("verbose", "true");
}
if (flags->NumArgs() < 1) {