118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
| ///
 | |
| /// \file RPM.cc
 | |
| /// \author K. Isom <kyle@imap.cc>
 | |
| /// \date 2024-02-03
 | |
| /// \brief Functionality for interacting with RPM packages on Gitea.
 | |
| ///
 | |
| /// Copyright 2024 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 "RPM.h"
 | |
| #include "Utility.h"
 | |
| 
 | |
| 
 | |
| namespace {
 | |
| std::string
 | |
| rpmEndpoint(std::string endpoint)
 | |
| {
 | |
| 	if (!endpoint.empty() && endpoint.at(0) != '/') {
 | |
| 		endpoint.insert(endpoint.cbegin(), '/');
 | |
| 	}
 | |
| 
 | |
| 	auto urlBase = "https://" + scsl::SimpleConfig::GetGlobal("server");
 | |
| 	return urlBase + "/api/packages/" +
 | |
| 	       scsl::SimpleConfig::GetGlobal("owner", defaultOwner) +
 | |
| 	       "/rpm/" + 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
 | |
| putRPM(std::vector<std::string> args)
 | |
| {
 | |
| 	auto ok = true;
 | |
| 	for (auto &arg: args) {
 | |
| 		auto url    = rpmEndpoint("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
 | |
| deleteRPM(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      = rpmEndpoint(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
 | |
| rpmPackager(std::vector<std::string> args)
 | |
| {
 | |
| 	scsl::Commander commander;
 | |
| 	commander.Register(scsl::Subcommand("del", 1, deleteRPM));
 | |
| 	commander.Register(scsl::Subcommand("put", 1, putRPM));
 | |
| 
 | |
| 	auto command = args[0];
 | |
| 	args.erase(args.cbegin());
 | |
| 	return commander.Run(command, args) == scsl::Subcommand::Status::OK;
 | |
| }
 |