diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-11-15 13:50:15 +0100 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-11-15 14:03:11 +0100 |
commit | 992ac5301fc8727d83017b242af2df9895eebfcc (patch) | |
tree | 4fdd21a61b54d368540d6ef65258f97473051381 /src/run_queue.c | |
parent | client: print the server response (diff) | |
download | cimple-992ac5301fc8727d83017b242af2df9895eebfcc.tar.gz cimple-992ac5301fc8727d83017b242af2df9895eebfcc.zip |
implement a command to list runs
Diffstat (limited to 'src/run_queue.c')
-rw-r--r-- | src/run_queue.c | 118 |
1 files changed, 100 insertions, 18 deletions
diff --git a/src/run_queue.c b/src/run_queue.c index 188a651..6414e6e 100644 --- a/src/run_queue.c +++ b/src/run_queue.c @@ -6,20 +6,27 @@ */ #include "run_queue.h" +#include "json.h" #include "log.h" +#include <json-c/json_object.h> + #include <stdlib.h> #include <string.h> #include <sys/queue.h> struct run { int id; - char *url; - char *rev; + char *repo_url; + char *repo_rev; + int status; + int exit_code; + SIMPLEQ_ENTRY(run) entries; }; -int run_create(struct run **_entry, int id, const char *_url, const char *_rev) +int run_new(struct run **_entry, int id, const char *_repo_url, const char *_repo_rev, + enum run_status status, int exit_code) { struct run *entry = malloc(sizeof(struct run)); if (!entry) { @@ -27,27 +34,29 @@ int run_create(struct run **_entry, int id, const char *_url, const char *_rev) goto fail; } - char *url = strdup(_url); - if (!url) { + char *repo_url = strdup(_repo_url); + if (!repo_url) { log_errno("strdup"); goto free_entry; } - char *rev = strdup(_rev); - if (!rev) { + char *repo_rev = strdup(_repo_rev); + if (!repo_rev) { log_errno("strdup"); - goto free_url; + goto free_repo_url; } entry->id = id; - entry->url = url; - entry->rev = rev; + entry->repo_url = repo_url; + entry->repo_rev = repo_rev; + entry->status = status; + entry->exit_code = exit_code; *_entry = entry; return 0; -free_url: - free(url); +free_repo_url: + free(repo_url); free_entry: free(entry); @@ -58,24 +67,64 @@ fail: void run_destroy(struct run *entry) { - free(entry->rev); - free(entry->url); + free(entry->repo_rev); + free(entry->repo_url); free(entry); } +int run_queued(struct run **entry, const char *repo_url, const char *repo_rev) +{ + return run_new(entry, -1, repo_url, repo_rev, RUN_STATUS_CREATED, -1); +} + +int run_created(struct run **entry, int id, const char *repo_url, const char *repo_rev) +{ + return run_new(entry, id, repo_url, repo_rev, RUN_STATUS_CREATED, -1); +} + +int run_to_json(const struct run *entry, struct json_object **_json) +{ + struct json_object *json = NULL; + int ret = 0; + + ret = json_new_object(&json); + if (ret < 0) + return -1; + ret = json_set_int_const_key(json, "id", entry->id); + if (ret < 0) + goto free; + ret = json_set_int_const_key(json, "exit_code", entry->exit_code); + if (ret < 0) + goto free; + ret = json_set_string_const_key(json, "repo_url", entry->repo_url); + if (ret < 0) + goto free; + ret = json_set_string_const_key(json, "repo_rev", entry->repo_rev); + if (ret < 0) + goto free; + + *_json = json; + return ret; + +free: + json_object_put(json); + + return ret; +} + int run_get_id(const struct run *entry) { return entry->id; } -const char *run_get_url(const struct run *entry) +const char *run_get_repo_url(const struct run *entry) { - return entry->url; + return entry->repo_url; } -const char *run_get_rev(const struct run *entry) +const char *run_get_repo_rev(const struct run *entry) { - return entry->rev; + return entry->repo_rev; } void run_set_id(struct run *entry, int id) @@ -99,6 +148,39 @@ void run_queue_destroy(struct run_queue *queue) SIMPLEQ_INIT(queue); } +int run_queue_to_json(const struct run_queue *queue, struct json_object **_json) +{ + struct json_object *json = NULL; + int ret = 0; + + ret = json_new_array(&json); + if (ret < 0) + return ret; + + struct run *entry = NULL; + SIMPLEQ_FOREACH(entry, queue, entries) + { + struct json_object *entry_json = NULL; + ret = run_to_json(entry, &entry_json); + if (ret < 0) + goto free; + + ret = json_append(json, entry_json); + if (ret < 0) { + json_object_put(entry_json); + goto free; + } + } + + *_json = json; + return ret; + +free: + json_object_put(json); + + return ret; +} + int run_queue_is_empty(const struct run_queue *queue) { return SIMPLEQ_EMPTY(queue); |