clib_package_t * clib_package_new_from_slug(const char *_slug, int verbose) { if (!_slug) return NULL; // sanitize `_slug` char *author = clib_package_parse_author(_slug); if (!author) return NULL; char *name = clib_package_parse_name(_slug); if (!name) return NULL; char *version = clib_package_parse_version(_slug); if (!version) return NULL; char *url = clib_package_url(author, name, version); if (!url) return NULL; char *json_url = clib_package_file_url(url, "package.json"); if (!json_url) { free(url); return NULL; } if (verbose) clib_package_log("fetch", json_url); http_get_response_t *res = http_get(json_url); if (!res || !res->ok) { clib_package_error("error", "unable to fetch %s", json_url); free(url); return NULL; } clib_package_t *pkg = clib_package_new(res->data, verbose); if (pkg) { // force version if (NULL == pkg->version || 0 != strcmp(version, pkg->version)) pkg->version = version; // force author if (NULL == pkg->author || 0 == strcmp(author, pkg->author)) pkg->author = author; // force repo char *repo = clib_package_repo(author, name); if (NULL == pkg->repo || 0 != strcmp(repo, pkg->repo)) { pkg->repo = repo; } else { free(repo); } pkg->url = url; } http_get_free(res); return pkg; }
int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) { if (!pkg || !dir) return -1; char *pkg_dir = path_join(dir, pkg->name); if (NULL == pkg_dir) { return -1; } if (-1 == mkdirp(pkg_dir, 0777)) { free(pkg_dir); return -1; } if (NULL == pkg->url) { pkg->url = clib_package_url(pkg->author, pkg->repo_name, pkg->version); } if (NULL == pkg->url) { free(pkg_dir); return -1; } if (NULL != pkg->src) { // write package.json char *package_json = path_join(pkg_dir, "package.json"); if (NULL == package_json) { free(pkg_dir); return -1; } fs_write(package_json, pkg->json); free(package_json); // write each source list_node_t *node; list_iterator_t *it = list_iterator_new(pkg->src, LIST_HEAD); while ((node = list_iterator_next(it))) { char *filename = node->val; // download source file char *file_url = clib_package_file_url(pkg->url, filename); char *file_path = path_join(pkg_dir, basename(filename)); if (NULL == file_url || NULL == file_path) { if (file_url) free(file_url); free(pkg_dir); return -1; } if (verbose) { clib_package_log("fetch", file_url); clib_package_log("save", file_path); } int rc = http_get_file(file_url, file_path); free(file_url); free(file_path); if (-1 == rc) { if (verbose) clib_package_error("error", "unable to fetch %s", file_url); free(pkg_dir); return -1; } } list_iterator_destroy(it); } return clib_package_install_dependencies(pkg, dir, verbose); }
int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) { char *pkg_dir = NULL; char *package_json = NULL; list_iterator_t *iterator = NULL; int rc = -1; if (!pkg || !dir) goto cleanup; if (!(pkg_dir = path_join(dir, pkg->name))) goto cleanup; // create directory for pkg if (-1 == mkdirp(pkg_dir, 0777)) goto cleanup; if (NULL == pkg->url) { pkg->url = clib_package_url(pkg->author , pkg->repo_name , pkg->version); if (NULL == pkg->url) goto cleanup; } // if no sources are listed, just install if (NULL == pkg->src) goto install; // write package.json if (!(package_json = path_join(pkg_dir, "package.json"))) goto cleanup; if (-1 == fs_write(package_json, pkg->json)) { logger_error("error", "Failed to write %s", package_json); goto cleanup; } iterator = list_iterator_new(pkg->src, LIST_HEAD); list_node_t *source; while ((source = list_iterator_next(iterator))) { char *filename = NULL; char *file_url = NULL; char *file_path = NULL; int error = 0; filename = source->val; // get file URL to fetch if (!(file_url = clib_package_file_url(pkg->url, filename))) { error = 1; goto loop_cleanup; } // get file path to save if (!(file_path = path_join(pkg_dir, basename(filename)))) { error = 1; goto loop_cleanup; } // TODO the whole URL is overkill and floods my terminal if (verbose) logger_info("fetch", file_url); // fetch source file and save to disk if (-1 == http_get_file(file_url, file_path)) { logger_error("error", "unable to fetch %s", file_url); error = 1; goto loop_cleanup; } if (verbose) logger_info("save", file_path); loop_cleanup: if (file_url) free(file_url); if (file_path) free(file_path); if (error) { list_iterator_destroy(iterator); rc = -1; goto cleanup; } } install: rc = clib_package_install_dependencies(pkg, dir, verbose); cleanup: if (pkg_dir) free(pkg_dir); if (package_json) free(package_json); if (iterator) list_iterator_destroy(iterator); return rc; }
clib_package_t * clib_package_new_from_slug(const char *slug, int verbose) { char *author = NULL; char *name = NULL; char *version = NULL; char *url = NULL; char *json_url = NULL; char *repo = NULL; http_get_response_t *res = NULL; clib_package_t *pkg = NULL; // parse chunks if (!slug) goto error; if (!(author = parse_repo_owner(slug, DEFAULT_REPO_OWNER))) goto error; if (!(name = parse_repo_name(slug))) goto error; if (!(version = parse_repo_version(slug, DEFAULT_REPO_VERSION))) goto error; if (!(url = clib_package_url(author, name, version))) goto error; if (!(json_url = clib_package_file_url(url, "package.json"))) goto error; if (!(repo = clib_package_repo(author, name))) goto error; // fetch json if (verbose) logger_info("fetch", json_url); res = http_get(json_url); if (!res || !res->ok) { logger_error("error", "unable to fetch %s", json_url); goto error; } free(json_url); json_url = NULL; free(name); name = NULL; // build package pkg = clib_package_new(res->data, verbose); http_get_free(res); res = NULL; if (!pkg) goto error; // force version number if (pkg->version) { if (0 != strcmp(version, pkg->version)) { free(pkg->version); pkg->version = version; } else { free(version); } } else { pkg->version = version; } // force package author (don't know how this could fail) if (pkg->author) { if (0 != strcmp(author, pkg->author)) { free(pkg->author); pkg->author = author; } else { free(author); } } else { pkg->author = author; } // force package repo if (pkg->repo) { if (0 != strcmp(repo, pkg->repo)) { free(pkg->repo); pkg->repo = repo; } else { free(repo); } } else { pkg->repo = repo; } pkg->url = url; return pkg; error: if (author) free(author); if (name) free(name); if (version) free(version); if (url) free(url); if (json_url) free(json_url); if (repo) free(repo); if (res) http_get_free(res); if (pkg) clib_package_free(pkg); return NULL; }