Skip to content

Commit c388648

Browse files
authored
Integrating clib-validate (#242)
* Intergating clib-validate * Intergrating clib-validate * Intergrating clib-validate * Intergrating clib-validate * Intergrating clib-validate
1 parent de8b558 commit c388648

File tree

5 files changed

+123
-12
lines changed

5 files changed

+123
-12
lines changed

src/clib-install.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "commander/commander.h"
99
#include "common/clib-cache.h"
1010
#include "common/clib-package.h"
11+
#include "common/clib-validate.h"
1112
#include "debug/debug.h"
1213
#include "fs/fs.h"
1314
#include "http-get/http-get.h"
@@ -128,8 +129,7 @@ static void setopt_skip_cache(command_t *self) {
128129
}
129130

130131
static int install_local_packages_with_package_name(const char *file) {
131-
if (-1 == fs_exists(file)) {
132-
logger_error("error", "Missing clib.json or package.json");
132+
if (0 != clib_validate(file)) {
133133
return 1;
134134
}
135135

src/clib-update.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "commander/commander.h"
99
#include "common/clib-cache.h"
1010
#include "common/clib-package.h"
11+
#include "common/clib-validate.h"
1112
#include "debug/debug.h"
1213
#include "fs/fs.h"
1314
#include "http-get/http-get.h"
@@ -98,8 +99,7 @@ static void setopt_concurrency(command_t *self) {
9899
#endif
99100

100101
static int install_local_packages_with_package_name(const char *file) {
101-
if (-1 == fs_exists(file)) {
102-
logger_error("error", "Missing clib.json or package.json");
102+
if (0 != clib_validate(file)) {
103103
return 1;
104104
}
105105

src/common/clib-package.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ static inline int install_packages(list_t *list, const char *dir, int verbose) {
395395
list_iterator_destroy(iterator);
396396

397397
if (freelist) {
398-
iterator = list_iterator_new(freelist, LIST_HEAD);
399-
while ((node = list_iterator_next(iterator))) {
400-
clib_package_t *pkg = node->val;
401-
if (pkg)
402-
clib_package_free(pkg);
403-
}
404-
list_iterator_destroy(iterator);
405-
list_destroy(freelist);
398+
iterator = list_iterator_new(freelist, LIST_HEAD);
399+
while ((node = list_iterator_next(iterator))) {
400+
clib_package_t *pkg = node->val;
401+
if (pkg)
402+
clib_package_free(pkg);
403+
}
404+
list_iterator_destroy(iterator);
405+
list_destroy(freelist);
406406
}
407407
return rc;
408408
}

src/common/clib-validate.c

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

src/common/clib-validate.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// clib-validate.h
3+
//
4+
// Copyright (c) 2014-2021 clib authors
5+
// MIT licensed
6+
//
7+
8+
#ifndef CLIB_VALIDATE_H
9+
#define CLIB_VALIDATE_H
10+
11+
/**
12+
* @return 0 if the file is valid
13+
*/
14+
int clib_validate(const char *file);
15+
16+
#endif

0 commit comments

Comments
 (0)