File indexing completed on 2024-12-22 05:06:06

0001 /*
0002  *   Copyright (C) 2020 Christian Mollekopf <mollekopf@kolabsystems.com>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include <QDebug>
0021 #include <QObject> // tr()
0022 #include <QTimer>
0023 
0024 #include "common/resource.h"
0025 #include "common/resourcecontrol.h"
0026 #include "common/log.h"
0027 #include "common/definitions.h"
0028 #include "common/secretstore.h"
0029 
0030 #include "sinksh_utils.h"
0031 #include "state.h"
0032 #include "syntaxtree.h"
0033 
0034 namespace SinkConnectionTest
0035 {
0036 
0037 bool connectiontest(const QStringList &args, State &state)
0038 {
0039     auto options = SyntaxTree::parseOptions(args);
0040     if (options.options.value("password").isEmpty()) {
0041         state.printError(QObject::tr("Pass in a password with --password"));
0042         return false;
0043     }
0044     auto password = options.options.value("password").first();
0045 
0046     Sink::Query query;
0047     auto resourceId = SinkshUtils::parseUid(options.positionalArguments.first().toLatin1());
0048 
0049     Sink::SecretStore::instance().insert(resourceId, password);
0050 
0051     Sink::ResourceControl::inspect(Sink::ResourceControl::Inspection::ConnectionInspection(resourceId))
0052         .then([state](const KAsync::Error &error) {
0053             int exitCode = 0;
0054             if (error) {
0055                 state.printLine("Connection test failed!");
0056                 exitCode = 1;
0057             } else {
0058                 state.printLine("Connection test successful!");
0059             }
0060             state.commandFinished(exitCode);
0061         }).exec();
0062 
0063     return true;
0064 }
0065 
0066 Syntax::List syntax()
0067 {
0068     Syntax connectiontest("connectiontest", QObject::tr("Test the connection to a server."), &SinkConnectionTest::connectiontest, Syntax::EventDriven);
0069 
0070     connectiontest.addPositionalArgument({"resourceId", "The ID of the resource to synchronize"});
0071     connectiontest.addParameter("password", {"password", "The password of the resource", true});
0072 
0073     connectiontest.completer = &SinkshUtils::resourceCompleter;
0074 
0075     return Syntax::List() << connectiontest;
0076 }
0077 
0078 REGISTER_SYNTAX(SinkConnectionTest)
0079 
0080 }