|
| 1 | + |
| 2 | +// |
| 3 | +// clib-validate: `clib-validate.c` |
| 4 | +// |
| 5 | +// Copyright (c) 2014 Stephen Mathieson |
| 6 | +// Copyright (c) 2021 clib authors |
| 7 | +// |
| 8 | +// MIT licensed |
| 9 | +// |
| 10 | + |
| 11 | +#include "fs/fs.h" |
| 12 | +#include "logger/logger.h" |
| 13 | +#include "parse-repo/parse-repo.h" |
| 14 | +#include "parson/parson.h" |
| 15 | +#include "path-join/path-join.h" |
| 16 | +#include "strdup/strdup.h" |
| 17 | +#include <stdio.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <string.h> |
| 20 | +#include <unistd.h> |
| 21 | + |
| 22 | +#define ERROR_FORMAT(err, ...) \ |
| 23 | + ({ \ |
| 24 | + rc = 1; \ |
| 25 | + logger_error("error", err, __VA_ARGS__); \ |
| 26 | + goto done; \ |
| 27 | + }) |
| 28 | + |
| 29 | +#define WARN(warning) ({ logger_warn("warning", warning); }); |
| 30 | + |
| 31 | +#define WARN_FORMAT(warning, ...) \ |
| 32 | + ({ logger_warn("warning", warning, __VA_ARGS__); }); |
| 33 | + |
| 34 | +#define WARN_MISSING(key, file) \ |
| 35 | + ({ WARN_FORMAT("missing " #key " in %s", file); }) |
| 36 | + |
| 37 | +#define require_string(name, file) \ |
| 38 | + ({ \ |
| 39 | + const char *__##name = json_object_get_string(obj, #name); \ |
| 40 | + if (!(__##name)) \ |
| 41 | + WARN_MISSING(#name, file); \ |
| 42 | + }) |
| 43 | + |
| 44 | +int clib_validate(const char *file) { |
| 45 | + const char *repo = NULL; |
| 46 | + char *repo_owner = NULL; |
| 47 | + char *repo_name = NULL; |
| 48 | + int rc = 0; |
| 49 | + JSON_Value *root = NULL; |
| 50 | + JSON_Object *obj = NULL; |
| 51 | + JSON_Value *src = NULL; |
| 52 | + |
| 53 | + if (-1 == fs_exists(file)) |
| 54 | + ERROR_FORMAT("no such file: %s", file); |
| 55 | + if (!(root = json_parse_file(file))) |
| 56 | + ERROR_FORMAT("malformed file: %s", file); |
| 57 | + if (!(obj = json_value_get_object(root))) |
| 58 | + ERROR_FORMAT("malformed file: %s", file); |
| 59 | + |
| 60 | + require_string(name, file); |
| 61 | + require_string(version, file); |
| 62 | + // TODO: validate semver |
| 63 | + |
| 64 | + repo = json_object_get_string(obj, "repo"); |
| 65 | + if (!repo) { |
| 66 | + WARN_MISSING("repo", file); |
| 67 | + } else { |
| 68 | + if (!(repo_name = parse_repo_name(repo))) |
| 69 | + WARN("invalid repo"); |
| 70 | + if (!(repo_owner = parse_repo_owner(repo, NULL))) |
| 71 | + WARN("invalid repo"); |
| 72 | + } |
| 73 | + |
| 74 | + require_string(description, file); |
| 75 | + require_string(license, file); |
| 76 | + |
| 77 | + if (!json_object_get_array(obj, "keywords")) { |
| 78 | + WARN_MISSING("keywords", file); |
| 79 | + } |
| 80 | + |
| 81 | + src = json_object_get_value(obj, "src"); |
| 82 | + if (!src) { |
| 83 | + |
| 84 | + if (!json_object_get_string(obj, "install")) |
| 85 | + ERROR_FORMAT("Must have either src or install defined in %s", file); |
| 86 | + |
| 87 | + } else if (json_value_get_type(src) != JSONArray) { |
| 88 | + WARN("src should be an array") |
| 89 | + } |
| 90 | + |
| 91 | +done: |
| 92 | + if (root) |
| 93 | + json_value_free(root); |
| 94 | + return rc; |
| 95 | +} |
0 commit comments