Skip to content

Commit a5adbcf

Browse files
committed
installer: Separate install functions to improve clarity.
1 parent 4be36e0 commit a5adbcf

File tree

10 files changed

+724
-662
lines changed

10 files changed

+724
-662
lines changed

src/clib-build.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct options {
8585

8686
const char *manifest_names[] = {"clib.json", "package.json", 0};
8787

88-
clib_package_opts_t package_opts = {0};
88+
clib_package_opts_t build_package_opts = {0};
8989
clib_package_t *root_package = 0;
9090

9191
command_t program = {0};
@@ -237,8 +237,8 @@ int build_package_with_manifest_name(const char *dir, const char *file) {
237237
}
238238

239239
if (root_package && root_package->prefix) {
240-
package_opts.prefix = root_package->prefix;
241-
clib_package_set_opts(package_opts);
240+
build_package_opts.prefix = root_package->prefix;
241+
clib_package_set_opts(build_package_opts);
242242
setenv("PREFIX", package_opts.prefix, 1);
243243
} else if (opts.prefix) {
244244
setenv("PREFIX", opts.prefix, 1);
@@ -678,12 +678,12 @@ int main(int argc, char **argv) {
678678

679679
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
680680

681-
package_opts.skip_cache = opts.skip_cache;
682-
package_opts.prefix = opts.prefix;
683-
package_opts.global = opts.global;
684-
package_opts.force = opts.force;
681+
build_package_opts.skip_cache = opts.skip_cache;
682+
build_package_opts.prefix = opts.prefix;
683+
build_package_opts.global = opts.global;
684+
build_package_opts.force = opts.force;
685685

686-
clib_package_set_opts(package_opts);
686+
clib_package_set_opts(build_package_opts);
687687

688688
if (0 == program.argc || (argc == rest_offset + rest_argc)) {
689689
rc = build_package(CWD);

src/clib-configure.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct options {
7676

7777
const char *manifest_names[] = {"clib.json", "package.json", 0};
7878

79-
clib_package_opts_t package_opts = {0};
79+
clib_package_opts_t configure_package_opts = {0};
8080
clib_package_t *root_package = 0;
8181

8282
hash_t *configured = 0;
@@ -609,12 +609,12 @@ int main(int argc, char **argv) {
609609

610610
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
611611

612-
package_opts.skip_cache = opts.skip_cache;
613-
package_opts.prefix = opts.prefix;
614-
package_opts.global = opts.global;
615-
package_opts.force = opts.force;
612+
configure_package_opts.skip_cache = package_opts.skip_cache;
613+
configure_package_opts.prefix = package_opts.prefix;
614+
configure_package_opts.global = package_opts.global;
615+
configure_package_opts.force = package_opts.force;
616616

617-
clib_package_set_opts(package_opts);
617+
clib_package_set_opts(configure_package_opts);
618618

619619
if (0 == program.argc || (argc == rest_offset + rest_argc)) {
620620
rc = configure_package(CWD);

src/clib-install.c

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111
#include "common/clib-validate.h"
1212
#include "debug/debug.h"
1313
#include "fs/fs.h"
14-
#include "http-get/http-get.h"
1514
#include "logger/logger.h"
1615
#include "parson/parson.h"
17-
#include "str-replace/str-replace.h"
1816
#include "version.h"
1917
#include <clib-secrets.h>
2018
#include <curl/curl.h>
21-
#include <libgen.h>
2219
#include <limits.h>
2320
#include <registry-manager.h>
2421
#include <repository.h>
2522
#include <stdio.h>
2623
#include <stdlib.h>
2724
#include <string.h>
25+
#include "clib-package-installer.h"
2826

2927
#define CLIB_PACKAGE_CACHE_TIME 30 * 24 * 60 * 60
3028

@@ -65,8 +63,9 @@ static struct options opts = {0};
6563

6664
static const char *manifest_names[] = {"clib.json", "package.json", NULL};
6765

68-
static clib_package_opts_t package_opts = {0};
6966
static clib_package_t *root_package = NULL;
67+
static clib_secrets_t secrets = NULL;
68+
static registries_t registries = NULL;
7069

7170
/**
7271
* Option setters.
@@ -286,27 +285,18 @@ static int install_package(const char *slug) {
286285
}
287286
}
288287

289-
// Read local config files.
290-
clib_secrets_t secrets = clib_secrets_load_from_file("clib_secrets.json");
291-
repository_init(secrets); // The repository requires the secrets for authentication.
292-
clib_package_t *package = clib_package_load_local_manifest(0);
293-
294-
registries_t registries = registry_manager_init_registries(package->registries, secrets);
295-
registry_manager_fetch_registries(registries);
296288
registry_package_ptr_t package_info = registry_manger_find_package(registries, slug);
297289
if (!package_info) {
298290
debug(&debugger, "Package %s not found in any registry.", slug);
299291
return -1;
300292
}
301293

302-
303294
pkg = clib_package_new_from_slug_and_url(slug, registry_package_get_href(package_info), opts.verbose);
304295
if (NULL == pkg)
305296
return -1;
306297

307298
if (root_package && root_package->prefix) {
308299
package_opts.prefix = root_package->prefix;
309-
clib_package_set_opts(package_opts);
310300
}
311301

312302
rc = clib_package_install(pkg, opts.dir, opts.verbose);
@@ -425,38 +415,34 @@ int main(int argc, char *argv[]) {
425415
realpath(opts.prefix, prefix);
426416
unsigned long int size = strlen(prefix) + 1;
427417
opts.prefix = malloc(size);
428-
memset((void *)opts.prefix, 0, size);
429-
memcpy((void *)opts.prefix, prefix, size);
418+
memset((void *) opts.prefix, 0, size);
419+
memcpy((void *) opts.prefix, prefix, size);
430420
}
431421

432422
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
433423

434-
package_opts.skip_cache = opts.skip_cache;
435-
package_opts.prefix = opts.prefix;
436-
package_opts.global = opts.global;
437-
package_opts.force = opts.force;
438-
package_opts.token = opts.token;
424+
clib_package_opts_t install_package_opts;
425+
install_package_opts.skip_cache = opts.skip_cache;
426+
install_package_opts.prefix = opts.prefix;
427+
install_package_opts.global = opts.global;
428+
install_package_opts.force = opts.force;
429+
install_package_opts.token = opts.token;
439430

440431
#ifdef HAVE_PTHREADS
441-
package_opts.concurrency = opts.concurrency;
432+
install_package_opts.concurrency = opts.concurrency;
442433
#endif
443434

444-
clib_package_set_opts(package_opts);
435+
clib_package_set_opts(install_package_opts);
445436

446-
if (!root_package) {
447-
const char *name = NULL;
448-
char *json = NULL;
449-
unsigned int i = 0;
437+
// Read local config files.
438+
secrets = clib_secrets_load_from_file("clib_secrets.json");
439+
root_package = clib_package_load_local_manifest(0);
450440

451-
do {
452-
name = manifest_names[i];
453-
json = fs_read(name);
454-
} while (NULL != manifest_names[++i] && !json);
441+
repository_init(secrets); // The repository requires the secrets for authentication.
442+
registries = registry_manager_init_registries(root_package->registries, secrets);
443+
registry_manager_fetch_registries(registries);
455444

456-
if (json) {
457-
root_package = clib_package_new(json, opts.verbose);
458-
}
459-
}
445+
clib_package_installer_init(registries, secrets);
460446

461447
int code = 0 == program.argc ? install_local_packages()
462448
: install_packages(program.argc, program.argv);

src/clib-update.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "parson/parson.h"
1717
#include "str-replace/str-replace.h"
1818
#include "version.h"
19+
#include <clib-package-installer.h>
1920
#include <curl/curl.h>
2021
#include <libgen.h>
2122
#include <limits.h>
@@ -57,7 +58,6 @@ static struct options opts = {0};
5758

5859
static const char *manifest_names[] = {"clib.json", "package.json", NULL};
5960

60-
static clib_package_opts_t package_opts = {0};
6161
static clib_package_t *root_package = NULL;
6262

6363
/**
@@ -338,26 +338,26 @@ int main(int argc, char *argv[]) {
338338
logger_error("error", "Failed to initialize cURL");
339339
}
340340

341-
if (opts.prefix) {
341+
if (package_opts.prefix) {
342342
char prefix[path_max];
343343
memset(prefix, 0, path_max);
344-
realpath(opts.prefix, prefix);
344+
realpath(package_opts.prefix, prefix);
345345
unsigned long int size = strlen(prefix) + 1;
346-
opts.prefix = malloc(size);
347-
memset((void *)opts.prefix, 0, size);
348-
memcpy((void *)opts.prefix, prefix, size);
346+
package_opts.prefix = malloc(size);
347+
memset((void *) package_opts.prefix, 0, size);
348+
memcpy((void *) package_opts.prefix, prefix, size);
349349
}
350350

351351
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
352352

353353
package_opts.skip_cache = 1;
354-
package_opts.prefix = opts.prefix;
354+
package_opts.prefix = package_opts.prefix;
355355
package_opts.global = 0;
356356
package_opts.force = 1;
357-
package_opts.token = opts.token;
357+
package_opts.token = package_opts.token;
358358

359359
#ifdef HAVE_PTHREADS
360-
package_opts.concurrency = opts.concurrency;
360+
package_opts.concurrency = package_opts.concurrency;
361361
#endif
362362

363363
clib_package_set_opts(package_opts);

src/clib-upgrade.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "tempdir/tempdir.h"
1919
#include "version.h"
2020
#include <asprintf/asprintf.h>
21+
#include <clib-package-installer.h>
2122
#include <curl/curl.h>
2223
#include <libgen.h>
2324
#include <limits.h>
@@ -61,7 +62,7 @@ static struct options opts = {0};
6162

6263
static const char *manifest_names[] = {"clib.json", "package.json", NULL};
6364

64-
static clib_package_opts_t package_opts = {0};
65+
static clib_package_opts_t upgrade_package_opts = {0};
6566
static clib_package_t *root_package = NULL;
6667

6768
/**
@@ -155,8 +156,8 @@ static int install_package(const char *slug) {
155156
}
156157

157158
if (root_package && root_package->prefix) {
158-
package_opts.prefix = root_package->prefix;
159-
clib_package_set_opts(package_opts);
159+
upgrade_package_opts.prefix = root_package->prefix;
160+
clib_package_set_opts(upgrade_package_opts);
160161
}
161162

162163
char *tmp = gettempdir();
@@ -239,23 +240,23 @@ int main(int argc, char *argv[]) {
239240
realpath(opts.prefix, prefix);
240241
unsigned long int size = strlen(prefix) + 1;
241242
opts.prefix = malloc(size);
242-
memset((void *)opts.prefix, 0, size);
243-
memcpy((void *)opts.prefix, prefix, size);
243+
memset((void *) opts.prefix, 0, size);
244+
memcpy((void *) opts.prefix, prefix, size);
244245
}
245246

246247
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
247248

248-
package_opts.skip_cache = 1;
249-
package_opts.prefix = opts.prefix;
250-
package_opts.global = 1;
251-
package_opts.force = opts.force;
252-
package_opts.token = opts.token;
249+
upgrade_package_opts.skip_cache = 1;
250+
upgrade_package_opts.prefix = opts.prefix;
251+
upgrade_package_opts.global = 1;
252+
upgrade_package_opts.force = opts.force;
253+
upgrade_package_opts.token = opts.token;
253254

254255
#ifdef HAVE_PTHREADS
255-
package_opts.concurrency = opts.concurrency;
256+
upgrade_package_opts.concurrency = opts.concurrency;
256257
#endif
257258

258-
clib_package_set_opts(package_opts);
259+
clib_package_set_opts(upgrade_package_opts);
259260

260261
char *slug = 0;
261262

0 commit comments

Comments
 (0)