From 10a7665246400434ea094b5e7280bbb9ae0f554d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkasv=C3=B6lgyi?= Date: Sun, 10 Jan 2021 15:54:55 +0100 Subject: [PATCH 1/2] #212 upgrade default --- src/clib-upgrade.c | 29 +++++++++--------- src/clib.c | 37 +++++----------------- src/common/clib-package.c | 4 +-- src/common/clib-release-info.c | 56 ++++++++++++++++++++++++++++++++++ src/common/clib-release-info.h | 16 ++++++++++ test/package/Makefile | 2 +- 6 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 src/common/clib-release-info.c create mode 100644 src/common/clib-release-info.h diff --git a/src/clib-upgrade.c b/src/clib-upgrade.c index 0681e404..881cb750 100644 --- a/src/clib-upgrade.c +++ b/src/clib-upgrade.c @@ -8,6 +8,7 @@ #include "commander/commander.h" #include "common/clib-cache.h" #include "common/clib-package.h" +#include "common/clib-release-info.h" #include "debug/debug.h" #include "fs/fs.h" #include "http-get/http-get.h" @@ -130,26 +131,26 @@ static int install_package(const char *slug) { } char *extended_slug = 0; + if (0 != opts.tag) { asprintf(&extended_slug, "%s@%s", slug, opts.tag); - } - - if (0 != extended_slug) { - pkg = clib_package_new_from_slug(extended_slug, opts.verbose); } else { - logger_warn("warn", - "In clib 2.6.0, this functionality will change. We will " - "default to the latest tag rather than the master branch.") - pkg = clib_package_new_from_slug(slug, opts.verbose); + const char *latest_tag = clib_relase_get_latest_tag(); + + asprintf(&extended_slug, "%s@%s", slug, latest_tag); + free(latest_tag); } + logger_info("info", "Upgrading to %s", extended_slug); + + pkg = clib_package_new_from_slug(extended_slug, opts.verbose); + if (NULL == pkg) { - if (opts.tag) { - logger_error( - "error", - "Unable to install tag %s. Please make sure it actually exists.", - opts.tag); - } + logger_error("error", + "Unable to install %s. If you provided --tag, please make " + "sure it actually exists.", + extended_slug); + return -1; } diff --git a/src/clib.c b/src/clib.c index dcd4fdbb..8f43ee00 100644 --- a/src/clib.c +++ b/src/clib.c @@ -7,6 +7,7 @@ #include "asprintf/asprintf.h" #include "common/clib-cache.h" +#include "common/clib-release-info.h" #include "debug/debug.h" #include "fs/fs.h" #include "http-get/http-get.h" @@ -79,9 +80,8 @@ static bool should_check_release(const char *path) { return now - modified >= RELEASE_NOTIFICATION_EXPIRATION; } -static void compare_versions(const JSON_Object *response, - const char *marker_file_path) { - const char *latest_version = json_object_get_string(response, "tag_name"); +static void compare_versions(const char *marker_file_path) { + const char *latest_version = clib_relase_get_latest_tag(); if (0 != strcmp(CLIB_VERSION, latest_version)) { logger_info("info", @@ -89,6 +89,7 @@ static void compare_versions(const JSON_Object *response, "upgrade with the following command: clib upgrade --tag %s", CLIB_VERSION, latest_version); } + free(latest_version); } static void notify_new_release(void) { @@ -96,7 +97,8 @@ static void notify_new_release(void) { path_join(clib_cache_meta_dir(), "release-notification-checked"); if (!marker_file_path) { - fs_write(marker_file_path, " "); + debug(&debugger, + "Unable to retrieve release notification marker file path"); return; } @@ -105,35 +107,10 @@ static void notify_new_release(void) { return; } - JSON_Value *root_json = NULL; - JSON_Object *json_object = NULL; - - http_get_response_t *res = http_get(LATEST_RELEASE_ENDPOINT); - - if (!res->ok) { - debug(&debugger, "Couldn't lookup latest release"); - goto cleanup; - } - - if (!(root_json = json_parse_string(res->data))) { - debug(&debugger, "Unable to parse release JSON response"); - goto cleanup; - } - - if (!(json_object = json_value_get_object(root_json))) { - debug(&debugger, "Unable to parse release JSON response object"); - goto cleanup; - } - - compare_versions(json_object, marker_file_path); + compare_versions(marker_file_path); fs_write(marker_file_path, " "); -cleanup: - if (root_json) - json_value_free(root_json); - free((void *)marker_file_path); - http_get_free(res); } int main(int argc, const char **argv) { diff --git a/src/common/clib-package.c b/src/common/clib-package.c index 9ed2217e..f24ec15e 100644 --- a/src/common/clib-package.c +++ b/src/common/clib-package.c @@ -1,4 +1,3 @@ -// // clib-package.c // // Copyright (c) 2014 Stephen Mathieson @@ -1065,8 +1064,7 @@ static int fetch_package_file(clib_package_t *pkg, const char *dir, char *file, #endif } -static void set_prefix(clib_package_t *pkg, long path_max) -{ +static void set_prefix(clib_package_t *pkg, long path_max) { if (NULL != opts.prefix || NULL != pkg->prefix) { char path[path_max]; memset(path, 0, path_max); diff --git a/src/common/clib-release-info.c b/src/common/clib-release-info.c new file mode 100644 index 00000000..8d277ff4 --- /dev/null +++ b/src/common/clib-release-info.c @@ -0,0 +1,56 @@ +// +// clib-release-info.c +// +// Copyright (c) 2016-2021 clib authors +// MIT licensed +// + +#include "debug/debug.h" +#include "http-get/http-get.h" +#include "parson/parson.h" +#include "strdup/strdup.h" + +#define LATEST_RELEASE_ENDPOINT \ + "https://api.github.com/repos/clibs/clib/releases/latest" + +static debug_t debugger; + +const char *clib_relase_get_latest_tag(void) { + debug_init(&debugger, "clib-release-info"); + + http_get_response_t *res = http_get(LATEST_RELEASE_ENDPOINT); + + JSON_Value *root_json = NULL; + JSON_Object *json_object = NULL; + char *tag_name = NULL; + + if (!res->ok) { + debug(&debugger, "Couldn't lookup latest release"); + goto cleanup; + } + + if (!(root_json = json_parse_string(res->data))) { + debug(&debugger, "Unable to parse release JSON response"); + goto cleanup; + } + + if (!(json_object = json_value_get_object(root_json))) { + debug(&debugger, "Unable to parse release JSON response object"); + goto cleanup; + } + + tag_name = strdup(json_object_get_string(json_object, "tag_name")); + + if (!tag_name) { + debug(&debugger, "strudp(tag_name) failed"); + goto cleanup; + } + +cleanup: + if (root_json) + json_value_free(root_json); + + http_get_free(res); + + return tag_name; +} diff --git a/src/common/clib-release-info.h b/src/common/clib-release-info.h new file mode 100644 index 00000000..14103776 --- /dev/null +++ b/src/common/clib-release-info.h @@ -0,0 +1,16 @@ +// +// clib-release-info.h +// +// Copyright (c) 2016-2021 clib authors +// MIT licensed +// + +#ifndef CLIB_RELEASE_INFO_H +#define CLIB_RELEASE_INFO_H + +/** + * @return NULL on failure, char * otherwise that must be freed + */ +const char *clib_relase_get_latest_tag(void); + +#endif diff --git a/test/package/Makefile b/test/package/Makefile index 53427f3b..024647e6 100644 --- a/test/package/Makefile +++ b/test/package/Makefile @@ -3,7 +3,7 @@ CC ?= cc VALGRIND ?= valgrind TEST_RUNNER ?= -SRC = ../../src/common/clib-package.c ../../src/common/clib-cache.c +SRC = ../../src/common/clib-package.c ../../src/common/clib-cache.c ../../src/common/clib-release-info.c DEPS += $(wildcard ../../deps/*/*.c) OBJS = $(SRC:.c=.o) $(DEPS:.c=.o) TEST_SRC = $(wildcard *.c) From bd9618a218c14db4607da064c48498a1d820d00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkasv=C3=B6lgyi?= Date: Sun, 10 Jan 2021 16:38:24 +0100 Subject: [PATCH 2/2] #212 upgrade default --- src/clib-upgrade.c | 2 +- src/clib.c | 2 +- src/common/clib-release-info.c | 2 +- src/common/clib-release-info.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/clib-upgrade.c b/src/clib-upgrade.c index 881cb750..88f50959 100644 --- a/src/clib-upgrade.c +++ b/src/clib-upgrade.c @@ -135,7 +135,7 @@ static int install_package(const char *slug) { if (0 != opts.tag) { asprintf(&extended_slug, "%s@%s", slug, opts.tag); } else { - const char *latest_tag = clib_relase_get_latest_tag(); + const char *latest_tag = clib_release_get_latest_tag(); asprintf(&extended_slug, "%s@%s", slug, latest_tag); free(latest_tag); diff --git a/src/clib.c b/src/clib.c index 8f43ee00..513a4601 100644 --- a/src/clib.c +++ b/src/clib.c @@ -81,7 +81,7 @@ static bool should_check_release(const char *path) { } static void compare_versions(const char *marker_file_path) { - const char *latest_version = clib_relase_get_latest_tag(); + const char *latest_version = clib_release_get_latest_tag(); if (0 != strcmp(CLIB_VERSION, latest_version)) { logger_info("info", diff --git a/src/common/clib-release-info.c b/src/common/clib-release-info.c index 8d277ff4..c64a2dbf 100644 --- a/src/common/clib-release-info.c +++ b/src/common/clib-release-info.c @@ -15,7 +15,7 @@ static debug_t debugger; -const char *clib_relase_get_latest_tag(void) { +const char *clib_release_get_latest_tag(void) { debug_init(&debugger, "clib-release-info"); http_get_response_t *res = http_get(LATEST_RELEASE_ENDPOINT); diff --git a/src/common/clib-release-info.h b/src/common/clib-release-info.h index 14103776..cafc7c92 100644 --- a/src/common/clib-release-info.h +++ b/src/common/clib-release-info.h @@ -11,6 +11,6 @@ /** * @return NULL on failure, char * otherwise that must be freed */ -const char *clib_relase_get_latest_tag(void); +const char *clib_release_get_latest_tag(void); #endif