Files
ropkg/rovem/channel/add-component.c
T
2026-06-22 17:45:54 +01:00

179 lines
3.7 KiB
C

#include "../commands.h"
#include <fx/cmdline/cmd.h>
#include <fx/error.h>
#include <ropkg/repo-mgmt.h>
#include <ropkg/status.h>
#include <stdio.h>
enum {
OPT_CHANNEL,
OPT_CHANNEL_VALUE,
OPT_COMPONENT,
OPT_COMPONENT_VALUE,
OPT_DESCRIPTION,
OPT_DESCRIPTION_VALUE,
OPT_REPO,
OPT_REPO_PATH,
};
static int channel_add_component(
const fx_command *self,
const fx_arglist *opt,
const fx_array *args)
{
const char *root_path = NULL;
const char *channel_name = NULL;
const char *component_name = NULL;
fx_arglist_get_string(opt, OPT_REPO, OPT_REPO_PATH, 0, &root_path);
fx_arglist_get_string(
opt,
OPT_CHANNEL,
OPT_CHANNEL_VALUE,
0,
&channel_name);
fx_arglist_get_string(
opt,
OPT_COMPONENT,
OPT_COMPONENT_VALUE,
0,
&component_name);
if (!root_path) {
fx_arglist_report_missing_args(opt, OPT_REPO, OPT_REPO_PATH, 0);
return -1;
}
if (!channel_name) {
fx_arglist_report_missing_args(
opt,
OPT_CHANNEL,
OPT_CHANNEL_VALUE,
0);
return -1;
}
if (!component_name) {
fx_arglist_report_missing_args(
opt,
OPT_COMPONENT,
OPT_COMPONENT_VALUE,
0);
return -1;
}
struct ropkg_repo_mgmt *repo = NULL;
fx_result result = ropkg_repo_mgmt_open(root_path, &repo);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
}
struct ropkg_repo_mgmt_channel *channel = NULL;
result = ropkg_repo_mgmt_open_channel(repo, channel_name, &channel);
if (fx_result_is_error(result)) {
ropkg_repo_mgmt_close(repo);
fx_throw(result);
return -1;
}
struct ropkg_repo_mgmt_component *component = NULL;
result = ropkg_repo_mgmt_channel_open_component(
channel,
component_name,
&component);
if (fx_result_is(result, ROPKG_ERROR_VENDOR, ROPKG_ERR_NO_COMPONENT)) {
fx_error_discard(result);
} else if (fx_result_is_error(result)) {
ropkg_repo_mgmt_channel_close(channel);
ropkg_repo_mgmt_close(repo);
fx_throw(result);
return -1;
} else {
ropkg_repo_mgmt_component_close(component);
ropkg_repo_mgmt_channel_close(channel);
ropkg_repo_mgmt_close(repo);
fprintf(stderr,
"component %s/%s already exists\n",
channel_name,
component_name);
return -1;
}
result = ropkg_repo_mgmt_channel_create_component(
channel,
component_name,
&component);
if (fx_result_is_error(result)) {
ropkg_repo_mgmt_close(repo);
fx_throw(result);
return -1;
}
ropkg_repo_mgmt_component_close(component);
ropkg_repo_mgmt_channel_close(channel);
ropkg_repo_mgmt_close(repo);
return 0;
}
FX_COMMAND(CMD_CHANNEL_ADD_COMPONENT, CMD_CHANNEL)
{
FX_COMMAND_NAME("add-component");
FX_COMMAND_DESC("add a component to a repository channel.");
FX_COMMAND_FLAGS(FX_COMMAND_SHOW_HELP_BY_DEFAULT);
FX_COMMAND_FUNCTION(channel_add_component);
FX_COMMAND_HELP_OPTION();
FX_COMMAND_OPTION(OPT_REPO)
{
FX_OPTION_LONG_NAME("repo");
FX_OPTION_SHORT_NAME('r');
FX_OPTION_DESC(
"the path to the root of the repository to add "
"the "
"package(s) to.");
FX_OPTION_ARG(OPT_REPO_PATH)
{
FX_ARG_NAME("path");
FX_ARG_DESC("the path to the repository root.");
FX_ARG_NR_VALUES(1);
}
}
FX_COMMAND_OPTION(OPT_CHANNEL)
{
FX_OPTION_LONG_NAME("channel");
FX_OPTION_SHORT_NAME('C');
FX_OPTION_DESC(
"the name of the channel to add the new "
"component to.");
FX_OPTION_ARG(OPT_CHANNEL_VALUE)
{
FX_ARG_NAME("name");
FX_ARG_DESC(
"the name of the channel to add the "
"new "
"component to.");
FX_ARG_NR_VALUES(1);
}
}
FX_COMMAND_OPTION(OPT_COMPONENT)
{
FX_OPTION_LONG_NAME("component");
FX_OPTION_SHORT_NAME('c');
FX_OPTION_DESC("the name of the new component.");
FX_OPTION_ARG(OPT_COMPONENT_VALUE)
{
FX_ARG_NAME("name");
FX_ARG_DESC("the name of the new component.");
FX_ARG_NR_VALUES(1);
}
}
}