Skip to content

Commit

Permalink
CLI: Add group commands
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored and louib committed Sep 22, 2019
1 parent 77fcde8 commit 68674da
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
- CLI: Add password generation options to `Add` and `Edit` commands [#3275](https://github.com/keepassxreboot/keepassxc/issues/3275)
- CLI: Add CSV export to the 'export' command [#3277]
- Add 'Monospaced font' option to the Notes field [#3321](https://github.com/keepassxreboot/keepassxc/issues/3321)
- CLI: Add group commands (mv, mkdir and rmdir) [#3313].

### Changed

Expand Down
80 changes: 80 additions & 0 deletions src/cli/AddGroup.cpp
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstdlib>
#include <stdio.h>

#include "AddGroup.h"

#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"

AddGroup::AddGroup()
{
name = QString("mkdir");
description = QObject::tr("Adds a new group to a database.");
positionalArguments.append({QString("group"), QObject::tr("Path of the group to add."), QString("")});
}

AddGroup::~AddGroup()
{
}

int AddGroup::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

const QStringList args = parser->positionalArguments();
const QString& databasePath = args.at(0);
const QString& groupPath = args.at(1);

QStringList pathParts = groupPath.split("/");
QString groupName = pathParts.takeLast();
QString parentGroupPath = pathParts.join("/");

Group* group = database->rootGroup()->findGroupByPath(groupPath);
if (group) {
errorTextStream << QObject::tr("Group %1 already exists!").arg(groupPath) << endl;
return EXIT_FAILURE;
}

Group* parentGroup = database->rootGroup()->findGroupByPath(parentGroupPath);
if (!parentGroup) {
errorTextStream << QObject::tr("Group %1 not found.").arg(parentGroupPath) << endl;
return EXIT_FAILURE;
}

Group* newGroup = new Group();
newGroup->setUuid(QUuid::createUuid());
newGroup->setName(groupName);
newGroup->setParent(parentGroup);

QString errorMessage;
if (!database->save(databasePath, &errorMessage, true, false)) {
errorTextStream << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
return EXIT_FAILURE;
}

if (!parser->isSet(Command::QuietOption)) {
outputTextStream << QObject::tr("Successfully added group %1.").arg(groupName) << endl;
}
return EXIT_SUCCESS;
}
32 changes: 32 additions & 0 deletions src/cli/AddGroup.h
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSXC_ADDGROUP_H
#define KEEPASSXC_ADDGROUP_H

#include "DatabaseCommand.h"

class AddGroup : public DatabaseCommand
{
public:
AddGroup();
~AddGroup();

int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser);
};

#endif // KEEPASSXC_ADDGROUP_H
3 changes: 3 additions & 0 deletions src/cli/CMakeLists.txt
Expand Up @@ -15,6 +15,7 @@

set(cli_SOURCES
Add.cpp
AddGroup.cpp
Analyze.cpp
Clip.cpp
Create.cpp
Expand All @@ -28,7 +29,9 @@ set(cli_SOURCES
List.cpp
Locate.cpp
Merge.cpp
Move.cpp
Remove.cpp
RemoveGroup.cpp
Show.cpp)

add_library(cli STATIC ${cli_SOURCES})
Expand Down
6 changes: 6 additions & 0 deletions src/cli/Command.cpp
Expand Up @@ -23,6 +23,7 @@
#include "Command.h"

#include "Add.h"
#include "AddGroup.h"
#include "Analyze.h"
#include "Clip.h"
#include "Create.h"
Expand All @@ -34,7 +35,9 @@
#include "List.h"
#include "Locate.h"
#include "Merge.h"
#include "Move.h"
#include "Remove.h"
#include "RemoveGroup.h"
#include "Show.h"
#include "TextStream.h"
#include "Utils.h"
Expand Down Expand Up @@ -119,7 +122,10 @@ void populateCommands()
commands.insert(QString("locate"), new Locate());
commands.insert(QString("ls"), new List());
commands.insert(QString("merge"), new Merge());
commands.insert(QString("mkdir"), new AddGroup());
commands.insert(QString("mv"), new Move());
commands.insert(QString("rm"), new Remove());
commands.insert(QString("rmdir"), new RemoveGroup());
commands.insert(QString("show"), new Show());
}
}
Expand Down
81 changes: 81 additions & 0 deletions src/cli/Move.cpp
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstdlib>
#include <stdio.h>

#include "Move.h"

#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"

Move::Move()
{
name = QString("mv");
description = QObject::tr("Moves an entry to a new group.");
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to move."), QString("")});
positionalArguments.append({QString("group"), QObject::tr("Path of the destination group."), QString("")});
}

Move::~Move()
{
}

int Move::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

const QStringList args = parser->positionalArguments();
const QString& databasePath = args.at(0);
const QString& entryPath = args.at(1);
const QString& destinationPath = args.at(2);

Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
errorTextStream << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
return EXIT_FAILURE;
}

Group* destinationGroup = database->rootGroup()->findGroupByPath(destinationPath);
if (!destinationGroup) {
errorTextStream << QObject::tr("Could not find group with path %1.").arg(destinationPath) << endl;
return EXIT_FAILURE;
}

if (destinationGroup == entry->parent()) {
errorTextStream << QObject::tr("Entry is already in group %1.").arg(destinationPath) << endl;
return EXIT_FAILURE;
}

entry->beginUpdate();
entry->setGroup(destinationGroup);
entry->endUpdate();

QString errorMessage;
if (!database->save(databasePath, &errorMessage, true, false)) {
errorTextStream << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
return EXIT_FAILURE;
}

outputTextStream << QObject::tr("Successfully moved entry %1 to group %2.").arg(entry->title(), destinationPath)
<< endl;
return EXIT_SUCCESS;
}
32 changes: 32 additions & 0 deletions src/cli/Move.h
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSXC_MOVE_H
#define KEEPASSXC_MOVE_H

#include "DatabaseCommand.h"

class Move : public DatabaseCommand
{
public:
Move();
~Move();

int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser);
};

#endif // KEEPASSXC_MOVE_H
85 changes: 85 additions & 0 deletions src/cli/RemoveGroup.cpp
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstdlib>
#include <stdio.h>

#include "RemoveGroup.h"

#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"

RemoveGroup::RemoveGroup()
{
name = QString("rmdir");
description = QString("Removes a group from a database.");
positionalArguments.append({QString("group"), QObject::tr("Path of the group to remove."), QString("")});
}

RemoveGroup::~RemoveGroup()
{
}

int RemoveGroup::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
bool quiet = parser->isSet(Command::QuietOption);
QString databasePath = parser->positionalArguments().at(0);
QString groupPath = parser->positionalArguments().at(1);

TextStream outputTextStream(quiet ? Utils::DEVNULL : Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

// Recursive option means were looking for a group to remove.
QPointer<Group> group = database->rootGroup()->findGroupByPath(groupPath);
if (!group) {
errorTextStream << QObject::tr("Group %1 not found.").arg(groupPath) << endl;
return EXIT_FAILURE;
}

if (group == database->rootGroup()) {
errorTextStream << QObject::tr("Cannot remove root group from database.") << endl;
return EXIT_FAILURE;
}

bool recycled = true;
auto* recycleBin = database->metadata()->recycleBin();
if (!database->metadata()->recycleBinEnabled() || (recycleBin && recycleBin->findGroupByUuid(group->uuid()))) {
delete group;
recycled = false;
} else {
database->recycleGroup(group);
};

QString errorMessage;
if (!database->save(databasePath, &errorMessage, true, false)) {
errorTextStream << QObject::tr("Unable to save database to file: %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}

if (recycled) {
outputTextStream << QObject::tr("Successfully recycled group %1.").arg(groupPath) << endl;
} else {
outputTextStream << QObject::tr("Successfully deleted group %1.").arg(groupPath) << endl;
}

return EXIT_SUCCESS;
}

0 comments on commit 68674da

Please sign in to comment.