2023-04-02 21:39:50 +02:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include "commandtag.h"
|
|
|
|
#include "logger.h"
|
2023-04-10 18:28:41 +02:00
|
|
|
#include "tagmanager.h"
|
2023-04-02 21:39:50 +02:00
|
|
|
|
2023-05-07 16:51:42 +02:00
|
|
|
bool CommandTag::ensureAbsolutePaths(const QVector<QString> &paths, QVector<QString> &absolutePaths)
|
|
|
|
{
|
|
|
|
for(const QString &path : paths)
|
|
|
|
{
|
|
|
|
QFileInfo info{path};
|
|
|
|
if(!info.exists())
|
|
|
|
{
|
|
|
|
Logger::error() << "Can't add tag for file " + info.absoluteFilePath() + " because it does not exist"
|
|
|
|
<< Qt::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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 false;
|
|
|
|
}
|
|
|
|
absolutePaths.append(absolutePath);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-02 21:39:50 +02:00
|
|
|
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");
|
2023-05-07 16:51:42 +02:00
|
|
|
parser.addPositionalArgument("remove", "Removes a path associated to a tag", "remove [tag] [path]");
|
2023-04-02 21:39:50 +02:00
|
|
|
parser.addPositionalArgument("delete", "Deletes a tag", "delete [tag]");
|
|
|
|
parser.addPositionalArgument("list", "Lists paths associated with a tag, or all tags", "list [tag]");
|
2023-05-07 16:51:42 +02:00
|
|
|
parser.addPositionalArgument("show", "Lists tags associated with a path", "show [path]");
|
|
|
|
|
2023-04-02 21:39:50 +02:00
|
|
|
parser.addHelpOption();
|
|
|
|
|
|
|
|
parser.parse(arguments);
|
|
|
|
|
|
|
|
QStringList args = parser.positionalArguments();
|
|
|
|
if(args.length() == 0)
|
|
|
|
{
|
|
|
|
parser.showHelp(EXIT_FAILURE);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2023-05-07 16:51:42 +02:00
|
|
|
TagManager tagManager{*this->dbService};
|
2023-04-02 21:39:50 +02:00
|
|
|
QString cmd = args[0];
|
|
|
|
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];
|
2023-05-07 16:51:42 +02:00
|
|
|
QVector<QString> paths = args.mid(2).toVector();
|
2023-04-02 21:39:50 +02:00
|
|
|
|
2023-05-07 16:51:42 +02:00
|
|
|
QVector<QString> absolutePaths;
|
|
|
|
if(!ensureAbsolutePaths(paths, absolutePaths))
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
bool result = tagManager.addPathsToTag(tag, absolutePaths);
|
2023-04-02 21:39:50 +02:00
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
Logger::error() << "Failed to assign tags" << Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2023-05-07 16:51:42 +02:00
|
|
|
if(cmd == "list")
|
|
|
|
{
|
2023-04-02 21:39:50 +02:00
|
|
|
|
2023-05-07 16:51:42 +02:00
|
|
|
QString tag;
|
|
|
|
if(args.length() >= 2)
|
|
|
|
{
|
|
|
|
tag = args[1];
|
|
|
|
}
|
|
|
|
QVector<QString> entries;
|
|
|
|
if(tag.isEmpty())
|
|
|
|
{
|
|
|
|
entries = tagManager.getTags();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entries = tagManager.getPaths(tag);
|
|
|
|
}
|
|
|
|
for(const QString &entry : entries)
|
|
|
|
{
|
|
|
|
Logger::info() << entry << Qt::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cmd == "remove")
|
|
|
|
{
|
|
|
|
if(args.length() < 3)
|
|
|
|
{
|
|
|
|
Logger::error() << "Not enough arguments provided. 'remove' requires a tag followed by at least one path"
|
|
|
|
<< Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
QString tag = args[1];
|
|
|
|
QVector<QString> paths = args.mid(2).toVector();
|
|
|
|
|
|
|
|
QVector<QString> absolutePaths;
|
|
|
|
if(!ensureAbsolutePaths(paths, absolutePaths))
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!tagManager.removePathsForTag(tag, absolutePaths))
|
|
|
|
{
|
|
|
|
Logger::error() << "Failed to remove path assignments" << Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cmd == "delete")
|
|
|
|
{
|
|
|
|
if(args.length() != 2)
|
|
|
|
{
|
|
|
|
Logger::error() << "The 'delete' command requires the tag to delete" << Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if(!tagManager.deleteTag(args[1]))
|
|
|
|
{
|
|
|
|
Logger::error() << "Failed to delete tag" << Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cmd == "show")
|
|
|
|
{
|
|
|
|
if(args.length() != 2)
|
|
|
|
{
|
|
|
|
Logger::error() << "The 'show' command requires a path to show the assigned tags" << Qt::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
QString path = args[1];
|
|
|
|
QVector<QString> absolutePaths;
|
|
|
|
if(!ensureAbsolutePaths({path}, absolutePaths))
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
QVector<QString> tags = tagManager.getTags(absolutePaths.at(0));
|
|
|
|
for(const QString &entry : tags)
|
|
|
|
{
|
|
|
|
Logger::info() << entry << Qt::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
2023-04-02 21:39:50 +02:00
|
|
|
}
|