Skip to content

Commit f9389c1

Browse files
committed
update: Fix clib-update with the new way of fetching packages, fix a bug with downloading packages from gitlab, move common definitinos to clib-settings
1 parent 7fd0937 commit f9389c1

11 files changed

+174
-162
lines changed

src/clib-build.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <path-join/path-join.h>
4242
#include <str-flatten/str-flatten.h>
4343
#include <trim/trim.h>
44-
44+
#include "clib-settings.h"
4545
#include "version.h"
4646

4747
#define PROGRAM_NAME "clib-build"

src/clib-configure.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <path-join/path-join.h>
4242
#include <str-flatten/str-flatten.h>
4343
#include <trim/trim.h>
44-
44+
#include "clib-settings.h"
4545
#include "version.h"
4646

4747
#define PROGRAM_NAME "clib-configure"

src/clib-search.c

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,50 @@ static void setopt_nocache(command_t *self) { opt_cache = 0; }
4949

5050
static void setopt_json(command_t *self) { opt_json = 1; }
5151

52-
#define COMPARE(v) \
53-
{ \
54-
if (NULL == v) { \
55-
rc = 0; \
56-
goto cleanup; \
57-
} \
58-
case_lower(v); \
59-
for (int i = 0; i < count; i++) { \
60-
if (strstr(v, args[i])) { \
61-
rc = 1; \
62-
break; \
63-
} \
64-
} \
52+
#define COMPARE(v) \
53+
{ \
54+
if (NULL == v) { \
55+
rc = 0; \
56+
goto cleanup; \
57+
} \
58+
case_lower(v); \
59+
for (int i = 0; i < count; i++) { \
60+
if (strstr(v, args[i])) { \
61+
rc = 1; \
62+
break; \
63+
} \
64+
} \
6565
}
6666

6767
static int matches(int count, char *args[], registry_package_ptr_t pkg) {
68-
// Display all packages if there's no query
69-
if (0 == count)
70-
return 1;
71-
72-
char *description = NULL;
73-
char *name = NULL;
74-
char *repo = NULL;
75-
char *href = NULL;
76-
int rc = 0;
77-
78-
name = clib_package_parse_name(registry_package_get_id(pkg));
79-
COMPARE(name);
80-
81-
description = strdup(registry_package_get_description(pkg));
82-
COMPARE(description);
83-
84-
repo = strdup(registry_package_get_id(pkg));
85-
COMPARE(repo);
86-
87-
href = strdup(registry_package_get_href(pkg));
88-
COMPARE(href);
89-
90-
cleanup:
91-
free(description);
92-
free(name);
93-
free(repo);
94-
free(href);
95-
return rc;
68+
// Display all packages if there's no query
69+
if (0 == count)
70+
return 1;
71+
72+
char *description = NULL;
73+
char *name = NULL;
74+
char *repo = NULL;
75+
char *href = NULL;
76+
int rc = 0;
77+
78+
name = clib_package_parse_name(registry_package_get_id(pkg));
79+
COMPARE(name);
80+
81+
description = strdup(registry_package_get_description(pkg));
82+
COMPARE(description);
83+
84+
repo = strdup(registry_package_get_id(pkg));
85+
COMPARE(repo);
86+
87+
href = strdup(registry_package_get_href(pkg));
88+
COMPARE(href);
89+
90+
cleanup:
91+
free(description);
92+
free(name);
93+
free(repo);
94+
free(href);
95+
return rc;
9696
}
9797

9898
/*
@@ -126,67 +126,67 @@ static char *wiki_html_cache() {
126126
static void display_package(const registry_package_ptr_t pkg,
127127
cc_color_t fg_color_highlight,
128128
cc_color_t fg_color_text) {
129-
cc_fprintf(fg_color_highlight, stdout, " %s\n", registry_package_get_id(pkg));
130-
printf(" url: ");
131-
cc_fprintf(fg_color_text, stdout, "%s\n", registry_package_get_href(pkg));
132-
printf(" desc: ");
133-
cc_fprintf(fg_color_text, stdout, "%s\n", registry_package_get_description(pkg));
134-
printf("\n");
129+
cc_fprintf(fg_color_highlight, stdout, " %s\n", registry_package_get_id(pkg));
130+
printf(" url: ");
131+
cc_fprintf(fg_color_text, stdout, "%s\n", registry_package_get_href(pkg));
132+
printf(" desc: ");
133+
cc_fprintf(fg_color_text, stdout, "%s\n", registry_package_get_description(pkg));
134+
printf("\n");
135135
}
136136

137137
static void add_package_to_json(const registry_package_ptr_t pkg,
138138
JSON_Array *json_list) {
139-
JSON_Value *json_pkg_root = json_value_init_object();
140-
JSON_Object *json_pkg = json_value_get_object(json_pkg_root);
139+
JSON_Value *json_pkg_root = json_value_init_object();
140+
JSON_Object *json_pkg = json_value_get_object(json_pkg_root);
141141

142-
json_object_set_string(json_pkg, "repo", registry_package_get_id(pkg));
143-
json_object_set_string(json_pkg, "href", registry_package_get_href(pkg));
144-
json_object_set_string(json_pkg, "description", registry_package_get_description(pkg));
145-
json_object_set_string(json_pkg, "category", registry_package_get_category(pkg));
142+
json_object_set_string(json_pkg, "repo", registry_package_get_id(pkg));
143+
json_object_set_string(json_pkg, "href", registry_package_get_href(pkg));
144+
json_object_set_string(json_pkg, "description", registry_package_get_description(pkg));
145+
json_object_set_string(json_pkg, "category", registry_package_get_category(pkg));
146146

147-
json_array_append_value(json_list, json_pkg_root);
147+
json_array_append_value(json_list, json_pkg_root);
148148
}
149149

150150
int main(int argc, char *argv[]) {
151-
opt_color = 1;
152-
opt_cache = 1;
151+
opt_color = 1;
152+
opt_cache = 1;
153153

154-
debug_init(&debugger, "clib-search");
154+
debug_init(&debugger, "clib-search");
155155

156-
clib_cache_init(CLIB_SEARCH_CACHE_TIME);
156+
clib_cache_init(CLIB_SEARCH_CACHE_TIME);
157157

158-
command_t program;
159-
command_init(&program, "clib-search", CLIB_VERSION);
160-
program.usage = "[options] [query ...]";
158+
command_t program;
159+
command_init(&program, "clib-search", CLIB_VERSION);
160+
program.usage = "[options] [query ...]";
161161

162-
command_option(&program, "-n", "--no-color", "don't colorize output",
163-
setopt_nocolor);
162+
command_option(&program, "-n", "--no-color", "don't colorize output",
163+
setopt_nocolor);
164164

165-
command_option(&program, "-c", "--skip-cache", "skip the search cache",
166-
setopt_nocache);
165+
command_option(&program, "-c", "--skip-cache", "skip the search cache",
166+
setopt_nocache);
167167

168-
command_option(&program, "-j", "--json", "generate a serialized JSON output",
169-
setopt_json);
168+
command_option(&program, "-j", "--json", "generate a serialized JSON output",
169+
setopt_json);
170170

171-
command_parse(&program, argc, argv);
171+
command_parse(&program, argc, argv);
172172

173-
for (int i = 0; i < program.argc; i++)
174-
case_lower(program.argv[i]);
173+
for (int i = 0; i < program.argc; i++)
174+
case_lower(program.argv[i]);
175175

176-
// set color theme
177-
cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE;
178-
cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE;
176+
// set color theme
177+
cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE;
178+
cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE;
179179

180-
// We search the local manifest for extra registries.
181-
// It is important to give the extra registries preference over the default registry.
182-
clib_secrets_t secrets = clib_secrets_load_from_file("clib_secrets.json");
180+
// We search the local manifest for extra registries.
181+
// It is important to give the extra registries preference over the default registry.
182+
clib_secrets_t secrets = clib_secrets_load_from_file("clib_secrets.json");
183183

184-
clib_package_t *package = clib_package_load_local_manifest(0);
185-
registries_t registries = registry_manager_init_registries(package->registries, secrets);
186-
registry_manager_fetch_registries(registries);
184+
clib_package_t *package = clib_package_load_local_manifest(0);
185+
registries_t registries = registry_manager_init_registries(package->registries, secrets);
186+
registry_manager_fetch_registries(registries);
187187

188-
// TODO, implement caching for the new registries.
189-
/*
188+
// TODO, implement caching for the new registries.
189+
/*
190190
char *html = wiki_html_cache();
191191
if (NULL == html) {
192192
command_free(&program);
@@ -198,45 +198,45 @@ int main(int argc, char *argv[]) {
198198
debug(&debugger, "found %zu packages", pkgs->len);
199199
*/
200200

201-
registry_iterator_t it = registry_iterator_new(registries);
202-
registry_ptr_t registry = NULL;
203-
while ((registry = registry_iterator_next(it))) {
204-
printf("SEARCH: packages from %s\n", registry_get_url(registry));
205-
registry_package_ptr_t pkg;
206-
registry_package_iterator_t it = registry_package_iterator_new(registry);
201+
registry_iterator_t it = registry_iterator_new(registries);
202+
registry_ptr_t registry = NULL;
203+
while ((registry = registry_iterator_next(it))) {
204+
printf("SEARCH: packages from %s\n", registry_get_url(registry));
205+
registry_package_ptr_t pkg;
206+
registry_package_iterator_t it = registry_package_iterator_new(registry);
207207

208-
JSON_Array *json_list = NULL;
209-
JSON_Value *json_list_root = NULL;
208+
JSON_Array *json_list = NULL;
209+
JSON_Value *json_list_root = NULL;
210210

211-
if (opt_json) {
212-
json_list_root = json_value_init_array();
213-
json_list = json_value_get_array(json_list_root);
214-
}
211+
if (opt_json) {
212+
json_list_root = json_value_init_array();
213+
json_list = json_value_get_array(json_list_root);
214+
}
215215

216-
printf("\n");
217-
218-
while ((pkg = registry_package_iterator_next(it))) {
219-
if (matches(program.argc, program.argv, pkg)) {
220-
if (opt_json) {
221-
add_package_to_json(pkg, json_list);
222-
} else {
223-
display_package(pkg, fg_color_highlight, fg_color_text);
224-
}
225-
} else {
226-
debug(&debugger, "skipped package %s", registry_package_get_id(pkg));
227-
}
228-
}
216+
printf("\n");
229217

218+
while ((pkg = registry_package_iterator_next(it))) {
219+
if (matches(program.argc, program.argv, pkg)) {
230220
if (opt_json) {
231-
char *serialized = json_serialize_to_string_pretty(json_list_root);
232-
puts(serialized);
233-
234-
json_free_serialized_string(serialized);
235-
json_value_free(json_list_root);
221+
add_package_to_json(pkg, json_list);
222+
} else {
223+
display_package(pkg, fg_color_highlight, fg_color_text);
236224
}
225+
} else {
226+
debug(&debugger, "skipped package %s", registry_package_get_id(pkg));
227+
}
228+
}
237229

238-
registry_package_iterator_destroy(it);
230+
if (opt_json) {
231+
char *serialized = json_serialize_to_string_pretty(json_list_root);
232+
puts(serialized);
233+
234+
json_free_serialized_string(serialized);
235+
json_value_free(json_list_root);
239236
}
240-
command_free(&program);
241-
return 0;
237+
238+
registry_package_iterator_destroy(it);
239+
}
240+
command_free(&program);
241+
return 0;
242242
}

src/clib-settings.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CLIB_SRC_CLIB_SETTINGS_H
2+
#define CLIB_SRC_CLIB_SETTINGS_H
3+
4+
// Shared settings
5+
#define CLIB_PACKAGE_CACHE_TIME 30 * 24 * 60 * 60
6+
7+
#ifdef HAVE_PTHREADS
8+
#define MAX_THREADS 12
9+
#endif
10+
11+
const char *manifest_names[] = {"clib.json", "package.json", 0};
12+
13+
#endif//CLIB_SRC_CLIB_SETTINGS_H

src/clib-uninstall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "version.h"
2020
#include <stdlib.h>
2121
#include <string.h>
22+
#include "clib-settings.h"
2223

2324
#define CLIB_UNINSTALL_DEFAULT_TARGET "make uninstall"
2425

src/clib-update.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <curl/curl.h>
2222
#include <libgen.h>
2323
#include <limits.h>
24+
#include <repository.h>
2425
#include <stdio.h>
2526
#include <stdlib.h>
2627
#include <string.h>
@@ -52,6 +53,8 @@ struct options {
5253
static struct options opts = {0};
5354

5455
static clib_package_t *root_package = NULL;
56+
static clib_secrets_t secrets = NULL;
57+
static registries_t registries = NULL;
5558

5659
/**
5760
* Option setters.
@@ -355,6 +358,16 @@ int main(int argc, char *argv[]) {
355358

356359
clib_package_set_opts(package_opts);
357360

361+
// Read local config files.
362+
secrets = clib_secrets_load_from_file("clib_secrets.json");
363+
root_package = clib_package_load_local_manifest(0);
364+
365+
repository_init(secrets); // The repository requires the secrets for authentication.
366+
registries = registry_manager_init_registries(root_package->registries, secrets);
367+
registry_manager_fetch_registries(registries);
368+
369+
clib_package_installer_init(registries, secrets);
370+
358371
int code = 0 == program.argc ? install_local_packages()
359372
: install_packages(program.argc, program.argv);
360373

0 commit comments

Comments
 (0)