CLI: Add interactive session mode command open

This change adds a GNU Readline-based interactive mode to keepassxc-cli. If GNU Readline is not available, commands are just read from stdin with no editing or auto-complete support.

DatabaseCommand is modified to add the path to the current database to the arguments passed to executeWithDatabase. In this way, instances of DatabaseCommand do not have to prompt to re-open the database after each invocation, and existing command implementations do not have to be changed to support interactive mode.

This change also introduces a new way of handling commands between interactive and batch modes.

* Fixes #3224.
* Ran make format
This commit is contained in:
James Ring 2019-09-13 09:49:03 -04:00 committed by Jonathan White
parent a07ea12ac4
commit b1eda37cca
27 changed files with 2751 additions and 2015 deletions

View file

@ -1,152 +1,195 @@
/*
* 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 <QMap>
#include "Command.h"
#include "Add.h"
#include "AddGroup.h"
#include "Analyze.h"
#include "Clip.h"
#include "Create.h"
#include "Diceware.h"
#include "Edit.h"
#include "Estimate.h"
#include "Export.h"
#include "Generate.h"
#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"
const QCommandLineOption Command::QuietOption =
QCommandLineOption(QStringList() << "q"
<< "quiet",
QObject::tr("Silence password prompt and other secondary outputs."));
const QCommandLineOption Command::KeyFileOption = QCommandLineOption(QStringList() << "k"
<< "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
const QCommandLineOption Command::NoPasswordOption =
QCommandLineOption(QStringList() << "no-password", QObject::tr("Deactivate password key for the database."));
const QCommandLineOption Command::YubiKeyOption =
QCommandLineOption(QStringList() << "y"
<< "yubikey",
QObject::tr("Yubikey slot used to encrypt the database."),
QObject::tr("slot"));
QMap<QString, Command*> commands;
Command::Command()
{
options.append(Command::QuietOption);
}
Command::~Command()
{
}
QString Command::getDescriptionLine()
{
QString response = name;
QString space(" ");
QString spaces = space.repeated(15 - name.length());
response = response.append(spaces);
response = response.append(description);
response = response.append("\n");
return response;
}
QSharedPointer<QCommandLineParser> Command::getCommandLineParser(const QStringList& arguments)
{
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
QSharedPointer<QCommandLineParser> parser = QSharedPointer<QCommandLineParser>(new QCommandLineParser());
parser->setApplicationDescription(description);
for (const CommandLineArgument& positionalArgument : positionalArguments) {
parser->addPositionalArgument(
positionalArgument.name, positionalArgument.description, positionalArgument.syntax);
}
for (const CommandLineArgument& optionalArgument : optionalArguments) {
parser->addPositionalArgument(optionalArgument.name, optionalArgument.description, optionalArgument.syntax);
}
for (const QCommandLineOption& option : options) {
parser->addOption(option);
}
parser->addHelpOption();
parser->process(arguments);
if (parser->positionalArguments().size() < positionalArguments.size()) {
errorTextStream << parser->helpText().replace("[options]", name.append(" [options]"));
return QSharedPointer<QCommandLineParser>(nullptr);
}
if (parser->positionalArguments().size() > (positionalArguments.size() + optionalArguments.size())) {
errorTextStream << parser->helpText().replace("[options]", name.append(" [options]"));
return QSharedPointer<QCommandLineParser>(nullptr);
}
return parser;
}
void populateCommands()
{
if (commands.isEmpty()) {
commands.insert(QString("add"), new Add());
commands.insert(QString("analyze"), new Analyze());
commands.insert(QString("clip"), new Clip());
commands.insert(QString("create"), new Create());
commands.insert(QString("diceware"), new Diceware());
commands.insert(QString("edit"), new Edit());
commands.insert(QString("estimate"), new Estimate());
commands.insert(QString("export"), new Export());
commands.insert(QString("generate"), new Generate());
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());
}
}
Command* Command::getCommand(const QString& commandName)
{
populateCommands();
if (commands.contains(commandName)) {
return commands[commandName];
}
return nullptr;
}
QList<Command*> Command::getCommands()
{
populateCommands();
return commands.values();
}
/*
* 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 <cstdio>
#include <cstdlib>
#include <utility>
#include <QMap>
#include "Command.h"
#include "Add.h"
#include "AddGroup.h"
#include "Analyze.h"
#include "Clip.h"
#include "Close.h"
#include "Create.h"
#include "Diceware.h"
#include "Edit.h"
#include "Estimate.h"
#include "Exit.h"
#include "Export.h"
#include "Generate.h"
#include "Help.h"
#include "List.h"
#include "Locate.h"
#include "Merge.h"
#include "Move.h"
#include "Open.h"
#include "Remove.h"
#include "RemoveGroup.h"
#include "Show.h"
#include "TextStream.h"
#include "Utils.h"
const QCommandLineOption Command::HelpOption = QCommandLineOption(QStringList()
#ifdef Q_OS_WIN
<< QStringLiteral("?")
#endif
<< QStringLiteral("h") << QStringLiteral("help"),
QObject::tr("Display this help."));
const QCommandLineOption Command::QuietOption =
QCommandLineOption(QStringList() << "q"
<< "quiet",
QObject::tr("Silence password prompt and other secondary outputs."));
const QCommandLineOption Command::KeyFileOption = QCommandLineOption(QStringList() << "k"
<< "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
const QCommandLineOption Command::NoPasswordOption =
QCommandLineOption(QStringList() << "no-password", QObject::tr("Deactivate password key for the database."));
const QCommandLineOption Command::YubiKeyOption =
QCommandLineOption(QStringList() << "y"
<< "yubikey",
QObject::tr("Yubikey slot used to encrypt the database."),
QObject::tr("slot"));
namespace
{
QSharedPointer<QCommandLineParser> buildParser(Command* command)
{
auto parser = QSharedPointer<QCommandLineParser>(new QCommandLineParser());
parser->setApplicationDescription(command->description);
for (const CommandLineArgument& positionalArgument : command->positionalArguments) {
parser->addPositionalArgument(
positionalArgument.name, positionalArgument.description, positionalArgument.syntax);
}
for (const CommandLineArgument& optionalArgument : command->optionalArguments) {
parser->addPositionalArgument(optionalArgument.name, optionalArgument.description, optionalArgument.syntax);
}
for (const QCommandLineOption& option : command->options) {
parser->addOption(option);
}
parser->addOption(Command::HelpOption);
return parser;
}
} // namespace
Command::Command()
: currentDatabase(nullptr)
{
options.append(Command::QuietOption);
}
Command::~Command()
{
}
QString Command::getDescriptionLine()
{
QString response = name;
QString space(" ");
QString spaces = space.repeated(15 - name.length());
response = response.append(spaces);
response = response.append(description);
response = response.append("\n");
return response;
}
QString Command::getHelpText()
{
return buildParser(this)->helpText().replace("[options]", name + " [options]");
}
QSharedPointer<QCommandLineParser> Command::getCommandLineParser(const QStringList& arguments)
{
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
QSharedPointer<QCommandLineParser> parser = buildParser(this);
if (!parser->parse(arguments)) {
errorTextStream << parser->errorText() << "\n\n";
errorTextStream << getHelpText();
return {};
}
if (parser->positionalArguments().size() < positionalArguments.size()) {
errorTextStream << getHelpText();
return {};
}
if (parser->positionalArguments().size() > (positionalArguments.size() + optionalArguments.size())) {
errorTextStream << getHelpText();
return {};
}
if (parser->isSet(HelpOption)) {
errorTextStream << getHelpText();
return {};
}
return parser;
}
namespace Commands
{
QMap<QString, QSharedPointer<Command>> s_commands;
void setupCommands(bool interactive)
{
s_commands.clear();
s_commands.insert(QStringLiteral("add"), QSharedPointer<Command>(new Add()));
s_commands.insert(QStringLiteral("analyze"), QSharedPointer<Command>(new Analyze()));
s_commands.insert(QStringLiteral("clip"), QSharedPointer<Command>(new Clip()));
s_commands.insert(QStringLiteral("close"), QSharedPointer<Command>(new Close()));
s_commands.insert(QStringLiteral("create"), QSharedPointer<Command>(new Create()));
s_commands.insert(QStringLiteral("diceware"), QSharedPointer<Command>(new Diceware()));
s_commands.insert(QStringLiteral("edit"), QSharedPointer<Command>(new Edit()));
s_commands.insert(QStringLiteral("estimate"), QSharedPointer<Command>(new Estimate()));
s_commands.insert(QStringLiteral("generate"), QSharedPointer<Command>(new Generate()));
s_commands.insert(QStringLiteral("help"), QSharedPointer<Command>(new Help()));
s_commands.insert(QStringLiteral("locate"), QSharedPointer<Command>(new Locate()));
s_commands.insert(QStringLiteral("ls"), QSharedPointer<Command>(new List()));
s_commands.insert(QStringLiteral("merge"), QSharedPointer<Command>(new Merge()));
s_commands.insert(QStringLiteral("mkdir"), QSharedPointer<Command>(new AddGroup()));
s_commands.insert(QStringLiteral("mv"), QSharedPointer<Command>(new Move()));
s_commands.insert(QStringLiteral("open"), QSharedPointer<Command>(new Open()));
s_commands.insert(QStringLiteral("rm"), QSharedPointer<Command>(new Remove()));
s_commands.insert(QStringLiteral("rmdir"), QSharedPointer<Command>(new RemoveGroup()));
s_commands.insert(QStringLiteral("show"), QSharedPointer<Command>(new Show()));
if (interactive) {
s_commands.insert(QStringLiteral("exit"), QSharedPointer<Command>(new Exit("exit")));
s_commands.insert(QStringLiteral("quit"), QSharedPointer<Command>(new Exit("quit")));
} else {
s_commands.insert(QStringLiteral("export"), QSharedPointer<Command>(new Export()));
}
}
QList<QSharedPointer<Command>> getCommands()
{
return s_commands.values();
}
QSharedPointer<Command> getCommand(const QString& commandName)
{
return s_commands.value(commandName);
}
} // namespace Commands