Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clib upgrade default behavior #230

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/clib-upgrade.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down
37 changes: 7 additions & 30 deletions src/clib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -79,24 +80,25 @@ 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",
"You are using clib %s, a new version is avalable. You can "
"upgrade with the following command: clib upgrade --tag %s",
CLIB_VERSION, latest_version);
}
free(latest_version);
}

static void notify_new_release(void) {
const char *marker_file_path =
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;
}

Expand All @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions src/common/clib-package.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// clib-package.c
//
// Copyright (c) 2014 Stephen Mathieson
Expand Down Expand Up @@ -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);
Expand Down
56 changes: 56 additions & 0 deletions src/common/clib-release-info.c
Original file line number Diff line number Diff line change
@@ -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) {
Isty001 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
16 changes: 16 additions & 0 deletions src/common/clib-release-info.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/package/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down