feat: Update 2 files
This commit is contained in:
@@ -254,8 +254,6 @@ complete -F _dropshell_completions ds
|
||||
// determine the architecture of the system
|
||||
std::string arch = shared_commands::get_arch();
|
||||
|
||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||
|
||||
// check that the user that owns the exe is the current user this process is running as.
|
||||
struct stat st;
|
||||
if (stat(exe_path.c_str(), &st) != 0) {
|
||||
@@ -269,48 +267,111 @@ complete -F _dropshell_completions ds
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get current version (format: YYYY.MMDD.HHMM)
|
||||
std::string runvercmd = exe_path.string() + " version";
|
||||
std::string currentver = _exec(runvercmd.c_str());
|
||||
currentver = trim(currentver);
|
||||
|
||||
info << "Current version: " << currentver << std::endl;
|
||||
info << "Checking for updates..." << std::endl;
|
||||
|
||||
shared_commands::cLocalTempFolder local_temp_folder;
|
||||
std::filesystem::path sos_path = local_temp_folder.path() / "sos";
|
||||
|
||||
// Download sos to check metadata
|
||||
bool should_download = true;
|
||||
if (!download_file("https://getbin.xyz/sos", sos_path))
|
||||
{
|
||||
warning << "Failed to download sos utility, proceeding with direct download." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
chmod(sos_path.c_str(), 0755);
|
||||
|
||||
// Use sos to get metadata about the remote dropshell binary
|
||||
std::string info_cmd = sos_path.string() + " info getbin.xyz dropshell:latest-" + arch;
|
||||
std::string info_output = _exec(info_cmd.c_str());
|
||||
|
||||
// Parse the version from the info output
|
||||
// The metadata should contain version in format: "version: YYYY.MMDD.HHMM"
|
||||
std::size_t version_pos = info_output.find("version: ");
|
||||
if (version_pos != std::string::npos)
|
||||
{
|
||||
std::string remote_version = info_output.substr(version_pos + 9);
|
||||
remote_version = remote_version.substr(0, remote_version.find('\n'));
|
||||
remote_version = trim(remote_version);
|
||||
|
||||
info << "Latest available version: " << remote_version << std::endl;
|
||||
|
||||
// Compare versions (both in YYYY.MMDD.HHMM format)
|
||||
if (!remote_version.empty() && currentver >= remote_version)
|
||||
{
|
||||
info << "Dropshell is already up to date." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!remote_version.empty() && currentver < remote_version)
|
||||
{
|
||||
info << "New version available, downloading..." << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
debug << "Version information not found in metadata, proceeding with download." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (!should_download)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Download new version from getbin.xyz
|
||||
std::string url = "https://getbin.xyz/dropshell:latest-" + arch;
|
||||
std::filesystem::path temp_file = local_temp_folder.path() / "dropshell";
|
||||
|
||||
bool download_okay = download_file(url, temp_file);
|
||||
if (!download_okay)
|
||||
{
|
||||
error << "Failed to download new version of dropshell." << std::endl;
|
||||
error << "Failed to download new version of dropshell from " << url << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// make executable
|
||||
chmod(temp_file.c_str(), 0755);
|
||||
|
||||
// check if the new version is the same as the old version
|
||||
uint64_t new_hash = hash_file(temp_file);
|
||||
uint64_t old_hash = hash_file(exe_path);
|
||||
if (new_hash == old_hash)
|
||||
{
|
||||
info << "Confirmed dropshell is the latest version." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string runvercmd = exe_path.string() + " version";
|
||||
std::string currentver = _exec(runvercmd.c_str());
|
||||
// Get version of downloaded binary to double-check
|
||||
runvercmd = temp_file.string() + " version";
|
||||
std::string newver = _exec(runvercmd.c_str());
|
||||
newver = trim(newver);
|
||||
|
||||
// Final version check
|
||||
if (currentver >= newver)
|
||||
{
|
||||
info << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl;
|
||||
info << "Release version is not newer, no update needed." << std::endl;
|
||||
info << "Downloaded version (" << newver << ") is not newer than current (" << currentver << ")" << std::endl;
|
||||
info << "No update needed." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// move the new version to the old version.
|
||||
std::filesystem::rename(exe_path, exe_path.parent_path() / "dropshell.old");
|
||||
info << "Updating from version " << currentver << " to " << newver << std::endl;
|
||||
|
||||
// Backup old version and replace with new
|
||||
std::filesystem::path backup_path = exe_path.parent_path() / "dropshell.old";
|
||||
if (std::filesystem::exists(backup_path))
|
||||
{
|
||||
std::filesystem::remove(backup_path);
|
||||
}
|
||||
|
||||
std::filesystem::rename(exe_path, backup_path);
|
||||
std::filesystem::rename(temp_file, exe_path);
|
||||
|
||||
// remove the old version.
|
||||
std::filesystem::remove(exe_path.parent_path() / "dropshell.old");
|
||||
// Remove the backup after successful replacement
|
||||
std::filesystem::remove(backup_path);
|
||||
|
||||
// execute the new version
|
||||
execlp("bash", "bash", "-c", (exe_path.parent_path() / "dropshell").string() + "install", (char *)nullptr);
|
||||
info << "Update complete. Restarting dropshell..." << std::endl;
|
||||
|
||||
// Execute the new version
|
||||
execlp("bash", "bash", "-c", (exe_path.string() + " install").c_str(), (char *)nullptr);
|
||||
error << "Failed to execute new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user