x
static cag_option options[] = {
{.identifier = 'r',
.access_letters = "r",
.access_name = "repository",
.description = "Specify the repository name to operate on.",
.value_name = "NAME"},
{.identifier = 'f',
.access_letters = "f",
.access_name = "filter",
.description = "Specify a filter to remove only relevant items.",
.value_name = "TAG"},
{.identifier = 'h',
.access_letters = "h",
.access_name = "help",
.description = "Shows the help for the remove command.",
.value_name = NULL},
};
static void print_help()
{
printf("Usage: ziit remove [OPTION...] <interval>\n");
printf("Removes all entries within the interval.\n\n");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
}
int run_remove(int argc, char *argv[], cfg_t *cfg)
{
ziit_connection con;
ZiitMessage rm_message, *response;
ZiitRmRequest rm_request;
cag_option_context ctx;
const char *repository, *interval;
int option_index, option_count;
ziit_message__init(&rm_message);
ziit_rm_request__init(&rm_request);
cag_option_prepare(&ctx, options, CAG_ARRAY_SIZE(options), argc, argv);
repository = "default";
while (cag_option_fetch(&ctx)) {
char identifier = cag_option_get(&ctx);
switch (identifier) {
case 'r':
repository = cag_option_get_value(&ctx);
break;
case 'f':
rm_request.filter = (char *)cag_option_get_value(&ctx);
break;
default:
fprintf(stderr, "Unknown option supplied to the command. Aborting.\n");
/* FALLTHRU */
case 'h':
print_help();
return EXIT_FAILURE;
}
}
option_index = cag_option_get_index(&ctx) + 1;
option_count = argc - option_index;
if (option_count < 1) {
fprintf(stderr, "Insufficient arguments supplied.\n");
print_help();
return EXIT_FAILURE;
}
interval = argv[option_index];
if (ziit_parse_interval(interval, &rm_request.start, &rm_request.offset) <
0) {
fprintf(stderr, "Could not parse supplied interval.\n");
print_help();
return EXIT_FAILURE;
}
if (ziit_connection_open(&con, cfg, repository) < 0) {
fprintf(stderr, "Could not open connection.\n");
goto err_open;
}
rm_message.content_case = ZIIT_MESSAGE__CONTENT_RM_REQUEST;
rm_message.rm_request = &rm_request;
if (ziit_connection_send(&con, &rm_message) < 0) {
fprintf(stderr, "Could not send add message to server.\n");
goto err_send;
}
response = ziit_connection_receive(&con);
if (response == NULL) {
fprintf(stderr, "Could not receive response from server.\n");
goto err_response;
}
if (response->content_case != ZIIT_MESSAGE__CONTENT_RM_RESPONSE) {
fprintf(stderr, "Unexpected answer from server.\n");
goto err_res_type;
}
if (response->rm_response->code != 0) {
fprintf(stderr, "%s", response->rm_response->message);
goto err_result;
} else {
char from_buffer[255], to_buffer[255];
time_t t;
struct tm *tf;
t = (time_t)rm_request.start;
tf = localtime(&t);
strftime(from_buffer, sizeof(from_buffer), "%Y-%m-%dT%H:%M:%S %Z", tf);
t = (time_t)(rm_request.start + rm_request.offset);
tf = localtime(&t);
strftime(to_buffer, sizeof(to_buffer), "%Y-%m-%dT%H:%M:%S %Z", tf);
printf("Deleting between %s and %s.\n", from_buffer, to_buffer);
printf("%s\n\n", response->rm_response->message);
printf("Affected items: %i\n", response->rm_response->affected);
}
ziit_message__free_unpacked(response, NULL);
ziit_connection_close(&con);
return EXIT_SUCCESS;
err_result:
err_res_type:
ziit_message__free_unpacked(response, NULL);
err_response:
err_send:
ziit_connection_close(&con);
err_open:
return -1;
}