summaryrefslogtreecommitdiffstats
path: root/src/sdk/commandlineinterface.cpp
blob: a6cd2215123c581fc91c3c96805058344237daa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include "commandlineinterface.h"

#include <component.h>
#include <init.h>
#include <packagemanagercore.h>
#include <globals.h>
#include <productkeycheck.h>
#include <errors.h>
#include <loggingutils.h>

#include <QDir>

CommandLineInterface::CommandLineInterface(int &argc, char *argv[])
    : SDKApp<QCoreApplication>(argc, argv)
{
    QInstaller::init(); // register custom operations
    m_parser.parse(arguments());
}

CommandLineInterface::~CommandLineInterface()
{
    delete m_core;
}

bool CommandLineInterface::initialize()
{
    QString errorMessage;
    if (!init(errorMessage)) {
        qCWarning(QInstaller::lcInstallerInstallLog) << errorMessage;
        return false;
    }
    if (m_core->settings().disableCommandLineInterface()) {
        qCWarning(QInstaller::lcInstallerInstallLog)
                << "Command line interface support disabled from installer configuration by vendor!";
        return false;
    }
    // Filter the arguments list by removing any key=value pair occurrences.
    m_positionalArguments = m_parser.positionalArguments();
    foreach (const QString &argument, m_positionalArguments) {
        if (argument.contains(QLatin1Char('=')))
            m_positionalArguments.removeOne(argument);
    }
    if (m_positionalArguments.isEmpty()) {
        // Special case, normally positional arguments should contain
        // at least the command invoked.
        qCDebug(QInstaller::lcInstallerInstallLog)
                << "Command line interface initialized but no command argument found.";
    } else {
        // Sanity and order of arguments already checked in main(), we should be
        // quite safe to assume that command is the first positional argument.
        m_positionalArguments.removeFirst();
    }
    return true;
}

int CommandLineInterface::checkUpdates()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (m_core->isInstaller()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot check updates with installer.";
        return EXIT_FAILURE;
    }
    m_core->setUpdater();
    if (!m_core->fetchRemotePackagesTree()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << m_core->error();
        return EXIT_FAILURE;
    }

    const QList<QInstaller::Component *> components =
        m_core->components(QInstaller::PackageManagerCore::ComponentType::Root);
    if (components.isEmpty()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "There are currently no updates available.";
        return EXIT_SUCCESS;
    }
    QInstaller::LoggingHandler::instance().printComponentInfo(components);
    return EXIT_SUCCESS;
}

int CommandLineInterface::listInstalledPackages()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (m_core->isInstaller()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot list installed packages with installer.";
        return EXIT_FAILURE;
    }
    m_core->setPackageManager();
    QString regexp;
    if (!m_positionalArguments.isEmpty())
        regexp = m_positionalArguments.first();
    m_core->listInstalledPackages(regexp);
    return EXIT_SUCCESS;
}

int CommandLineInterface::searchAvailablePackages()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (!m_core->isInstaller())
        m_core->setPackageManager();
    if (!checkLicense())
        return EXIT_FAILURE;
    QString regexp;
    if (!m_positionalArguments.isEmpty())
        regexp = m_positionalArguments.first();
    m_core->listAvailablePackages(regexp, parsePackageFilters());
    return EXIT_SUCCESS;
}

int CommandLineInterface::updatePackages()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (m_core->isInstaller()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot update packages with installer.";
        return EXIT_FAILURE;
    }
    if (!checkLicense())
        return EXIT_FAILURE;
    try {
        return m_core->updateComponentsSilently(m_positionalArguments);
    } catch (const QInstaller::Error &err) {
        qCCritical(QInstaller::lcInstallerInstallLog) << err.message();
        return EXIT_FAILURE;
    }
}

int CommandLineInterface::installPackages()
{
    if (!initialize() || (m_core->isInstaller() && !setTargetDir()) || !checkLicense())
        return EXIT_FAILURE;
    try {
        if (m_positionalArguments.isEmpty()) {
            if (!m_core->isInstaller()) {
                qCWarning(QInstaller::lcInstallerInstallLog)
                    << "Cannot perform default installation with maintenance tool.";
                return EXIT_FAILURE;
            }
            // No packages provided, install default components
            return m_core->installDefaultComponentsSilently();
        }
        // Normal installation
        return m_core->installSelectedComponentsSilently(m_positionalArguments);
    } catch (const QInstaller::Error &err) {
        qCCritical(QInstaller::lcInstallerInstallLog) << err.message();
        return EXIT_FAILURE;
    }
}

int CommandLineInterface::uninstallPackages()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (m_core->isInstaller()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot uninstall packages with installer.";
        return EXIT_FAILURE;
    }
    m_core->setPackageManager();
    try {
        return m_core->uninstallComponentsSilently(m_positionalArguments);
    } catch (const QInstaller::Error &err) {
        qCCritical(QInstaller::lcInstallerInstallLog) << err.message();
        return EXIT_FAILURE;
    }
}

int CommandLineInterface::removeInstallation()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (m_core->isInstaller()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot uninstall packages with installer.";
        return EXIT_FAILURE;
    }
    m_core->setUninstaller();
    try {
        return m_core->removeInstallationSilently();
    } catch (const QInstaller::Error &err) {
        qCCritical(QInstaller::lcInstallerInstallLog) << err.message();
        return EXIT_FAILURE;
    }
}

int CommandLineInterface::createOfflineInstaller()
{
    if (!initialize())
        return EXIT_FAILURE;
    if (!m_core->isInstaller() || m_core->isOfflineOnly()) {
        qCWarning(QInstaller::lcInstallerInstallLog)
            << "Offline installer can only be created with an online installer.";
        return EXIT_FAILURE;
    }
    if (m_positionalArguments.isEmpty()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Missing components argument.";
        return EXIT_FAILURE;
    }
    if (!(setTargetDir() && checkLicense()))
        return EXIT_FAILURE;

    if (m_parser.isSet(CommandLineOptions::scOfflineInstallerNameLong)) {
        m_core->setOfflineBinaryName(m_parser.value(CommandLineOptions::scOfflineInstallerNameLong));
    } else {
        const QString offlineName = m_core->offlineBinaryName();
        qCDebug(QInstaller::lcInstallerInstallLog) << "No filename specified for "
            "the generated installer, using default name:" << offlineName;
    }
    try {
        return m_core->createOfflineInstaller(m_positionalArguments);
    } catch (const QInstaller::Error &err) {
        qCCritical(QInstaller::lcInstallerInstallLog) << err.message();
        return EXIT_FAILURE;
    }
}

bool CommandLineInterface::checkLicense()
{
    const ProductKeyCheck *const productKeyCheck = ProductKeyCheck::instance();
    if (!productKeyCheck->hasValidLicense()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << "No valid license found.";
        return false;
    }
    return true;
}

bool CommandLineInterface::setTargetDir()
{
    QString targetDir;
    if (m_parser.isSet(CommandLineOptions::scRootLong)) {
        targetDir = m_parser.value(CommandLineOptions::scRootLong);
    } else {
        targetDir = m_core->value(QLatin1String("TargetDir"));
        qCDebug(QInstaller::lcInstallerInstallLog) << "No target directory specified, using default value:" << targetDir;
    }
    if (m_core->checkTargetDir(targetDir)) {
        QString targetDirWarning = m_core->targetDirWarning(targetDir);
        if (!targetDirWarning.isEmpty()) {
            qCWarning(QInstaller::lcInstallerInstallLog) << m_core->targetDirWarning(targetDir);
        } else {
            m_core->setValue(QLatin1String("TargetDir"), targetDir);
            return true;
        }
    }
    return false;
}

QHash<QString, QString> CommandLineInterface::parsePackageFilters()
{
    QHash<QString, QString> filterHash;
    if (m_parser.isSet(CommandLineOptions::scFilterPackagesLong)) {
        const QStringList filterList = m_parser.value(CommandLineOptions::scFilterPackagesLong)
            .split(QLatin1Char(','));

        for (auto &filter : filterList) {
            const int i = filter.indexOf(QLatin1Char('='));
            const QString element = filter.left(i).trimmed();
            const QString value = filter.mid(i + 1).trimmed();

            if ((i == -1) || (filter.count(QLatin1Char('=') > 1))
                    || element.isEmpty() || value.isEmpty()) {
                qCWarning(QInstaller::lcInstallerInstallLog).nospace() << "Ignoring unknown entry "
                    << filter << "in package filter arguments. Please use syntax \"element=regex,...\".";
                continue;
            }
            filterHash.insert(element, value);
        }
    }
    return filterHash;
}