looqs/cli/commanddelete.cpp

131 sor
3.8 KiB
C++

2019-04-07 20:22:20 +02:00
#include <QCommandLineParser>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QRegularExpression>
#include <QSqlError>
#include "commanddelete.h"
#include "logger.h"
2019-04-07 20:22:20 +02:00
int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern)
{
int offset = 0;
int limit = 1000;
QVector<FileData> files;
int processedRows = this->dbService->getFiles(files, pattern, 0, limit);
while(processedRows > 0 )
2019-04-07 20:22:20 +02:00
{
for(FileData &file : files)
2019-04-07 20:22:20 +02:00
{
QFileInfo fileInfo(file.absPath);
if(!fileInfo.exists())
2019-04-07 20:22:20 +02:00
{
2019-04-29 10:24:50 +02:00
if(!dryRun)
2019-04-07 20:22:20 +02:00
{
2019-04-29 10:24:50 +02:00
if(this->dbService->deleteFile(file.absPath))
{
2019-04-29 10:24:50 +02:00
if(verbose)
{
Logger::info() << "Deleted" << file.absPath << endl;
}
}
else
{
Logger::error()<< "Failed to delete:" << file.absPath << ", exiting." << endl
;
return 1;
}
2019-04-07 20:22:20 +02:00
}
else
2019-04-07 20:22:20 +02:00
{
2019-04-29 10:24:50 +02:00
Logger::info() << "Would delete " << file.absPath << endl;
2019-04-07 20:22:20 +02:00
}
}
}
2019-04-29 10:24:50 +02:00
offset += limit;
2019-04-29 10:24:50 +02:00
processedRows = this->dbService->getFiles(files, pattern, offset, limit);
2019-04-07 20:22:20 +02:00
}
return 0;
}
2019-04-07 20:22:20 +02:00
int CommandDelete::removePaths(const QStringList &paths, bool verbose, bool dryRun)
{
int result = 0;
for(const QString &file : paths)
2019-04-07 20:22:20 +02:00
{
QFileInfo fileInfo(file);
QString absPath = fileInfo.absoluteFilePath();
if(this->dbService->fileExistsInDatabase(absPath))
2019-04-07 20:22:20 +02:00
{
if(!dryRun)
2019-04-07 20:22:20 +02:00
{
if(this->dbService->deleteFile(absPath))
{
if(verbose)
{
Logger::info() << "Deleted" << absPath << endl;
}
}
else
2019-04-07 20:22:20 +02:00
{
Logger::error()<< "Failed to delete:" << absPath << endl;
result = 1;
2019-04-07 20:22:20 +02:00
}
}
2019-04-07 20:22:20 +02:00
}
else
{
Logger::error() << "No such file in database:" << absPath << endl;
result = 1;
}
}
return result;
}
int CommandDelete::handle(QStringList arguments)
{
QCommandLineParser parser;
parser.addOptions({
{ { "v", "verbose" }, "Print path of the files while deleting them" },
{ { "n", "dry-run"}, "Only print which files would be deleted from the database, don't delete them"},
{ "pattern", "Only delete files from index matching the pattern, e. g. */.git/*. Only applies to --deleted or standalone.", "pattern" },
{ "deleted", "Delete all files from the index that don't exist anymore" }
});
parser.addHelpOption();
parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
parser.process(arguments);
bool verbose = parser.isSet("verbose");
bool dryRun = parser.isSet("dry-run");
QString pattern = parser.value("pattern");
if(parser.isSet("deleted"))
{
int result = removeNonExistent(verbose, dryRun, pattern);
if(result != 0)
{
return result;
2019-04-07 20:22:20 +02:00
}
}
2019-04-07 20:22:20 +02:00
QStringList files = parser.positionalArguments();
if(files.length() > 0)
{
int result = removePaths(files, verbose, dryRun);
if(result != 0)
{
return result;
}
2019-04-07 20:22:20 +02:00
}
2019-04-07 20:22:20 +02:00
return 0;
}