Skip to content

Instantly share code, notes, and snippets.

@LemonHaze420
Last active July 20, 2022 04:31
Show Gist options
  • Save LemonHaze420/cb7cfe485c268f77fa82fe3b4a28fb88 to your computer and use it in GitHub Desktop.
Save LemonHaze420/cb7cfe485c268f77fa82fe3b4a28fb88 to your computer and use it in GitHub Desktop.
compares the latest tag name to the one provided to the function and returns either true/false if it matches
#include <curl\curl.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
size_t WriteCallback(char* contents, size_t size, size_t nmemb, void* userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
bool compare_online_tag_to_our_tag(const char* szRepoAndOwner, const char* szCurrentTag)
{
bool result = true;
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
if (curl) {
struct curl_slist* headers = NULL;
curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, std::string("https://api.github.com/repos/").append(szRepoAndOwner).append("/releases/latest").c_str());
# ifdef _DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
# endif
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "GitHub API/1.0");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
# ifdef _DEBUG
printf("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
# endif
} else {
json j = json::parse(response);
if (j.contains("tag_name")) {
auto tag_name = j["tag_name"].get<std::string>();
if (!strstr(tag_name.c_str(), szCurrentTag))
result = false;
}
}
curl_easy_cleanup(curl);
}
#ifdef _DEBUG
else
printf("curl_easy_init() failed\n");
#endif
curl_global_cleanup();
return result;
}
// ...
static void thread(void) {
if(!compare_online_tag_to_our_tag("LemonHaze420/CoolRepo", "v1.0")) {
// incorrect version!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment