test: Update 6 files
This commit is contained in:
@@ -22,12 +22,13 @@ Usage: dehydrate [OPTIONS] SOURCE DEST
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
-s Silent mode (no output)
|
-s Silent mode (no output)
|
||||||
-u Update dehydrate to the latest version
|
-v Show version only
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/
|
dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/
|
||||||
dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/
|
dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/
|
||||||
dehydrate -u Updates dehydrate to the latest version
|
dehydrate -v Shows version number
|
||||||
|
dehydrate version Shows version number
|
||||||
```
|
```
|
||||||
|
|
||||||
All c++ code produced is in namespace `recreate_{SOURCEFILE|SOURCEFOLDER}`
|
All c++ code produced is in namespace `recreate_{SOURCEFILE|SOURCEFOLDER}`
|
||||||
|
@@ -9,13 +9,11 @@ Usage: dehydrate [OPTIONS] SOURCE DEST
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
-s Silent mode (no output)
|
-s Silent mode (no output)
|
||||||
-u Update dehydrate to the latest version
|
|
||||||
-v Show version only
|
-v Show version only
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/
|
dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/
|
||||||
dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/
|
dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/
|
||||||
dehydrate -u Updates dehydrate to the latest version
|
|
||||||
dehydrate -v Shows version number
|
dehydrate -v Shows version number
|
||||||
dehydrate version Shows version number
|
dehydrate version Shows version number
|
||||||
)";
|
)";
|
||||||
@@ -36,8 +34,6 @@ Args parse_args(int argc, char* argv[]) {
|
|||||||
|
|
||||||
if (flag == "-s") {
|
if (flag == "-s") {
|
||||||
args.silent = true;
|
args.silent = true;
|
||||||
} else if (flag == "-u") {
|
|
||||||
args.update = true;
|
|
||||||
} else if (flag == "-v") {
|
} else if (flag == "-v") {
|
||||||
args.version = true;
|
args.version = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -47,8 +43,8 @@ Args parse_args(int argc, char* argv[]) {
|
|||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If update or version flag is set, return early
|
// If version flag is set, return early
|
||||||
if (args.update || args.version) {
|
if (args.version) {
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
struct Args {
|
struct Args {
|
||||||
bool silent = false;
|
bool silent = false;
|
||||||
bool update = false;
|
|
||||||
bool version = false;
|
bool version = false;
|
||||||
std::string source;
|
std::string source;
|
||||||
std::string dest;
|
std::string dest;
|
||||||
|
53
src/main.cpp
53
src/main.cpp
@@ -1,58 +1,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "argparse.hpp"
|
#include "argparse.hpp"
|
||||||
#include "generator.hpp"
|
#include "generator.hpp"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
|
|
||||||
|
|
||||||
std::string get_arch()
|
|
||||||
{
|
|
||||||
// determine the architecture of the system
|
|
||||||
std::string arch;
|
|
||||||
#ifdef __aarch64__
|
|
||||||
arch = "arm64";
|
|
||||||
#elif __x86_64__
|
|
||||||
arch = "amd64";
|
|
||||||
#endif
|
|
||||||
return arch;
|
|
||||||
}
|
|
||||||
|
|
||||||
int update()
|
|
||||||
{
|
|
||||||
// determine path to this executable
|
|
||||||
std::filesystem::path exepath = std::filesystem::canonical("/proc/self/exe");
|
|
||||||
std::filesystem::path parent_path = exepath.parent_path();
|
|
||||||
std::string project_name = exepath.filename().string();
|
|
||||||
|
|
||||||
// determine the architecture of the system
|
|
||||||
std::string arch = get_arch();
|
|
||||||
|
|
||||||
std::string url = "https://gitea.jde.nz/public/"+project_name+"/releases/download/latest/"+project_name+"." + arch;
|
|
||||||
|
|
||||||
// download new version, preserve permissions and ownership
|
|
||||||
std::string bash_script;
|
|
||||||
bash_script += "docker run --rm -v "+parent_path.string()+":/target";
|
|
||||||
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
|
||||||
bash_script += " sh -c \"";
|
|
||||||
bash_script += " curl -fsSL " + url + " -o /target/"+project_name+"_temp &&";
|
|
||||||
bash_script += " chmod --reference=/target/"+project_name+" /target/"+project_name+"_temp &&";
|
|
||||||
bash_script += " chown --reference=/target/"+project_name+" /target/"+project_name+"_temp &&";
|
|
||||||
bash_script += " mv /target/"+project_name+"_temp /target/"+project_name;
|
|
||||||
bash_script += "\"";
|
|
||||||
|
|
||||||
std::cout << "Updating " << exepath << " to the latest " << arch << " version." << std::endl;
|
|
||||||
|
|
||||||
// std::cout << "bash_script: " << std::endl
|
|
||||||
// << bash_script << std::endl;
|
|
||||||
|
|
||||||
// run the bash script
|
|
||||||
execlp("bash", "bash", "-c", bash_script.c_str(), (char *)nullptr);
|
|
||||||
std::cerr << "Failed to execute command." << std::endl;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@@ -65,12 +18,6 @@ int main(int argc, char* argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle update request
|
|
||||||
if (args.update) {
|
|
||||||
std::cout << "Dehydrate version " << VERSION << std::endl;
|
|
||||||
return update();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show version for normal operations (unless silent)
|
// Show version for normal operations (unless silent)
|
||||||
if (!args.silent) {
|
if (!args.silent) {
|
||||||
std::cout << "Dehydrate version " << VERSION << std::endl;
|
std::cout << "Dehydrate version " << VERSION << std::endl;
|
||||||
|
@@ -107,6 +107,21 @@ static bool _recreate_file_(const std::filesystem::path& outpath, uint64_t file_
|
|||||||
bool recreate_tree(std::string destination_folder) {
|
bool recreate_tree(std::string destination_folder) {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
bool any_written = false;
|
bool any_written = false;
|
||||||
|
{
|
||||||
|
// File: subdir/nested.txt
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "subdir/nested.txt";
|
||||||
|
static const char filedata_base64[] = "VGhpcyBmaWxlIGlzIGluIGEgc3ViZGlyZWN0b3J5Lg==";
|
||||||
|
|
||||||
|
// Decode Base64 data
|
||||||
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
|
unsigned char* decoded_data = new unsigned char[decoded_size];
|
||||||
|
size_t actual_size;
|
||||||
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
|
bool file_written = _recreate_file_(outpath, 14153000318456068100ULL, std::filesystem::perms(256), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
{
|
{
|
||||||
// File: small.txt
|
// File: small.txt
|
||||||
fs::path outpath = fs::path(destination_folder) / "small.txt";
|
fs::path outpath = fs::path(destination_folder) / "small.txt";
|
||||||
@@ -137,21 +152,6 @@ bool recreate_tree(std::string destination_folder) {
|
|||||||
delete[] decoded_data;
|
delete[] decoded_data;
|
||||||
any_written = any_written || file_written;
|
any_written = any_written || file_written;
|
||||||
}
|
}
|
||||||
{
|
|
||||||
// File: test3.sh
|
|
||||||
fs::path outpath = fs::path(destination_folder) / "test3.sh";
|
|
||||||
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKZWNobyAnSGVsbG8gZnJvbSB0ZXN0IHNjcmlwdCcKZXhpdCAw";
|
|
||||||
|
|
||||||
// Decode Base64 data
|
|
||||||
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
|
||||||
unsigned char* decoded_data = new unsigned char[decoded_size];
|
|
||||||
size_t actual_size;
|
|
||||||
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
|
||||||
|
|
||||||
bool file_written = _recreate_file_(outpath, 14335927320996074478ULL, std::filesystem::perms(488), decoded_data, actual_size);
|
|
||||||
delete[] decoded_data;
|
|
||||||
any_written = any_written || file_written;
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
// File: test1.txt
|
// File: test1.txt
|
||||||
fs::path outpath = fs::path(destination_folder) / "test1.txt";
|
fs::path outpath = fs::path(destination_folder) / "test1.txt";
|
||||||
@@ -169,9 +169,9 @@ bool recreate_tree(std::string destination_folder) {
|
|||||||
any_written = any_written || file_written;
|
any_written = any_written || file_written;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// File: subdir/nested.txt
|
// File: test3.sh
|
||||||
fs::path outpath = fs::path(destination_folder) / "subdir/nested.txt";
|
fs::path outpath = fs::path(destination_folder) / "test3.sh";
|
||||||
static const char filedata_base64[] = "VGhpcyBmaWxlIGlzIGluIGEgc3ViZGlyZWN0b3J5Lg==";
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKZWNobyAnSGVsbG8gZnJvbSB0ZXN0IHNjcmlwdCcKZXhpdCAw";
|
||||||
|
|
||||||
// Decode Base64 data
|
// Decode Base64 data
|
||||||
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
@@ -179,7 +179,7 @@ bool recreate_tree(std::string destination_folder) {
|
|||||||
size_t actual_size;
|
size_t actual_size;
|
||||||
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
bool file_written = _recreate_file_(outpath, 14153000318456068100ULL, std::filesystem::perms(256), decoded_data, actual_size);
|
bool file_written = _recreate_file_(outpath, 14335927320996074478ULL, std::filesystem::perms(488), decoded_data, actual_size);
|
||||||
delete[] decoded_data;
|
delete[] decoded_data;
|
||||||
any_written = any_written || file_written;
|
any_written = any_written || file_written;
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user