Commit dc70225a authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Allow ENV variables in shell runner

FEATURE: 409107
FIXED-IN: 5.20
parent 88ff6b48
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <KNotificationJobUiDelegate> #include <KNotificationJobUiDelegate>
#include <KToolInvocation> #include <KToolInvocation>
#include <KShell>
#include <QRegularExpression>
#include <QStandardPaths>
#include <KIO/CommandLauncherJob> #include <KIO/CommandLauncherJob>
...@@ -36,9 +39,6 @@ ShellRunner::ShellRunner(QObject *parent, const QVariantList &args) ...@@ -36,9 +39,6 @@ ShellRunner::ShellRunner(QObject *parent, const QVariantList &args)
// If the runner is not authorized we can suspend it // If the runner is not authorized we can suspend it
bool enabled = KAuthorized::authorize(QStringLiteral("run_command")) && KAuthorized::authorize(QStringLiteral("shell_access")); bool enabled = KAuthorized::authorize(QStringLiteral("run_command")) && KAuthorized::authorize(QStringLiteral("shell_access"));
suspendMatching(!enabled); suspendMatching(!enabled);
setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File |
Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::UnknownType |
Plasma::RunnerContext::Help);
addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds commands that match :q:, using common shell syntax"))); addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds commands that match :q:, using common shell syntax")));
m_actionList = {addAction(QStringLiteral("runInTerminal"), m_actionList = {addAction(QStringLiteral("runInTerminal"),
...@@ -53,20 +53,33 @@ ShellRunner::~ShellRunner() ...@@ -53,20 +53,33 @@ ShellRunner::~ShellRunner()
void ShellRunner::match(Plasma::RunnerContext &context) void ShellRunner::match(Plasma::RunnerContext &context)
{ {
const QString term = context.query(); bool isShellCommand = context.type() == Plasma::RunnerContext::ShellCommand;
Plasma::QueryMatch match(this); QStringList envs;
match.setId(term); QString command;
match.setType(Plasma::QueryMatch::ExactMatch); // If it is not a shell command we check if we use ENV variables, FEATURE: 409107
match.setIcon(m_matchIcon); // This is not recognized when setting the context type and we can't change it, because
match.setText(i18n("Run %1", term)); // other runners depend on the current pattern
match.setRelevance(0.7); if (!isShellCommand) {
context.addMatch(match); isShellCommand = parseENVVariables(context.query(), envs, command);
}
if (isShellCommand) {
const QString term = context.query();
Plasma::QueryMatch match(this);
match.setId(term);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setIcon(m_matchIcon);
match.setText(i18n("Run %1", term));
match.setData(QVariantList({command, envs}));
match.setRelevance(0.7);
context.addMatch(match);
}
} }
void ShellRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) void ShellRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{ {
if (match.selectedAction()) { if (match.selectedAction()) {
KToolInvocation::invokeTerminal(context.query()); const QVariantList data = match.data().toList();
KToolInvocation::invokeTerminal(data.at(0).toString(), data.at(1).toStringList());
return; return;
} }
...@@ -82,4 +95,21 @@ QList<QAction *> ShellRunner::actionsForMatch(const Plasma::QueryMatch &match) ...@@ -82,4 +95,21 @@ QList<QAction *> ShellRunner::actionsForMatch(const Plasma::QueryMatch &match)
return m_actionList; return m_actionList;
} }
bool ShellRunner::parseENVVariables(const QString &query, QStringList &envs, QString &command)
{
const static QRegularExpression envRegex = QRegularExpression(QStringLiteral("^.+=.+$"));
const QStringList split = KShell::splitArgs(query);
for (const auto &entry : split) {
if (!QStandardPaths::findExecutable(entry).isEmpty()) {
command = KShell::joinArgs(split.mid(split.indexOf(entry)));
return true;
} else if (envRegex.match(entry).hasMatch()) {
envs.append(entry);
} else {
return false;
}
}
return false;
}
#include "shellrunner.moc" #include "shellrunner.moc"
...@@ -38,6 +38,7 @@ class ShellRunner : public Plasma::AbstractRunner ...@@ -38,6 +38,7 @@ class ShellRunner : public Plasma::AbstractRunner
QList<QAction *> actionsForMatch(const Plasma::QueryMatch &match) override; QList<QAction *> actionsForMatch(const Plasma::QueryMatch &match) override;
private: private:
bool parseENVVariables(const QString &query, QStringList &envs, QString &command);
QList<QAction *> m_actionList; QList<QAction *> m_actionList;
QIcon m_matchIcon; QIcon m_matchIcon;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment