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

Notifying the user if new version is available #217

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions deps/http-get/clib.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-get",
"version": "0.2.2",
"version": "0.3.0",
"repo": "clibs/http-get.c",
"description": "Simple HTTP GET requests backed by libcurl",
"keywords": [
Expand All @@ -14,4 +14,4 @@
"src/http-get.c",
"src/http-get.h"
]
}
}
1 change: 1 addition & 0 deletions deps/http-get/http-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ http_get_response_t *http_get_shared(const char *url, CURLSH *share) {
curl_easy_setopt(req, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(req, CURLOPT_WRITEFUNCTION, http_get_cb);
curl_easy_setopt(req, CURLOPT_WRITEDATA, (void *) res);
curl_easy_setopt(req, CURLOPT_USERAGENT, "http-get.c/"HTTP_GET_VERSION);

int c = curl_easy_perform(req);

Expand Down
2 changes: 1 addition & 1 deletion deps/http-get/http-get.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <unistd.h>
#endif

#define HTTP_GET_VERSION "0.1.0"
#define HTTP_GET_VERSION "0.3.0"

typedef struct {
char *data;
Expand Down
87 changes: 87 additions & 0 deletions src/clib.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
//

#include "asprintf/asprintf.h"
#include "common/clib-cache.h"
#include "debug/debug.h"
#include "fs/fs.h"
#include "http-get/http-get.h"
#include "logger/logger.h"
#include "parson/parson.h"
#include "path-join/path-join.h"
#include "str-flatten/str-flatten.h"
#include "strdup/strdup.h"
#include "trim/trim.h"
#include "version.h"
#include "which/which.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -22,6 +29,10 @@
#define realpath(a, b) _fullpath(a, b, strlen(a))
#endif

#define LATEST_RELEASE_ENDPOINT \
"https://api.github.com/repos/clibs/clib/releases/latest"
#define RELEASE_NOTIFICATION_EXPIRATION 3 * 24 * 60 * 60 // 3 days

debug_t debugger;

static const char *usage =
Expand Down Expand Up @@ -54,6 +65,78 @@ static const char *usage =
} \
})

static bool should_check_release(const char *path) {
fs_stats *stat = fs_stat(path);

if (!stat) {
return true;
}

time_t modified = stat->st_mtime;
time_t now = time(NULL);
free(stat);

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");

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);
}
}

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, " ");
return;
}

if (!should_check_release(marker_file_path)) {
debug(&debugger, "No need to check for new release yet");
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");
exit(0);
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);
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) {

char *cmd = NULL;
Expand All @@ -65,6 +148,10 @@ int main(int argc, const char **argv) {

debug_init(&debugger, "clib");

clib_cache_meta_init();

notify_new_release();

// usage
if (NULL == argv[1] || 0 == strncmp(argv[1], "-h", 2) ||
0 == strncmp(argv[1], "--help", 6)) {
Expand Down
13 changes: 13 additions & 0 deletions src/common/clib-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
static char package_cache_dir[BUFSIZ];
static char search_cache[BUFSIZ];
static char json_cache_dir[BUFSIZ];
static char meta_cache_dir[BUFSIZ];
static time_t expiration;

static void json_cache_path(char *pkg_cache, char *author, char *name,
Expand All @@ -59,6 +60,18 @@ static int check_dir(char *dir) {
return 0;
}

int clib_cache_meta_init(void) {
sprintf(meta_cache_dir, BASE_CACHE_PATTERN "/meta", BASE_DIR);

if (0 != check_dir(meta_cache_dir)) {
return -1;
}

return 0;
}

const char *clib_cache_meta_dir(void) { return meta_cache_dir; }

int clib_cache_init(time_t exp) {
expiration = exp;

Expand Down
12 changes: 12 additions & 0 deletions src/common/clib-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
*/
int clib_cache_init(time_t expiration);

/**
* Initializes the internal cache directory
*
* @return 0 on success, -1 otherwise
*/
int clib_cache_meta_init(void);

/**
* @return directory of internally cached data
*/
const char *clib_cache_meta_dir(void);

/**
* @return The base base dir
*/
Expand Down
6 changes: 3 additions & 3 deletions src/common/clib-package.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
if (0 != fetch) {
fetch_package_file_thread_data_t *data = fetch;
int *status;
pthread_join(data->thread, (void **) &status);
pthread_join(data->thread, (void **)&status);
if (NULL != status) {
rc = *status;
free(status);
Expand Down Expand Up @@ -1493,7 +1493,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
while (--i >= 0) {
fetch_package_file_thread_data_t *data = fetchs[i];
int *status;
pthread_join(data->thread, (void **) &status);
pthread_join(data->thread, (void **)&status);
free(data);
fetchs[i] = NULL;

Expand Down Expand Up @@ -1523,7 +1523,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
fetch_package_file_thread_data_t *data = fetchs[i];
int *status;

pthread_join(data->thread, (void **) &status);
pthread_join(data->thread, (void **)&status);

(void)pending--;
free(data);
Expand Down