| 1 | + | #include "list.h" |
| 2 | + | #include "constants.h" |
| 3 | + | #include "interval.h" |
| 4 | + | #include "message.pb-c.h" |
| 5 | + | #include "net.h" |
| 6 | + | #include <cargs.h> |
| 7 | + | #include <confuse.h> |
| 8 | + | #include <stdio.h> |
| 9 | + | #include <stdlib.h> |
| 10 | + | #include <string.h> |
| 11 | + | #include <time.h> |
| 12 | + | |
| 13 | + | static cag_option options[] = { |
| 14 | + | {.identifier = 'r', |
| 15 | + | .access_letters = "r", |
| 16 | + | .access_name = "repository", |
| 17 | + | .description = "Specify the repository name to operate on.", |
| 18 | + | .value_name = "NAME"}, |
| 19 | + | {.identifier = 'h', |
| 20 | + | .access_letters = "h", |
| 21 | + | .access_name = "help", |
| 22 | + | .description = "Shows the help for the add command.", |
| 23 | + | .value_name = NULL}, |
| 24 | + | }; |
| 25 | + | |
| 26 | + | static void print_help() |
| 27 | + | { |
| 28 | + | printf("Usage: ziit add [OPTION...] <interval> <message> [<tags>]\n"); |
| 29 | + | printf("Adds a new entry to the time log.\n\n"); |
| 30 | + | cag_option_print(options, CAG_ARRAY_SIZE(options), stdout); |
| 31 | + | } |
| 32 | + | |
| 33 | + | static int pad_right(char *buffer, int buffer_len, int padding) |
| 34 | + | { |
| 35 | + | size_t content_len; |
| 36 | + | |
| 37 | + | if (buffer_len < padding + 1) { |
| 38 | + | return -1; |
| 39 | + | } |
| 40 | + | |
| 41 | + | buffer[padding] = '\0'; |
| 42 | + | content_len = strlen(buffer); |
| 43 | + | |
| 44 | + | for (int i = (int)content_len; i < padding; ++i) { |
| 45 | + | buffer[i] = ' '; |
| 46 | + | } |
| 47 | + | |
| 48 | + | if (content_len > padding - 2) { |
| 49 | + | for (int i = padding - 4; i < padding - 1 && i > 3; ++i) { |
| 50 | + | buffer[i] = '.'; |
| 51 | + | } |
| 52 | + | buffer[padding - 1] = ' '; |
| 53 | + | } |
| 54 | + | |
| 55 | + | return 0; |
| 56 | + | } |
| 57 | + | |
| 58 | + | int run_list(int argc, char *argv[], cfg_t *cfg) |
| 59 | + | { |
| 60 | + | ziit_connection con; |
| 61 | + | ZiitMessage list_message, *response; |
| 62 | + | ZiitListRequest list_request; |
| 63 | + | ZiitEntry *entry; |
| 64 | + | |
| 65 | + | cag_option_context ctx; |
| 66 | + | const char *repository, *interval; |
| 67 | + | int option_index, option_count; |
| 68 | + | |
| 69 | + | ziit_message__init(&list_message); |
| 70 | + | ziit_list_request__init(&list_request); |
| 71 | + | |
| 72 | + | cag_option_prepare(&ctx, options, CAG_ARRAY_SIZE(options), argc, argv); |
| 73 | + | |
| 74 | + | repository = "default"; |
| 75 | + | |
| 76 | + | while (cag_option_fetch(&ctx)) { |
| 77 | + | char identifier = cag_option_get(&ctx); |
| 78 | + | switch (identifier) { |
| 79 | + | case 'r': |
| 80 | + | repository = cag_option_get_value(&ctx); |
| 81 | + | break; |
| 82 | + | default: |
| 83 | + | fprintf(stderr, "Unknown option supplied to the command. Aborting.\n"); |
| 84 | + | case 'h': |
| 85 | + | print_help(); |
| 86 | + | return EXIT_FAILURE; |
| 87 | + | } |
| 88 | + | } |
| 89 | + | |
| 90 | + | option_index = cag_option_get_index(&ctx) + 1; |
| 91 | + | option_count = argc - option_index; |
| 92 | + | if (option_count < 1) { |
| 93 | + | fprintf(stderr, "Insufficient arguments supplied.\n"); |
| 94 | + | print_help(); |
| 95 | + | return EXIT_FAILURE; |
| 96 | + | } |
| 97 | + | |
| 98 | + | interval = argv[option_index]; |
| 99 | + | |
| 100 | + | if (ziit_parse_interval(interval, &list_request.start, &list_request.offset) < |
| 101 | + | 0) { |
| 102 | + | fprintf(stderr, "Could not parse supplied interval.\n"); |
| 103 | + | print_help(); |
| 104 | + | return EXIT_FAILURE; |
| 105 | + | } |
| 106 | + | |
| 107 | + | if (ziit_connection_open(&con, cfg, repository) < 0) { |
| 108 | + | fprintf(stderr, "Could not open connection.\n"); |
| 109 | + | goto err_open; |
| 110 | + | } |
| 111 | + | |
| 112 | + | list_message.content_case = ZIIT_MESSAGE__CONTENT_LIST_REQUEST; |
| 113 | + | list_message.list_request = &list_request; |
| 114 | + | |
| 115 | + | if (ziit_connection_send(&con, &list_message) < 0) { |
| 116 | + | fprintf(stderr, "Could not send list message to server.\n"); |
| 117 | + | goto err_send; |
| 118 | + | } |
| 119 | + | |
| 120 | + | response = ziit_connection_receive(&con); |
| 121 | + | if (response == NULL) { |
| 122 | + | fprintf(stderr, "Could not receive resposne from server.\n"); |
| 123 | + | goto err_response; |
| 124 | + | } |
| 125 | + | |
| 126 | + | if (response->content_case != ZIIT_MESSAGE__CONTENT_LIST_RESPONSE) { |
| 127 | + | fprintf(stderr, "Unexpected answer from server.\n"); |
| 128 | + | goto err_response; |
| 129 | + | } |
| 130 | + | |
| 131 | + | if (response->list_response->code != 0) { |
| 132 | + | fprintf(stderr, "Listing entries FAILED:\n"); |
| 133 | + | fprintf(stderr, "%s\n", response->add_response->message); |
| 134 | + | goto err_result; |
| 135 | + | } else { |
| 136 | + | char buffer[255]; |
| 137 | + | time_t t; |
| 138 | + | struct tm *tf; |
| 139 | + | int padding_id, padding_at, padding_tags, bs, padding_duration; |
| 140 | + | |
| 141 | + | padding_id = 13; |
| 142 | + | padding_at = 27; |
| 143 | + | padding_duration = 14; |
| 144 | + | padding_tags = 17; |
| 145 | + | bs = 255; |
| 146 | + | |
| 147 | + | strcpy(buffer, "id"); |
| 148 | + | pad_right(buffer, bs, padding_id); |
| 149 | + | printf("%s", buffer); |
| 150 | + | |
| 151 | + | strcpy(buffer, "at"); |
| 152 | + | pad_right(buffer, bs, padding_at); |
| 153 | + | printf("%s", buffer); |
| 154 | + | |
| 155 | + | strcpy(buffer, "duration"); |
| 156 | + | pad_right(buffer, bs, padding_duration); |
| 157 | + | printf("%s", buffer); |
| 158 | + | |
| 159 | + | strcpy(buffer, "tags"); |
| 160 | + | pad_right(buffer, bs, padding_tags); |
| 161 | + | printf("%s", buffer); |
| 162 | + | |
| 163 | + | printf("message\n"); |
| 164 | + | |
| 165 | + | for (int i = 0; i < response->list_response->n_entries; ++i) { |
| 166 | + | |
| 167 | + | entry = response->list_response->entries[i]; |
| 168 | + | t = (time_t)entry->at_time; |
| 169 | + | tf = localtime(&t); |
| 170 | + | |
| 171 | + | snprintf(buffer, bs, "%li ", t); |
| 172 | + | pad_right(buffer, bs, padding_id); |
| 173 | + | printf("%s", buffer); |
| 174 | + | |
| 175 | + | strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S %Z", tf); |
| 176 | + | pad_right(buffer, bs, padding_at); |
| 177 | + | printf("%s", buffer); |
| 178 | + | |
| 179 | + | snprintf(buffer, bs, "%lih %02lim %02lis", entry->duration / 60 / 60, |
| 180 | + | (entry->duration / 60) % 60, entry->duration % 60); |
| 181 | + | pad_right(buffer, bs, padding_duration); |
| 182 | + | printf("%s", buffer); |
| 183 | + | |
| 184 | + | snprintf(buffer, bs, "%s", entry->tags); |
| 185 | + | pad_right(buffer, bs, padding_tags); |
| 186 | + | printf("%s", buffer); |
| 187 | + | |
| 188 | + | printf("%s\n", entry->message); |
| 189 | + | } |
| 190 | + | } |
| 191 | + | |
| 192 | + | ziit_message__free_unpacked(response, NULL); |
| 193 | + | ziit_connection_close(&con); |
| 194 | + | |
| 195 | + | return EXIT_SUCCESS; |
| 196 | + | |
| 197 | + | err_result: |
| 198 | + | ziit_message__free_unpacked(response, NULL); |
| 199 | + | err_response: |
| 200 | + | err_send: |
| 201 | + | ziit_connection_close(&con); |
| 202 | + | err_open: |
| 203 | + | return -1; |
| 204 | + | } |
| 205 | + | |