Skip to content

Commit 7fd0937

Browse files
committed
installer: Separate install functions to improve clarity.
misc: Fix some problems codacy detected.
1 parent 962b2b8 commit 7fd0937

File tree

10 files changed

+724
-656
lines changed

10 files changed

+724
-656
lines changed

src/clib-build.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct options {
7979
#endif
8080
};
8181

82-
clib_package_opts_t package_opts = {0};
82+
clib_package_opts_t build_package_opts = {0};
8383
clib_package_t *root_package = 0;
8484

8585
command_t program = {0};
@@ -231,8 +231,8 @@ int build_package_with_manifest_name(const char *dir, const char *file) {
231231
}
232232

233233
if (root_package && root_package->prefix) {
234-
package_opts.prefix = root_package->prefix;
235-
clib_package_set_opts(package_opts);
234+
build_package_opts.prefix = root_package->prefix;
235+
clib_package_set_opts(build_package_opts);
236236
setenv("PREFIX", package_opts.prefix, 1);
237237
} else if (opts.prefix) {
238238
setenv("PREFIX", opts.prefix, 1);
@@ -671,12 +671,12 @@ int main(int argc, char **argv) {
671671

672672
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
673673

674-
package_opts.skip_cache = opts.skip_cache;
675-
package_opts.prefix = opts.prefix;
676-
package_opts.global = opts.global;
677-
package_opts.force = opts.force;
674+
build_package_opts.skip_cache = opts.skip_cache;
675+
build_package_opts.prefix = opts.prefix;
676+
build_package_opts.global = opts.global;
677+
build_package_opts.force = opts.force;
678678

679-
clib_package_set_opts(package_opts);
679+
clib_package_set_opts(build_package_opts);
680680

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

src/clib-configure.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct options {
7070
#endif
7171
};
7272

73-
clib_package_opts_t package_opts = {0};
73+
clib_package_opts_t configure_package_opts = {0};
7474
clib_package_t *root_package = 0;
7575

7676
hash_t *configured = 0;
@@ -603,12 +603,12 @@ int main(int argc, char **argv) {
603603

604604
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
605605

606-
package_opts.skip_cache = opts.skip_cache;
607-
package_opts.prefix = opts.prefix;
608-
package_opts.global = opts.global;
609-
package_opts.force = opts.force;
606+
configure_package_opts.skip_cache = package_opts.skip_cache;
607+
configure_package_opts.prefix = package_opts.prefix;
608+
configure_package_opts.global = package_opts.global;
609+
configure_package_opts.force = package_opts.force;
610610

611-
clib_package_set_opts(package_opts);
611+
clib_package_set_opts(configure_package_opts);
612612

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

src/clib-install.c

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@
1212
#include "common/clib-validate.h"
1313
#include "debug/debug.h"
1414
#include "fs/fs.h"
15-
#include "http-get/http-get.h"
1615
#include "logger/logger.h"
1716
#include "parson/parson.h"
18-
#include "str-replace/str-replace.h"
1917
#include "version.h"
2018
#include <clib-secrets.h>
2119
#include <curl/curl.h>
22-
#include <libgen.h>
2320
#include <limits.h>
2421
#include <registry-manager.h>
2522
#include <repository.h>
2623
#include <stdio.h>
2724
#include <stdlib.h>
2825
#include <string.h>
26+
#include "clib-package-installer.h"
2927

3028
#define SX(s) #s
3129
#define S(s) SX(s)
@@ -58,8 +56,9 @@ struct options {
5856

5957
static struct options opts = {0};
6058

61-
static clib_package_opts_t package_opts = {0};
6259
static clib_package_t *root_package = NULL;
60+
static clib_secrets_t secrets = NULL;
61+
static registries_t registries = NULL;
6362

6463
/**
6564
* Option setters.
@@ -279,27 +278,18 @@ static int install_package(const char *slug) {
279278
}
280279
}
281280

282-
// Read local config files.
283-
clib_secrets_t secrets = clib_secrets_load_from_file("clib_secrets.json");
284-
repository_init(secrets); // The repository requires the secrets for authentication.
285-
clib_package_t *package = clib_package_load_local_manifest(0);
286-
287-
registries_t registries = registry_manager_init_registries(package->registries, secrets);
288-
registry_manager_fetch_registries(registries);
289281
registry_package_ptr_t package_info = registry_manger_find_package(registries, slug);
290282
if (!package_info) {
291283
debug(&debugger, "Package %s not found in any registry.", slug);
292284
return -1;
293285
}
294286

295-
296287
pkg = clib_package_new_from_slug_and_url(slug, registry_package_get_href(package_info), opts.verbose);
297288
if (NULL == pkg)
298289
return -1;
299290

300291
if (root_package && root_package->prefix) {
301292
package_opts.prefix = root_package->prefix;
302-
clib_package_set_opts(package_opts);
303293
}
304294

305295
rc = clib_package_install(pkg, opts.dir, opts.verbose);
@@ -418,38 +408,34 @@ int main(int argc, char *argv[]) {
418408
realpath(opts.prefix, prefix);
419409
unsigned long int size = strlen(prefix) + 1;
420410
opts.prefix = malloc(size);
421-
memset((void *)opts.prefix, 0, size);
422-
memcpy((void *)opts.prefix, prefix, size);
411+
memset((void *) opts.prefix, 0, size);
412+
memcpy((void *) opts.prefix, prefix, size);
423413
}
424414

425415
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
426416

427-
package_opts.skip_cache = opts.skip_cache;
428-
package_opts.prefix = opts.prefix;
429-
package_opts.global = opts.global;
430-
package_opts.force = opts.force;
431-
package_opts.token = opts.token;
417+
clib_package_opts_t install_package_opts = {0};
418+
install_package_opts.skip_cache = opts.skip_cache;
419+
install_package_opts.prefix = opts.prefix;
420+
install_package_opts.global = opts.global;
421+
install_package_opts.force = opts.force;
422+
install_package_opts.token = opts.token;
432423

433424
#ifdef HAVE_PTHREADS
434-
package_opts.concurrency = opts.concurrency;
425+
install_package_opts.concurrency = opts.concurrency;
435426
#endif
436427

437-
clib_package_set_opts(package_opts);
428+
clib_package_set_opts(install_package_opts);
438429

439-
if (!root_package) {
440-
const char *name = NULL;
441-
char *json = NULL;
442-
unsigned int i = 0;
430+
// Read local config files.
431+
secrets = clib_secrets_load_from_file("clib_secrets.json");
432+
root_package = clib_package_load_local_manifest(0);
443433

444-
do {
445-
name = manifest_names[i];
446-
json = fs_read(name);
447-
} while (NULL != manifest_names[++i] && !json);
434+
repository_init(secrets); // The repository requires the secrets for authentication.
435+
registries = registry_manager_init_registries(root_package->registries, secrets);
436+
registry_manager_fetch_registries(registries);
448437

449-
if (json) {
450-
root_package = clib_package_new(json, opts.verbose);
451-
}
452-
}
438+
clib_package_installer_init(registries, secrets);
453439

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

src/clib-update.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "parson/parson.h"
1818
#include "str-replace/str-replace.h"
1919
#include "version.h"
20+
#include <clib-package-installer.h>
2021
#include <curl/curl.h>
2122
#include <libgen.h>
2223
#include <limits.h>
@@ -50,7 +51,6 @@ struct options {
5051

5152
static struct options opts = {0};
5253

53-
static clib_package_opts_t package_opts = {0};
5454
static clib_package_t *root_package = NULL;
5555

5656
/**
@@ -331,14 +331,14 @@ int main(int argc, char *argv[]) {
331331
logger_error("error", "Failed to initialize cURL");
332332
}
333333

334-
if (opts.prefix) {
334+
if (package_opts.prefix) {
335335
char prefix[path_max];
336336
memset(prefix, 0, path_max);
337-
realpath(opts.prefix, prefix);
337+
realpath(package_opts.prefix, prefix);
338338
unsigned long int size = strlen(prefix) + 1;
339-
opts.prefix = malloc(size);
340-
memset((void *)opts.prefix, 0, size);
341-
memcpy((void *)opts.prefix, prefix, size);
339+
package_opts.prefix = malloc(size);
340+
memset((void *) package_opts.prefix, 0, size);
341+
memcpy((void *) package_opts.prefix, prefix, size);
342342
}
343343

344344
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);

src/clib-upgrade.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "tempdir/tempdir.h"
2020
#include "version.h"
2121
#include <asprintf/asprintf.h>
22+
#include <clib-package-installer.h>
2223
#include <curl/curl.h>
2324
#include <libgen.h>
2425
#include <limits.h>
@@ -54,7 +55,7 @@ struct options {
5455

5556
static struct options opts = {0};
5657

57-
static clib_package_opts_t package_opts = {0};
58+
static clib_package_opts_t upgrade_package_opts = {0};
5859
static clib_package_t *root_package = NULL;
5960

6061
/**
@@ -148,8 +149,8 @@ static int install_package(const char *slug) {
148149
}
149150

150151
if (root_package && root_package->prefix) {
151-
package_opts.prefix = root_package->prefix;
152-
clib_package_set_opts(package_opts);
152+
upgrade_package_opts.prefix = root_package->prefix;
153+
clib_package_set_opts(upgrade_package_opts);
153154
}
154155

155156
char *tmp = gettempdir();
@@ -232,23 +233,23 @@ int main(int argc, char *argv[]) {
232233
realpath(opts.prefix, prefix);
233234
unsigned long int size = strlen(prefix) + 1;
234235
opts.prefix = malloc(size);
235-
memset((void *)opts.prefix, 0, size);
236-
memcpy((void *)opts.prefix, prefix, size);
236+
memset((void *) opts.prefix, 0, size);
237+
memcpy((void *) opts.prefix, prefix, size);
237238
}
238239

239240
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
240241

241-
package_opts.skip_cache = 1;
242-
package_opts.prefix = opts.prefix;
243-
package_opts.global = 1;
244-
package_opts.force = opts.force;
245-
package_opts.token = opts.token;
242+
upgrade_package_opts.skip_cache = 1;
243+
upgrade_package_opts.prefix = opts.prefix;
244+
upgrade_package_opts.global = 1;
245+
upgrade_package_opts.force = opts.force;
246+
upgrade_package_opts.token = opts.token;
246247

247248
#ifdef HAVE_PTHREADS
248-
package_opts.concurrency = opts.concurrency;
249+
upgrade_package_opts.concurrency = opts.concurrency;
249250
#endif
250251

251-
clib_package_set_opts(package_opts);
252+
clib_package_set_opts(upgrade_package_opts);
252253

253254
char *slug = 0;
254255

0 commit comments

Comments
 (0)