restructured the build module
This commit is contained in:
@@ -1,234 +0,0 @@
|
||||
/**
|
||||
* @file buildFuncs.cpp
|
||||
* @brief Implementation of the build module support functions
|
||||
*
|
||||
* Implements functions for the build module that create DPM packages
|
||||
* according to the specification.
|
||||
*
|
||||
* @copyright Copyright (c) 2025 SILO GROUP LLC
|
||||
* @author Chris Punches <chris.punches@silogroup.org>
|
||||
*
|
||||
* Part of the Dark Horse Linux Package Manager (DPM)
|
||||
*/
|
||||
|
||||
#include "buildFuncs.hpp"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
/**
|
||||
* @brief Handler for the help command
|
||||
*/
|
||||
int cmd_help(int argc, char** argv) {
|
||||
dpm_log(LOG_INFO, "DPM Build Module - Creates DPM packages according to specification");
|
||||
dpm_log(LOG_INFO, "Available commands:");
|
||||
dpm_log(LOG_INFO, " create - Create a new DPM package");
|
||||
dpm_log(LOG_INFO, " help - Display this help message");
|
||||
dpm_log(LOG_INFO, "");
|
||||
dpm_log(LOG_INFO, "Usage: dpm build create [options]");
|
||||
dpm_log(LOG_INFO, "Options:");
|
||||
dpm_log(LOG_INFO, " -o, --output-dir DIR Directory to save the built package (default: current directory)");
|
||||
dpm_log(LOG_INFO, " -c, --contents DIR Directory with package contents (required)");
|
||||
dpm_log(LOG_INFO, " -m, --metadata DIR Directory with package metadata (required)");
|
||||
dpm_log(LOG_INFO, " -H, --hooks DIR Directory with package hooks (optional)");
|
||||
dpm_log(LOG_INFO, " -n, --name NAME Package name (required if not in metadata)");
|
||||
dpm_log(LOG_INFO, " -s, --sign KEY Path to GPG key for signing the package (optional)");
|
||||
dpm_log(LOG_INFO, " -f, --force Force package creation even if warnings occur");
|
||||
dpm_log(LOG_INFO, " -v, --verbose Enable verbose output");
|
||||
dpm_log(LOG_INFO, " -h, --help Display this help message");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handler for unknown commands
|
||||
*/
|
||||
int cmd_unknown(const char* command, int argc, char** argv) {
|
||||
std::string msg = "Unknown command: ";
|
||||
msg += (command ? command : "");
|
||||
dpm_log(LOG_WARN, msg.c_str());
|
||||
dpm_log(LOG_WARN, "Run 'dpm build help' for a list of available commands");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parses a command string to Command enum
|
||||
*/
|
||||
Command parse_command(const char* cmd_str) {
|
||||
if (cmd_str == nullptr || strlen(cmd_str) == 0) {
|
||||
return CMD_HELP;
|
||||
}
|
||||
|
||||
if (strcmp(cmd_str, "help") == 0) {
|
||||
return CMD_HELP;
|
||||
}
|
||||
else if (strcmp(cmd_str, "create") == 0) {
|
||||
return CMD_CREATE;
|
||||
}
|
||||
|
||||
return CMD_UNKNOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handler for the create command
|
||||
*/
|
||||
int cmd_create(int argc, char** argv) {
|
||||
BuildOptions options;
|
||||
|
||||
// Parse command-line options
|
||||
int parse_result = parse_create_options(argc, argv, options);
|
||||
if (parse_result != 0) {
|
||||
return parse_result;
|
||||
}
|
||||
|
||||
// Validate options
|
||||
int validate_result = validate_build_options(options);
|
||||
if (validate_result != 0) {
|
||||
return validate_result;
|
||||
}
|
||||
|
||||
// Log the operation
|
||||
if (options.verbose) {
|
||||
dpm_log(LOG_INFO, "Creating DPM package with the following options:");
|
||||
dpm_log(LOG_INFO, (" Output directory: " + options.output_dir).c_str());
|
||||
dpm_log(LOG_INFO, (" Contents directory: " + options.contents_dir).c_str());
|
||||
dpm_log(LOG_INFO, (" Metadata directory: " + options.metadata_dir).c_str());
|
||||
|
||||
if (!options.hooks_dir.empty()) {
|
||||
dpm_log(LOG_INFO, (" Hooks directory: " + options.hooks_dir).c_str());
|
||||
}
|
||||
|
||||
if (!options.package_name.empty()) {
|
||||
dpm_log(LOG_INFO, (" Package name: " + options.package_name).c_str());
|
||||
}
|
||||
|
||||
if (!options.signature_key.empty()) {
|
||||
dpm_log(LOG_INFO, (" Signature key: " + options.signature_key).c_str());
|
||||
}
|
||||
|
||||
if (options.force) {
|
||||
dpm_log(LOG_INFO, " Force: Yes");
|
||||
}
|
||||
}
|
||||
|
||||
// For now, just log that we would create the package
|
||||
dpm_log(LOG_INFO, "Package creation functionality not yet implemented");
|
||||
dpm_log(LOG_INFO, "Would create package using the provided options");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parses command-line arguments for the create command
|
||||
*/
|
||||
int parse_create_options(int argc, char** argv, BuildOptions& options) {
|
||||
static struct option long_options[] = {
|
||||
{"output-dir", required_argument, 0, 'o'},
|
||||
{"contents", required_argument, 0, 'c'},
|
||||
{"metadata", required_argument, 0, 'm'},
|
||||
{"hooks", required_argument, 0, 'H'},
|
||||
{"name", required_argument, 0, 'n'},
|
||||
{"sign", required_argument, 0, 's'},
|
||||
{"force", no_argument, 0, 'f'},
|
||||
{"verbose", no_argument, 0, 'v'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
// Skip program name and module name
|
||||
int adjusted_argc = argc;
|
||||
char** adjusted_argv = argv;
|
||||
|
||||
// Reset getopt
|
||||
optind = 0;
|
||||
opterr = 1;
|
||||
|
||||
int opt;
|
||||
int option_index = 0;
|
||||
|
||||
while ((opt = getopt_long(adjusted_argc, adjusted_argv, "o:c:m:H:n:s:fvh", long_options, &option_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'o':
|
||||
options.output_dir = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
options.contents_dir = optarg;
|
||||
break;
|
||||
case 'm':
|
||||
options.metadata_dir = optarg;
|
||||
break;
|
||||
case 'H':
|
||||
options.hooks_dir = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
options.package_name = optarg;
|
||||
break;
|
||||
case 's':
|
||||
options.signature_key = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
options.force = true;
|
||||
break;
|
||||
case 'v':
|
||||
options.verbose = true;
|
||||
break;
|
||||
case 'h':
|
||||
cmd_help(0, nullptr);
|
||||
return 1;
|
||||
case '?':
|
||||
// Error message is printed by getopt
|
||||
dpm_log(LOG_ERROR, "Run 'dpm build create --help' for usage information");
|
||||
return 1;
|
||||
default:
|
||||
dpm_log(LOG_ERROR, "Unknown option");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validates the build options
|
||||
*/
|
||||
int validate_build_options(const BuildOptions& options) {
|
||||
// Check if contents directory is provided and exists
|
||||
if (options.contents_dir.empty()) {
|
||||
dpm_log(LOG_ERROR, "Contents directory is required (--contents)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(options.contents_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Contents directory does not exist: " + options.contents_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if metadata directory is provided and exists
|
||||
if (options.metadata_dir.empty()) {
|
||||
dpm_log(LOG_ERROR, "Metadata directory is required (--metadata)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(options.metadata_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Metadata directory does not exist: " + options.metadata_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if hooks directory exists if provided
|
||||
if (!options.hooks_dir.empty() && !std::filesystem::exists(options.hooks_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Hooks directory does not exist: " + options.hooks_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if output directory exists
|
||||
if (!std::filesystem::exists(options.output_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Output directory does not exist: " + options.output_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if signature key exists if provided
|
||||
if (!options.signature_key.empty() && !std::filesystem::exists(options.signature_key)) {
|
||||
dpm_log(LOG_ERROR, ("Signature key file does not exist: " + options.signature_key).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
193
modules/build/src/cli_parsers.cpp
Normal file
193
modules/build/src/cli_parsers.cpp
Normal file
@@ -0,0 +1,193 @@
|
||||
#include "cli_parsers.hpp"
|
||||
|
||||
int parse_create_options(int argc, char** argv, BuildOptions& options) {
|
||||
// For debugging
|
||||
dpm_log(LOG_DEBUG, "Parsing command-line arguments");
|
||||
for (int i = 0; i < argc; i++) {
|
||||
std::string arg_msg = "Arg " + std::to_string(i) + ": " + (argv[i] ? argv[i] : "(null)");
|
||||
dpm_log(LOG_DEBUG, arg_msg.c_str());
|
||||
}
|
||||
|
||||
// First process any arguments in --option=value format
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (!argv[i] || strlen(argv[i]) == 0) continue;
|
||||
|
||||
std::string arg(argv[i]);
|
||||
size_t equals_pos = arg.find('=');
|
||||
|
||||
if (equals_pos != std::string::npos && arg.length() > 3 && arg[0] == '-' && arg[1] == '-') {
|
||||
// Extract option and value
|
||||
std::string option = arg.substr(0, equals_pos);
|
||||
std::string value = arg.substr(equals_pos + 1);
|
||||
|
||||
if (option == "--output-dir") {
|
||||
options.output_dir = value;
|
||||
} else if (option == "--contents") {
|
||||
options.contents_dir = value;
|
||||
} else if (option == "--metadata") {
|
||||
options.metadata_dir = value;
|
||||
} else if (option == "--hooks") {
|
||||
options.hooks_dir = value;
|
||||
} else if (option == "--name") {
|
||||
options.package_name = value;
|
||||
} else if (option == "--sign") {
|
||||
options.signature_key = value;
|
||||
} else if (option == "--force") {
|
||||
options.force = true;
|
||||
} else if (option == "--verbose") {
|
||||
options.verbose = true;
|
||||
} else if (option == "--help") {
|
||||
options.show_help = true;
|
||||
}
|
||||
|
||||
// Convert this argument to a dummy to prevent getopt from processing it
|
||||
argv[i] = strdup("--dummy");
|
||||
}
|
||||
}
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"output-dir", required_argument, 0, 'o'},
|
||||
{"contents", required_argument, 0, 'c'},
|
||||
{"metadata", required_argument, 0, 'm'},
|
||||
{"hooks", required_argument, 0, 'H'},
|
||||
{"name", required_argument, 0, 'n'},
|
||||
{"sign", required_argument, 0, 's'},
|
||||
{"force", no_argument, 0, 'f'},
|
||||
{"verbose", no_argument, 0, 'v'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"dummy", no_argument, 0, 0}, // Add dummy option to prevent getopt errors
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
// Reset getopt
|
||||
optind = 0;
|
||||
opterr = 0; // Suppress getopt error messages
|
||||
|
||||
int opt;
|
||||
int option_index = 0;
|
||||
|
||||
while ((opt = getopt_long(argc, argv, "o:c:m:H:n:s:fvh", long_options, &option_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'o':
|
||||
options.output_dir = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
options.contents_dir = optarg;
|
||||
break;
|
||||
case 'm':
|
||||
options.metadata_dir = optarg;
|
||||
break;
|
||||
case 'H':
|
||||
options.hooks_dir = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
options.package_name = optarg;
|
||||
break;
|
||||
case 's':
|
||||
options.signature_key = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
options.force = true;
|
||||
break;
|
||||
case 'v':
|
||||
options.verbose = true;
|
||||
break;
|
||||
case 'h':
|
||||
options.show_help = true;
|
||||
break;
|
||||
case '?':
|
||||
// Ignore errors as we handle equals-format options separately
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Expand paths after all arguments have been processed
|
||||
if (!options.output_dir.empty()) {
|
||||
options.output_dir = expand_path(options.output_dir);
|
||||
}
|
||||
|
||||
if (!options.contents_dir.empty()) {
|
||||
options.contents_dir = expand_path(options.contents_dir);
|
||||
}
|
||||
|
||||
if (!options.metadata_dir.empty()) {
|
||||
options.metadata_dir = expand_path(options.metadata_dir);
|
||||
}
|
||||
|
||||
if (!options.hooks_dir.empty()) {
|
||||
options.hooks_dir = expand_path(options.hooks_dir);
|
||||
}
|
||||
|
||||
if (!options.signature_key.empty()) {
|
||||
options.signature_key = expand_path(options.signature_key);
|
||||
}
|
||||
|
||||
// Log the parsed options for debugging
|
||||
dpm_log(LOG_DEBUG, ("Parsed options: contents_dir=" + options.contents_dir).c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Command parse_command(const char* cmd_str) {
|
||||
if (cmd_str == nullptr || strlen(cmd_str) == 0) {
|
||||
return CMD_HELP;
|
||||
}
|
||||
|
||||
if (strcmp(cmd_str, "help") == 0) {
|
||||
return CMD_HELP;
|
||||
}
|
||||
else if (strcmp(cmd_str, "create") == 0) {
|
||||
return CMD_CREATE;
|
||||
}
|
||||
|
||||
return CMD_UNKNOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validates the build options
|
||||
*/
|
||||
int validate_build_options(const BuildOptions& options) {
|
||||
// Check if contents directory is provided and exists
|
||||
if (options.contents_dir.empty()) {
|
||||
dpm_log(LOG_ERROR, "Contents directory is required (--contents)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(options.contents_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Contents directory does not exist: " + options.contents_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if metadata directory is provided and exists
|
||||
if (options.metadata_dir.empty()) {
|
||||
dpm_log(LOG_ERROR, "Metadata directory is required (--metadata)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(options.metadata_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Metadata directory does not exist: " + options.metadata_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if hooks directory exists if provided
|
||||
if (!options.hooks_dir.empty() && !std::filesystem::exists(options.hooks_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Hooks directory does not exist: " + options.hooks_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if output directory exists
|
||||
if (!std::filesystem::exists(options.output_dir)) {
|
||||
dpm_log(LOG_ERROR, ("Output directory does not exist: " + options.output_dir).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if signature key exists if provided
|
||||
if (!options.signature_key.empty() && !std::filesystem::exists(options.signature_key)) {
|
||||
dpm_log(LOG_ERROR, ("Signature key file does not exist: " + options.signature_key).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
90
modules/build/src/commands.cpp
Normal file
90
modules/build/src/commands.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "commands.hpp"
|
||||
|
||||
int cmd_create(int argc, char** argv) {
|
||||
// create a container for commandline options
|
||||
BuildOptions options;
|
||||
|
||||
// Parse command-line options
|
||||
int parse_result = parse_create_options(argc, argv, options);
|
||||
if (parse_result != 0) {
|
||||
return parse_result;
|
||||
}
|
||||
|
||||
// If help was requested, show it and return
|
||||
if (options.show_help) {
|
||||
return cmd_help(argc, argv);
|
||||
}
|
||||
|
||||
// Validate options
|
||||
int validate_result = validate_build_options(options);
|
||||
if (validate_result != 0) {
|
||||
return validate_result;
|
||||
}
|
||||
|
||||
// Log the operation
|
||||
if (options.verbose) {
|
||||
dpm_log(LOG_INFO, "Creating DPM package with the following options:");
|
||||
dpm_log(LOG_INFO, (" Output directory: " + options.output_dir).c_str());
|
||||
dpm_log(LOG_INFO, (" Contents directory: " + options.contents_dir).c_str());
|
||||
dpm_log(LOG_INFO, (" Metadata directory: " + options.metadata_dir).c_str());
|
||||
|
||||
if (!options.hooks_dir.empty()) {
|
||||
dpm_log(LOG_INFO, (" Hooks directory: " + options.hooks_dir).c_str());
|
||||
}
|
||||
|
||||
if (!options.package_name.empty()) {
|
||||
dpm_log(LOG_INFO, (" Package name: " + options.package_name).c_str());
|
||||
}
|
||||
|
||||
if (!options.signature_key.empty()) {
|
||||
dpm_log(LOG_INFO, (" Signature key: " + options.signature_key).c_str());
|
||||
}
|
||||
|
||||
if (options.force) {
|
||||
dpm_log(LOG_INFO, " Force: Yes");
|
||||
}
|
||||
}
|
||||
|
||||
// For now, just log that we would create the package
|
||||
dpm_log(LOG_INFO, "Package creation functionality not yet implemented");
|
||||
dpm_log(LOG_INFO, "Would create package using the provided options");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handler for the help command
|
||||
*/
|
||||
int cmd_help(int argc, char** argv) {
|
||||
dpm_log(LOG_INFO, "DPM Build Module - Creates DPM packages according to specification");
|
||||
dpm_log(LOG_INFO, "Available commands:");
|
||||
dpm_log(LOG_INFO, " create - Create a new DPM package");
|
||||
dpm_log(LOG_INFO, " help - Display this help message");
|
||||
dpm_log(LOG_INFO, "");
|
||||
dpm_log(LOG_INFO, "Usage: dpm build create [options]");
|
||||
dpm_log(LOG_INFO, "Options:");
|
||||
dpm_log(LOG_INFO, " -o, --output-dir DIR Directory to save the built package (default: current directory)");
|
||||
dpm_log(LOG_INFO, " -c, --contents DIR Directory with package contents (required)");
|
||||
dpm_log(LOG_INFO, " -m, --metadata DIR Directory with package metadata (required)");
|
||||
dpm_log(LOG_INFO, " -H, --hooks DIR Directory with package hooks (optional)");
|
||||
dpm_log(LOG_INFO, " -n, --name NAME Package name (required if not in metadata)");
|
||||
dpm_log(LOG_INFO, " -s, --sign KEY Path to GPG key for signing the package (optional)");
|
||||
dpm_log(LOG_INFO, " -f, --force Force package creation even if warnings occur");
|
||||
dpm_log(LOG_INFO, " -v, --verbose Enable verbose output");
|
||||
dpm_log(LOG_INFO, " -h, --help Display this help message");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handler for unknown commands
|
||||
*/
|
||||
int cmd_unknown(const char* command, int argc, char** argv) {
|
||||
std::string msg = "Unknown command: ";
|
||||
msg += (command ? command : "");
|
||||
dpm_log(LOG_WARN, msg.c_str());
|
||||
dpm_log(LOG_WARN, "Run 'dpm build help' for a list of available commands");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
32
modules/build/src/helpers.cpp
Normal file
32
modules/build/src/helpers.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @file buildFuncs.cpp
|
||||
* @brief Implementation of the build module support functions
|
||||
*
|
||||
* Implements functions for the build module that create DPM packages
|
||||
* according to the specification.
|
||||
*
|
||||
* @copyright Copyright (c) 2025 SILO GROUP LLC
|
||||
* @author Chris Punches <chris.punches@silogroup.org>
|
||||
*
|
||||
* Part of the Dark Horse Linux Package Manager (DPM)
|
||||
*/
|
||||
|
||||
#include "helpers.hpp"
|
||||
|
||||
/**
|
||||
* @brief Expands environment variables and tildes in a path
|
||||
*/
|
||||
std::string expand_path(const std::string& path) {
|
||||
wordexp_t exp_result;
|
||||
std::string expanded_path = path;
|
||||
|
||||
// Use wordexp to expand the path
|
||||
if (wordexp(path.c_str(), &exp_result, 0) == 0) {
|
||||
if (exp_result.we_wordc > 0) {
|
||||
expanded_path = exp_result.we_wordv[0];
|
||||
}
|
||||
wordfree(&exp_result);
|
||||
}
|
||||
|
||||
return expanded_path;
|
||||
}
|
||||
Reference in New Issue
Block a user