mirror of
https://github.com/spitkov/ynspkg.git
synced 2025-01-18 20:44:39 +01:00
fix some bugs with updating repo cache
This commit is contained in:
parent
0add4e98be
commit
fd53ef08fb
3 changed files with 87 additions and 11 deletions
|
@ -16,6 +16,7 @@ public:
|
||||||
bool upgrade(const std::string& package_name);
|
bool upgrade(const std::string& package_name);
|
||||||
bool list();
|
bool list();
|
||||||
bool interactive_mode();
|
bool interactive_mode();
|
||||||
|
bool debug();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr const char* REPO_URL = "https://raw.githubusercontent.com/spitkov/ynsrepo/refs/heads/main/repo.json";
|
static constexpr const char* REPO_URL = "https://raw.githubusercontent.com/spitkov/ynsrepo/refs/heads/main/repo.json";
|
||||||
|
|
|
@ -12,6 +12,7 @@ void print_usage() {
|
||||||
<< " remove <package> Remove a package\n"
|
<< " remove <package> Remove a package\n"
|
||||||
<< " upgrade <package> Upgrade a package\n"
|
<< " upgrade <package> Upgrade a package\n"
|
||||||
<< " list List all packages\n"
|
<< " list List all packages\n"
|
||||||
|
<< " debug Show debug information\n"
|
||||||
<< " interactive Start interactive mode\n\n"
|
<< " interactive Start interactive mode\n\n"
|
||||||
<< "Interactive Mode:\n"
|
<< "Interactive Mode:\n"
|
||||||
<< " Run 'yns interactive' to enter interactive mode where you can\n"
|
<< " Run 'yns interactive' to enter interactive mode where you can\n"
|
||||||
|
@ -22,7 +23,8 @@ void print_usage() {
|
||||||
bool needs_sudo(const std::string& command) {
|
bool needs_sudo(const std::string& command) {
|
||||||
return command == "update" || command == "install" ||
|
return command == "update" || command == "install" ||
|
||||||
command == "remove" || command == "upgrade" ||
|
command == "remove" || command == "upgrade" ||
|
||||||
command == "interactive" || command == "list";
|
command == "interactive" || command == "list" ||
|
||||||
|
command == "debug";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_self_path() {
|
std::string get_self_path() {
|
||||||
|
@ -61,6 +63,9 @@ int main(int argc, char* argv[]) {
|
||||||
else if (command == "list") {
|
else if (command == "list") {
|
||||||
return pm.list() ? 0 : 1;
|
return pm.list() ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
else if (command == "debug") {
|
||||||
|
return pm.debug() ? 0 : 1;
|
||||||
|
}
|
||||||
else if (command == "interactive") {
|
else if (command == "interactive") {
|
||||||
return pm.interactive_mode() ? 0 : 1;
|
return pm.interactive_mode() ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,22 +29,38 @@ PackageManager::PackageManager() {
|
||||||
|
|
||||||
bool PackageManager::download_file(const std::string& url, const std::string& output_path) {
|
bool PackageManager::download_file(const std::string& url, const std::string& output_path) {
|
||||||
CURL* curl = curl_easy_init();
|
CURL* curl = curl_easy_init();
|
||||||
if (!curl) return false;
|
if (!curl) {
|
||||||
|
print_error("Failed to initialize CURL");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::string response_data;
|
std::string response_data;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
|
|
||||||
|
char error_buffer[CURL_ERROR_SIZE];
|
||||||
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
|
||||||
|
|
||||||
CURLcode res = curl_easy_perform(curl);
|
CURLcode res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
|
||||||
if (res != CURLE_OK) return false;
|
if (res != CURLE_OK) {
|
||||||
|
print_error("Failed to download: " + std::string(error_buffer));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::ofstream output_file(output_path);
|
std::ofstream output_file(output_path, std::ios::trunc);
|
||||||
if (!output_file) return false;
|
if (!output_file) {
|
||||||
|
print_error("Failed to open file for writing: " + output_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
output_file << response_data;
|
output_file << response_data;
|
||||||
|
output_file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,21 +81,37 @@ bool PackageManager::execute_script(const std::string& script_path) {
|
||||||
|
|
||||||
bool PackageManager::cache_repo() {
|
bool PackageManager::cache_repo() {
|
||||||
print_progress("Updating package cache", 0);
|
print_progress("Updating package cache", 0);
|
||||||
|
|
||||||
if (!download_file(REPO_URL, CACHE_FILE)) {
|
if (!download_file(REPO_URL, CACHE_FILE)) {
|
||||||
print_error("Failed to download repository data");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
std::ifstream cache_file(CACHE_FILE);
|
||||||
|
if (!cache_file) {
|
||||||
|
print_error("Failed to read downloaded cache file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
repo_cache = json::parse(cache_file);
|
||||||
print_progress("Updating package cache", 100);
|
print_progress("Updating package cache", 100);
|
||||||
print_success("Package cache updated successfully");
|
print_success("Package cache updated successfully");
|
||||||
return true;
|
return true;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
print_error("Failed to parse repository data: " + std::string(e.what()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
json PackageManager::read_cache() {
|
json PackageManager::read_cache() {
|
||||||
try {
|
try {
|
||||||
std::ifstream cache_file(CACHE_FILE);
|
std::ifstream cache_file(CACHE_FILE);
|
||||||
if (!cache_file) return json::object();
|
if (!cache_file) {
|
||||||
|
print_error("Cache not found. Run 'yns update' first");
|
||||||
|
return json::object();
|
||||||
|
}
|
||||||
return json::parse(cache_file);
|
return json::parse(cache_file);
|
||||||
} catch (...) {
|
} catch (const std::exception& e) {
|
||||||
|
print_error("Failed to read cache: " + std::string(e.what()));
|
||||||
return json::object();
|
return json::object();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -378,3 +410,41 @@ bool PackageManager::interactive_mode() {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PackageManager::debug() {
|
||||||
|
std::cout << "\nRepository URL: " << REPO_URL << "\n";
|
||||||
|
std::cout << "Cache file: " << CACHE_FILE << "\n";
|
||||||
|
std::cout << "Installed DB: " << INSTALLED_DB << "\n\n";
|
||||||
|
|
||||||
|
std::cout << "Cache contents:\n";
|
||||||
|
std::cout << "===============\n";
|
||||||
|
try {
|
||||||
|
std::ifstream cache_file(CACHE_FILE);
|
||||||
|
if (!cache_file) {
|
||||||
|
print_error("Cache file not found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
json cache = json::parse(cache_file);
|
||||||
|
std::cout << cache.dump(2) << "\n\n";
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
print_error("Failed to read cache: " + std::string(e.what()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Installed packages:\n";
|
||||||
|
std::cout << "==================\n";
|
||||||
|
try {
|
||||||
|
std::ifstream db_file(INSTALLED_DB);
|
||||||
|
if (!db_file) {
|
||||||
|
print_error("No installed packages database found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
json db = json::parse(db_file);
|
||||||
|
std::cout << db.dump(2) << "\n";
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
print_error("Failed to read installed packages: " + std::string(e.what()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in a new issue