aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-02-13 00:07:28 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-02-13 00:07:28 -0500
commit350d21afffc4b77f65fea7409c4f00b56d4f97e4 (patch)
treec992c6e2c94a95a95908b41fbf1db78d0404e765
parentdeaff24e42c8f3687ffe6bae549a04bc2f0a35aa (diff)
downloadstasis-350d21afffc4b77f65fea7409c4f00b56d4f97e4.tar.gz
Implement delivery_mission_render_files
-rw-r--r--include/deliverable.h2
-rw-r--r--include/template.h8
-rw-r--r--src/deliverable.c79
-rw-r--r--src/template.c64
4 files changed, 104 insertions, 49 deletions
diff --git a/include/deliverable.h b/include/deliverable.h
index 1c9e95e..76d7eb4 100644
--- a/include/deliverable.h
+++ b/include/deliverable.h
@@ -337,4 +337,6 @@ int delivery_init_tmpdir(struct Delivery *ctx);
int delivery_init_artifactory(struct Delivery *ctx);
int delivery_artifact_upload(struct Delivery *ctx);
+
+int delivery_mission_render_files(struct Delivery *ctx);
#endif //OMC_DELIVERABLE_H
diff --git a/include/template.h b/include/template.h
index 24ca98f..b12208f 100644
--- a/include/template.h
+++ b/include/template.h
@@ -35,4 +35,12 @@ char *tpl_getval(char *key);
*/
char *tpl_render(char *str);
+/**
+ * Write tpl_render() output to a file
+ * @param str the text to render
+ * @param filename the output file name
+ * @return 0 on success, <0 on error
+ */
+int tpl_render_to_file(char *str, const char *filename);
+
#endif //OMC_TEMPLATE_H
diff --git a/src/deliverable.c b/src/deliverable.c
index ce8972e..a91901a 100644
--- a/src/deliverable.c
+++ b/src/deliverable.c
@@ -1467,29 +1467,70 @@ int delivery_mission_render_files(struct Delivery *ctx) {
fprintf(stderr, "Mission directory is not configured. Context not initialized?\n");
return -1;
}
+ struct Data {
+ char *src;
+ char *dest;
+ } data;
+ struct INIFILE *cfg = ctx->rules._handle;
+ union INIVal val;
- ctx->deploy.upload_ctx.workaround_parent_only = true;
- ctx->deploy.upload_ctx.build_name = strdup(ctx->info.release_name);
- ctx->deploy.upload_ctx.build_number = ctx->info.time_now;
-
- char files[PATH_MAX];
- int status = 0;
-
- if (jfrog_cli_rt_ping(&ctx->deploy.auth_ctx)) {
- msg(OMC_MSG_ERROR | OMC_MSG_L2, "Unable to contact artifactory server: %s\n", ctx->deploy.auth_ctx.url);
+ data.src = calloc(PATH_MAX, sizeof(*data.src));
+ if (!data.src) {
+ perror("data.src");
return -1;
}
- jfrog_cli_rt_build_collect_env(&ctx->deploy.auth_ctx, ctx->deploy.upload_ctx.build_name, ctx->deploy.upload_ctx.build_number);
+ for (size_t i = 0; i < cfg->section_count; i++) {
+ char *section_name = cfg->section[i]->key;
+ if (!startswith(section_name, "template:")) {
+ continue;
+ }
+ val.as_char_p = strchr(section_name, ':') + 1;
+ if (val.as_char_p && isempty(val.as_char_p)) {
+ return 1;
+ }
+ sprintf(data.src, "%s/%s/%s", ctx->storage.mission_dir, ctx->meta.mission, val.as_char_p);
+ msg(OMC_MSG_L2, "%s\n", data.src);
- strcpy(files, "omc/sources/**/results.xml");
- status += jfrog_cli_rt_upload(&ctx->deploy.auth_ctx, &ctx->deploy.upload_ctx, files, "omc/delivery/results");
- strcpy(files, "omc/output/delivery/*.yml");
- status += jfrog_cli_rt_upload(&ctx->deploy.auth_ctx, &ctx->deploy.upload_ctx, files, "omc/delivery/");
- strcpy(files, "omc/output/packages/*");
- status += jfrog_cli_rt_upload(&ctx->deploy.auth_ctx, &ctx->deploy.upload_ctx, files, "omc/packages/");
+ getter_required(cfg, section_name, "destination", INIVAL_TYPE_STR)
+ conv_str_stackvar(data, dest)
- jfrog_cli_rt_build_publish(&ctx->deploy.auth_ctx, ctx->deploy.upload_ctx.build_name, ctx->deploy.upload_ctx.build_number);
+ char *contents;
+ struct stat st;
+ if (lstat(data.src, &st)) {
+ perror(data.src);
+ continue;
+ }
- return status;
-}
+ contents = calloc(st.st_size + 1, sizeof(*contents));
+ if (!contents) {
+ perror("template file contents");
+ continue;
+ }
+
+ FILE *fp;
+ fp = fopen(data.src, "rb");
+ if (!fp) {
+ perror(data.src);
+ free(contents);
+ continue;
+ }
+
+ if (fread(contents, st.st_size, sizeof(*contents), fp) < 1) {
+ perror("while reading template file");
+ free(contents);
+ fclose(fp);
+ continue;
+ }
+ fclose(fp);
+
+ msg(OMC_MSG_L3, "Writing %s\n", data.dest);
+ if (tpl_render_to_file(contents, data.dest)) {
+ free(contents);
+ continue;
+ }
+ free(contents);
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/src/template.c b/src/template.c
index fe7ef11..646f5f9 100644
--- a/src/template.c
+++ b/src/template.c
@@ -56,13 +56,13 @@ char *tpl_getval(char *key) {
}
static int grow(size_t z, size_t *output_bytes, char **output) {
- if (z > *output_bytes) {
+ if (z >= *output_bytes) {
size_t new_size = *output_bytes + z + 1;
#ifdef DEBUG
fprintf(stderr, "template output buffer new size: %zu\n", *output_bytes);
#endif
char *tmp = realloc(*output, new_size);
- if (!*tmp) {
+ if (!tmp) {
perror("realloc failed");
return -1;
}
@@ -77,13 +77,13 @@ char *tpl_render(char *str) {
if (!str) {
return NULL;
}
- size_t output_bytes = strlen(str);
+ size_t output_bytes = strlen(str) * 2;
char *output = NULL;
char *b_close = NULL;
char *pos = NULL;
pos = str;
- output = calloc(output_bytes + 1, sizeof(*output));
+ output = calloc(output_bytes, sizeof(*output));
if (!output) {
perror("unable to allocate output buffer");
return NULL;
@@ -94,6 +94,7 @@ char *tpl_render(char *str) {
char key[255] = {0};
char *value = NULL;
+ memset(key, 0, sizeof(key));
grow(z, &output_bytes, &output);
// At opening brace
if (!strncmp(pos, "{{", 2)) {
@@ -105,6 +106,9 @@ char *tpl_render(char *str) {
// Read key name
size_t key_len = 0;
while (isalnum(*pos) || *pos != '}') {
+ if (isspace(*pos) || isblank(*pos)) {
+ break;
+ }
key[key_len] = *pos;
key_len++;
pos++;
@@ -129,7 +133,7 @@ char *tpl_render(char *str) {
continue;
}
// Jump past closing brace
- pos = b_close + 2;
+ pos += (b_close + 2 - pos);
if (do_env) {
char *k = type_stop + 1;
@@ -145,11 +149,11 @@ char *tpl_render(char *str) {
}
if (value) {
- // Append replacement value
- grow(z + strlen(value), &output_bytes, &output);
- strcat(output, value);
// Set output iterator to end of replacement value
z += strlen(value);
+ // Append replacement value
+ grow(z, &output_bytes, &output);
+ strcat(output, value);
}
#ifdef DEBUG
@@ -166,26 +170,26 @@ char *tpl_render(char *str) {
return output;
}
-/*
-const char *TESTDATA = "follow the {{ env:PATH }}\ni have a {{ COLOR}} pencil box\n\nthe cup is full of {{LIQUID }}\n{{GOAT}}, the barn is {{ COLOR }} too!\n";
-int main() {
- char *name = "couch";
- char *liquid = "milk";
- char *color = "brown";
- char *meh = "MEHHHHHHHH";
-
- tpl_register("THING", name);
- tpl_register("LIQUID", liquid);
- tpl_register("COLOR", color);
- tpl_register("GOAT", meh);
-
- puts(TESTDATA);
- puts("----------\n");
-
- char *output = tpl_render(TESTDATA, strlen(TESTDATA) + 1024);
- printf("%s\n", output);
- free(output);
- tpl_free();
+int tpl_render_to_file(char *str, const char *filename) {
+ char *result;
+ FILE *fp;
+
+ // Render the input string
+ result = tpl_render(str);
+ if (!result) {
+ return -1;
+ }
+
+ // Open the destination file for writing
+ fp = fopen(filename, "w+");
+ if (!fp) {
+ return -1;
+ }
+
+ // Write rendered string to file
+ fprintf(fp, "%s", result);
+ fclose(fp);
+
+ guard_free(result);
return 0;
-}
- */
+} \ No newline at end of file