cli: Begin 'tag' command
This commit is contained in:
parent
4fe745e858
commit
44b9986166
@ -15,6 +15,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
|||||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
commandtag.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
commandadd.cpp \
|
commandadd.cpp \
|
||||||
commanddelete.cpp \
|
commanddelete.cpp \
|
||||||
@ -27,6 +28,7 @@ HEADERS += \
|
|||||||
command.h \
|
command.h \
|
||||||
commandadd.h \
|
commandadd.h \
|
||||||
commanddelete.h \
|
commanddelete.h \
|
||||||
|
commandtag.h \
|
||||||
commandupdate.h \
|
commandupdate.h \
|
||||||
commandsearch.h \
|
commandsearch.h \
|
||||||
commandlist.h
|
commandlist.h
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "looqsgeneralexception.h"
|
|
||||||
|
|
||||||
void Command::execute()
|
void Command::execute()
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include "commandsearch.h"
|
#include "commandsearch.h"
|
||||||
#include "databasefactory.h"
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
int CommandSearch::handle(QStringList arguments)
|
int CommandSearch::handle(QStringList arguments)
|
||||||
|
65
cli/commandtag.cpp
Normal file
65
cli/commandtag.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include <QCommandLineParser>
|
||||||
|
#include "commandtag.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
int CommandTag::handle(QStringList arguments)
|
||||||
|
{
|
||||||
|
QCommandLineParser parser;
|
||||||
|
parser.addPositionalArgument("add", "Adds a tag to a file",
|
||||||
|
"add [tag] [paths...]. Adds the tag to the specified paths");
|
||||||
|
parser.addPositionalArgument("remove", "Removes a file associated to tag", "remove [tag] [file]");
|
||||||
|
parser.addPositionalArgument("delete", "Deletes a tag", "delete [tag]");
|
||||||
|
parser.addPositionalArgument("list", "Lists paths associated with a tag, or all tags", "list [tag]");
|
||||||
|
parser.addHelpOption();
|
||||||
|
|
||||||
|
parser.parse(arguments);
|
||||||
|
|
||||||
|
QStringList args = parser.positionalArguments();
|
||||||
|
if(args.length() == 0)
|
||||||
|
{
|
||||||
|
parser.showHelp(EXIT_FAILURE);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString cmd = args[0];
|
||||||
|
qDebug() << cmd;
|
||||||
|
if(cmd == "add")
|
||||||
|
{
|
||||||
|
if(args.length() < 3)
|
||||||
|
{
|
||||||
|
Logger::error() << "Not enough arguments provided. 'add' requires a tag followed by at least one path"
|
||||||
|
<< Qt::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
QString tag = args[1];
|
||||||
|
auto paths = args.mid(2).toVector();
|
||||||
|
for(int i = 0; i < paths.size(); i++)
|
||||||
|
{
|
||||||
|
QFileInfo info{paths[i]};
|
||||||
|
if(!info.exists())
|
||||||
|
{
|
||||||
|
Logger::error() << "Can't add tag for file " + info.absoluteFilePath() + " because it does not exist"
|
||||||
|
<< Qt::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
QString absolutePath = info.absoluteFilePath();
|
||||||
|
if(!this->dbService->fileExistsInDatabase(absolutePath))
|
||||||
|
{
|
||||||
|
Logger::error() << "Only files that have been indexed can be tagged. File not in index: " + absolutePath
|
||||||
|
<< Qt::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
paths[i] = absolutePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool result = this->dbService->addTag(tag, paths);
|
||||||
|
if(!result)
|
||||||
|
{
|
||||||
|
Logger::error() << "Failed to assign tags" << Qt::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
13
cli/commandtag.h
Normal file
13
cli/commandtag.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef COMMANDTAG_H
|
||||||
|
#define COMMANDTAG_H
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
class CommandTag : public Command
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Command::Command;
|
||||||
|
|
||||||
|
int handle(QStringList arguments) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMMANDTAG_H
|
@ -21,6 +21,7 @@
|
|||||||
#include "commandupdate.h"
|
#include "commandupdate.h"
|
||||||
#include "commandsearch.h"
|
#include "commandsearch.h"
|
||||||
#include "commandlist.h"
|
#include "commandlist.h"
|
||||||
|
#include "commandtag.h"
|
||||||
#include "databasefactory.h"
|
#include "databasefactory.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "sandboxedprocessor.h"
|
#include "sandboxedprocessor.h"
|
||||||
@ -31,7 +32,7 @@
|
|||||||
void printUsage(QString argv0)
|
void printUsage(QString argv0)
|
||||||
{
|
{
|
||||||
qInfo() << "Usage:" << argv0 << "command";
|
qInfo() << "Usage:" << argv0 << "command";
|
||||||
qInfo() << "Valid commands: add, update, delete, search, list. Each command has a --help option.";
|
qInfo() << "Valid commands: add, update, search, delete, tag, list. Each command has a --help option.";
|
||||||
}
|
}
|
||||||
|
|
||||||
Command *commandFromName(QString name, SqliteDbService &dbService)
|
Command *commandFromName(QString name, SqliteDbService &dbService)
|
||||||
@ -56,6 +57,10 @@ Command *commandFromName(QString name, SqliteDbService &dbService)
|
|||||||
{
|
{
|
||||||
return new CommandList(dbService);
|
return new CommandList(dbService);
|
||||||
}
|
}
|
||||||
|
if(name == "tag")
|
||||||
|
{
|
||||||
|
return new CommandTag(dbService);
|
||||||
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user