112 lines
3.0 KiB
C++
Raw Normal View History

2026-02-24 20:57:59 +04:00
#include "logos_manager.h"
#include <QCoreApplication>
#include <QDir>
2026-02-26 14:54:44 -03:00
#include <QEventLoop>
#include <QTimer>
2026-02-24 20:57:59 +04:00
#include <iostream>
2026-02-26 14:54:44 -03:00
#define DEFAULT_TIMEOUT 10000
int app_main(LogosModules* modules, int argc, char* argv[]);
2026-02-24 20:57:59 +04:00
2026-02-26 14:54:44 -03:00
bool await(QEventLoop* loop, int timeoutMs) {
QTimer::singleShot(timeoutMs, loop, [loop]() {
std::cerr << "Call timed out." << std::endl;
loop->exit(1);
});
return loop->exec() == 0;
}
void notify(QEventLoop* loop, bool successValue) {
loop->exit(successValue ? 0 : 1);
}
2026-02-24 20:57:59 +04:00
int main(int argc, char* argv[]) {
2026-02-26 11:55:31 -03:00
if (argc < 2) {
2026-02-26 14:54:44 -03:00
std::cerr << "Usage: " << argv[0] << " <file path>" << std::endl;
2026-02-26 11:55:31 -03:00
return 1;
}
2026-02-24 20:57:59 +04:00
QCoreApplication app(argc, argv);
QCoreApplication::setOrganizationName("Logos");
QCoreApplication::setApplicationName("LogosStorageCLI");
Logos logos(QCoreApplication::applicationDirPath() + "/../modules");
2026-02-24 20:57:59 +04:00
if (!logos.init()) {
std::cerr << "Failed to initialize Logos" << std::endl;
return 1;
}
int result = app_main(logos.modules(), argc, argv);
2026-02-24 21:04:18 +04:00
logos.cleanup();
return result;
}
2026-02-24 21:04:18 +04:00
int app_main(LogosModules* modules, int argc, char* argv[]) {
2026-02-26 14:54:44 -03:00
2026-02-26 11:55:31 -03:00
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file>" << std::endl;
return 1;
}
const QString jsonConfig = "{"
"\"listen-addrs\": [\"/ip4/0.0.0.0/tcp/8000\"],"
"\"disc-port\": 9000,"
"\"nat\": \"none\""
"}";
if (!modules->storage_module.init(jsonConfig)) {
std::cerr << "Failed to initialize storage module" << std::endl;
return 1;
}
2026-02-26 14:54:44 -03:00
{
QEventLoop loop;
modules->storage_module.on("storageStart", [&loop](const QVariantList& data) {
bool success = data[0].toBool();
if (!success) {
std::cerr << "Failed to start storage module: " << data[1].toString().toStdString() << std::endl;
}
notify(&loop, success);
});
modules->storage_module.start();
if (!await(&loop, DEFAULT_TIMEOUT)) {
return 1;
}
}
{
QEventLoop loop;
// Uploads file.
modules->storage_module.on("storageUploadDone", [&loop](const QVariantList& data) {
bool success = data[0].toBool();
if (!success) {
std::cerr << "Failed to upload file: " << data[2].toString().toStdString() << std::endl;
notify(&loop, false);
return;
}
std::cout << "CID: " << data[2].toString().toStdString() << std::endl;
notify(&loop, true);
});
QUrl url = QUrl::fromLocalFile(argv[1]);
LogosResult result = modules->storage_module.uploadUrl(url);
if (!result.success) {
std::cerr << "Failed to upload file: " << result.getValue<QString>().toStdString() << std::endl;
return 1;
}
if (!await(&loop, DEFAULT_TIMEOUT)) {
return 1;
}
}
std::cout << "Upload completed successfully. Type CTRL+C to exit." << std::endl;
return QCoreApplication::exec();
2026-02-26 11:55:31 -03:00
}