mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-08-09 23:12:23 -04:00
CLI: Add group commands
This commit is contained in:
parent
964478e78f
commit
19f87ca057
12 changed files with 1066 additions and 497 deletions
80
src/cli/AddGroup.cpp
Normal file
80
src/cli/AddGroup.cpp
Normal file
|
@ -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
src/cli/AddGroup.h
Normal file
32
src/cli/AddGroup.h
Normal file
|
@ -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
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
set(cli_SOURCES
|
||||
Add.cpp
|
||||
AddGroup.cpp
|
||||
Analyze.cpp
|
||||
Clip.cpp
|
||||
Create.cpp
|
||||
|
@ -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})
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "Command.h"
|
||||
|
||||
#include "Add.h"
|
||||
#include "AddGroup.h"
|
||||
#include "Analyze.h"
|
||||
#include "Clip.h"
|
||||
#include "Create.h"
|
||||
|
@ -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"
|
||||
|
@ -125,7 +128,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());
|
||||
}
|
||||
}
|
||||
|
|
81
src/cli/Move.cpp
Normal file
81
src/cli/Move.cpp
Normal file
|
@ -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
src/cli/Move.h
Normal file
32
src/cli/Move.h
Normal file
|
@ -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
src/cli/RemoveGroup.cpp
Normal file
85
src/cli/RemoveGroup.cpp
Normal file
|
@ -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;
|
||||
}
|
32
src/cli/RemoveGroup.h
Normal file
32
src/cli/RemoveGroup.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (C) 2017 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_REMOVEGROUP_H
|
||||
#define KEEPASSXC_REMOVEGROUP_H
|
||||
|
||||
#include "DatabaseCommand.h"
|
||||
|
||||
class RemoveGroup : public DatabaseCommand
|
||||
{
|
||||
public:
|
||||
RemoveGroup();
|
||||
~RemoveGroup();
|
||||
|
||||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser);
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_REMOVEGROUP_H
|
|
@ -51,9 +51,18 @@ Lists the contents of a group in a database. If no group is specified, it will d
|
|||
.IP "merge [options] <database1> <database2>"
|
||||
Merges two databases together. The first database file is going to be replaced by the result of the merge, for that reason it is advisable to keep a backup of the two database files before attempting a merge. In the case that both databases make use of the same credentials, the \fI--same-credentials\fP or \fI-s\fP option can be used.
|
||||
|
||||
.IP "mkdir [options] <database> <group>"
|
||||
Adds a new group to a database.
|
||||
|
||||
.IP "mv [options] <database> <entry> <group>"
|
||||
Moves an entry to a new group.
|
||||
|
||||
.IP "rm [options] <database> <entry>"
|
||||
Removes an entry from a database. If the database has a recycle bin, the entry will be moved there. If the entry is already in the recycle bin, it will be removed permanently.
|
||||
|
||||
.IP "rmdir [options] <database> <group>"
|
||||
Removes a group from a database. If the database has a recycle bin, the group will be moved there. If the group is already in the recycle bin, it will be removed permanently.
|
||||
|
||||
.IP "show [options] <database> <entry>"
|
||||
Shows the title, username, password, URL and notes of a database entry. Can also show the current TOTP. Regarding the occurrence of multiple entries with the same name in different groups, everything stated in the \fIclip\fP command section also applies here.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue