char * clib_package_parse_version(const char *slug) { return parse_repo_version(slug, DEFAULT_REPO_VERSION); }
#include "describe/describe.h" #include "parse-repo.h" describe("parse_repo_version", { char *version = NULL; it("should return NULL when given an invalid slug", { assert(NULL == parse_repo_version(NULL, NULL)); assert(NULL == parse_repo_version("", NULL)); assert(NULL == parse_repo_version("/", NULL)); }); it("should support omitting owner", { version = parse_repo_version("foo@bar", "master"); assert_str_equal("bar", version); free(version); version = parse_repo_version("[email protected]", "master"); assert_str_equal("1.0.0", version); free(version); version = parse_repo_version("[email protected]", "master"); assert_str_equal("x.y.z", version); free(version); }); it("should support owner/name@version", { version = parse_repo_version("foo/bar@baz", "master"); assert_str_equal("baz", version); free(version);
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; }