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 = 'h',
.access_letters = "h",
.access_name = "help",
.description = "Shows the help for the add command.",
.value_name = NULL},
};
static void print_help()
{
printf("Usage: ziit list [-r <repository>] <interval>\n");
printf("Lists entries in the specified interval.\n\n");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
}
static int pad_right(char *buffer, int buffer_len, int padding)
{
size_t content_len;
if (buffer_len < padding + 1) {
return -1;
}
buffer[padding] = '\0';
content_len = strlen(buffer);
for (int i = (int)content_len; i < padding; ++i) {
buffer[i] = ' ';
}
if (content_len > padding - 2) {
for (int i = padding - 4; i < padding - 1 && i > 3; ++i) {
buffer[i] = '.';
}
buffer[padding - 1] = ' ';
}
return 0;
}
int run_list(int argc, char *argv[], cfg_t *cfg)
{
ziit_connection con;
ZiitMessage list_message, *response;
ZiitListRequest list_request;
ZiitEntry *entry;
cag_option_context ctx;
const char *repository, *interval;
int option_index, option_count;
ziit_message__init(&list_message);
ziit_list_request__init(&list_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;
default:
fprintf(stderr, "Unknown option supplied to the command. Aborting.\n");
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, &list_request.start, &list_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;
}
list_message.content_case = ZIIT_MESSAGE__CONTENT_LIST_REQUEST;
list_message.list_request = &list_request;
if (ziit_connection_send(&con, &list_message) < 0) {
fprintf(stderr, "Could not send list message to server.\n");
goto err_send;
}
response = ziit_connection_receive(&con);
if (response == NULL) {
fprintf(stderr, "Could not receive resposne from server.\n");
goto err_response;
}
if (response->content_case != ZIIT_MESSAGE__CONTENT_LIST_RESPONSE) {
fprintf(stderr, "Unexpected answer from server.\n");
goto err_response;
}
if (response->list_response->code != 0) {
fprintf(stderr, "Listing entries FAILED:\n");
fprintf(stderr, "%s\n", response->add_response->message);
goto err_result;
} else {
char buffer[255];
time_t t;
struct tm *tf;
int padding_id, padding_at, padding_tags, bs, padding_duration;
padding_id = 13;
padding_at = 27;
padding_duration = 14;
padding_tags = 17;
bs = 255;
strcpy(buffer, "id");
pad_right(buffer, bs, padding_id);
printf("%s", buffer);
strcpy(buffer, "at");
pad_right(buffer, bs, padding_at);
printf("%s", buffer);
strcpy(buffer, "duration");
pad_right(buffer, bs, padding_duration);
printf("%s", buffer);
strcpy(buffer, "tags");
pad_right(buffer, bs, padding_tags);
printf("%s", buffer);
printf("message\n");
for (int i = 0; i < response->list_response->n_entries; ++i) {
entry = response->list_response->entries[i];
t = (time_t)entry->at_time;
tf = localtime(&t);
snprintf(buffer, bs, "%li ", t);
pad_right(buffer, bs, padding_id);
printf("%s", buffer);
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S %Z", tf);
pad_right(buffer, bs, padding_at);
printf("%s", buffer);
snprintf(buffer, bs, "%lih%02lim%02lis", entry->duration / 60 / 60,
(entry->duration / 60) % 60, entry->duration % 60);
pad_right(buffer, bs, padding_duration);
printf("%s", buffer);
snprintf(buffer, bs, "%s", entry->tags);
pad_right(buffer, bs, padding_tags);
printf("%s", buffer);
printf("%s\n", entry->message);
}
}
ziit_message__free_unpacked(response, NULL);
ziit_connection_close(&con);
return EXIT_SUCCESS;
err_result:
ziit_message__free_unpacked(response, NULL);
err_response:
err_send:
ziit_connection_close(&con);
err_open:
return -1;
}