repoindex/main.cpp

108 lines
4.4 KiB
C++
Raw Normal View History

2015-09-06 15:34:39 +02:00
#include "repoindex/alpm/manager.h"
#include "repoindex/alpm/utilities.h"
#include "repoindex/alpm/config.h"
#include "repoindex/alpm/resolvebuildorder.h"
#include "repoindex/alpm/mingwbundle.h"
2015-09-04 14:37:01 +02:00
2015-09-06 15:34:39 +02:00
#include "repoindex/network/server.h"
2015-08-10 22:46:01 +02:00
#include <c++utilities/application/argumentparser.h>
#include <c++utilities/conversion/stringconversion.h>
2015-08-10 22:46:01 +02:00
#include <c++utilities/application/failure.h>
#include <QCoreApplication>
#include <iostream>
#include <algorithm>
2015-08-10 22:46:01 +02:00
using namespace std;
using namespace ApplicationUtilities;
using namespace RepoIndex;
2015-08-10 22:46:01 +02:00
int main(int argc, char *argv[])
{
// check whether shell syntax is enabled
for(char **arg = argv, **end = argv + argc; arg != end; ++arg) {
if(strcmp(*arg, "--sh-syntax") == 0) {
useShSyntax = true;
shchar = "# ";
break;
}
}
2015-08-10 22:46:01 +02:00
// setup the argument parser
ArgumentParser parser;
ConfigArgs configArgs(parser);
parser.setIgnoreUnknownArguments(false);
// parse command line arguments
try {
parser.parseArgs(argc, argv);
} catch (Failure &e) {
cerr << shchar << "Unable to parse arguments: " << e.what() << endl;
2015-08-10 22:46:01 +02:00
return 2;
}
try {
// load configuration
Config config;
config.loadFromConfigFile(configArgs);
config.loadFromArgs(configArgs);
if(find_if(parser.mainArguments().cbegin(), parser.mainArguments().cend(), [&configArgs] (const Argument *arg) {
return arg != &configArgs.helpArg && arg->isPresent();
}) != parser.mainArguments().cend()) {
// create app
2015-08-10 22:46:01 +02:00
QCoreApplication application(argc, argv);
// setup ALPM
Manager manager(config);
manager.applyPacmanConfig();
manager.applyRepoIndexConfig();
cerr << shchar << "Loading databases ..." << endl;
manager.initAlpmDataBases();
if(configArgs.serverArg.isPresent()) {
// setup the server
Server server(manager, manager.config());
QObject::connect(&server, &Server::closed, &application, &QCoreApplication::quit);
// run Qt loop
return application.exec();
} else if(configArgs.buildOrderArg.isPresent()) {
BuildOrderResolver resolver(manager);
const QStringList results = resolver.resolve(configArgs.buildOrderArg.values());
// print results
if(useShSyntax) {
cout << "export REPOINDEX_RESULTS=(";
for(const auto &pkgName : results) {
cout << ' ' << '\'' << pkgName.toLocal8Bit().data() << '\'';
}
cout << ' ' << ')' << endl;
} else {
cout << "Results: ";
for(const auto &pkgName : results) {
cout << pkgName.toLocal8Bit().data() << ' ';
}
cout << endl;
}
} else if(configArgs.mingwBundleArg.isPresent()) {
2015-09-04 14:37:01 +02:00
MingwBundle bundle(manager, configArgs.mingwBundleArg.values(), configArgs.iconThemesArg.values());
bundle.createBundle(configArgs.targetDirArg.isPresent() ? configArgs.targetDirArg.values().front() : string("."),
configArgs.targetNameArg.values().front(),
configArgs.targetFormatArg.isPresent() ? configArgs.targetFormatArg.values().front() : string("zip"));
} else if(configArgs.upgradeLookupArg.isPresent()) {
cerr << shchar << "TODO" << endl;
}
} else if(!configArgs.helpArg.isPresent()) {
if(useShSyntax) {
cerr << "export REPOINDEX_ERROR='No command line arguments specified. See --help for available commands.'" << endl;
} else {
cerr << "No command line arguments specified. See --help for available commands." << endl;
}
2015-08-10 22:46:01 +02:00
}
} catch (std::exception &e) {
if(useShSyntax) {
string error = e.what();
ConversionUtilities::findAndReplace<string>(error, "'", "\'");
cerr << "export REPOINDEX_ERROR='" << error << '\'' << endl;
} else {
cerr << shchar << "Error: " << e.what() << endl;
}
2015-08-10 22:46:01 +02:00
return 1;
}
}