Allow running Syncthing's CLI via Syncthing Tray when bundling libsyncthing

This commit is contained in:
Martchus 2022-06-26 02:25:23 +02:00
parent 8ba75f50e2
commit a0923f8c1a
7 changed files with 67 additions and 4 deletions

View File

@ -117,8 +117,13 @@ file(
GLOB_RECURSE SRC_FILES_SYNCTHING
LIST_DIRECTORIES false
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"${SYNCTHING_PATH}/c-bindings/*.go" "${SYNCTHING_PATH}/c-bindings/*.h" "${SYNCTHING_PATH}/c-bindings/*.c"
"${SYNCTHING_PATH}/lib/*.go" "${SYNCTHING_PATH}/lib/*.h" "${SYNCTHING_PATH}/lib/*.c")
"${SYNCTHING_PATH}/c-bindings/*.go"
"${SYNCTHING_PATH}/c-bindings/*.h"
"${SYNCTHING_PATH}/c-bindings/*.c"
"${SYNCTHING_PATH}/cmd/syncthing/cli/*.go"
"${SYNCTHING_PATH}/lib/*.go"
"${SYNCTHING_PATH}/lib/*.h"
"${SYNCTHING_PATH}/lib/*.c")
if (NOT SRC_FILES_SYNCTHING)
message(
FATAL_ERROR

@ -1 +1 @@
Subproject commit d6f512c7f808f706db54920c0cf1639d86f7b1c7
Subproject commit 452e7d21d3861f14d3162a66cc67b9d3c428b44c

View File

@ -13,6 +13,7 @@
#include "libsyncthinginternal.h"
#include <atomic>
#include <cstring>
using namespace std;
@ -38,6 +39,14 @@ inline _GoString_ gostr(const string &str)
return _GoString_{ str.data(), static_cast<ptrdiff_t>(str.size()) };
}
/*!
* \brief Converts the specified null-terminated C-string to a "GoString".
*/
inline _GoString_ gostr(const char *str)
{
return _GoString_{ str, static_cast<ptrdiff_t>(std::strlen(str)) };
}
/*!
* \brief Converts the specified C-string to a std::string. Takes care of freeing \a str.
*/
@ -203,4 +212,16 @@ string longSyncthingVersion()
return stdstr(::libst_long_syncthing_version());
}
/*!
* \brief Runs Syncthing's CLI with the specified \a args.
*/
long long runCli(const std::vector<const char *> &args)
{
::libst_clear_cli_args();
for (const auto *const arg : args) {
::libst_append_cli_arg(gostr(arg));
}
return ::libst_run_cli();
}
} // namespace LibSyncthing

View File

@ -59,6 +59,7 @@ LIB_SYNCTHING_EXPORT std::int64_t stopSyncthing();
LIB_SYNCTHING_EXPORT std::string ownDeviceId();
LIB_SYNCTHING_EXPORT std::string syncthingVersion();
LIB_SYNCTHING_EXPORT std::string longSyncthingVersion();
LIB_SYNCTHING_EXPORT long long runCli(const std::vector<const char *> &args);
} // namespace LibSyncthing

View File

@ -32,6 +32,7 @@ class InterfaceTests : public TestFixture {
CPPUNIT_TEST(testVersion);
CPPUNIT_TEST(testRunWithoutConfig);
CPPUNIT_TEST(testRunWithConfig);
CPPUNIT_TEST(testCli);
CPPUNIT_TEST_SUITE_END();
public:
@ -41,6 +42,7 @@ public:
void testVersion();
void testRunWithoutConfig();
void testRunWithConfig();
void testCli();
void setUp() override;
void tearDown() override;
@ -249,3 +251,11 @@ void InterfaceTests::testRunWithConfig()
options.configDir = options.dataDir = setupTestConfigDir();
testRun(bind(static_cast<std::int64_t (*)(const RuntimeOptions &)>(&runSyncthing), cref(options)), true);
}
/*!
* \brief Tests running Syncthing's CLI.
*/
void InterfaceTests::testCli()
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("run arbitrary CLI command", 0ll, runCli({ "config", "version" }));
}

View File

@ -118,6 +118,14 @@ endif ()
# link also explicitly against the following Qt modules
list(APPEND ADDITIONAL_QT_MODULES Network)
# configure libsyncthing
option(USE_LIBSYNCTHING "whether libsyncthing should be included for syncthingtray's CLI" OFF)
if (USE_LIBSYNCTHING)
find_package(syncthing ${META_APP_VERSION} REQUIRED)
use_syncthing()
list(APPEND META_PUBLIC_COMPILE_DEFINITIONS SYNCTHINGTRAY_USE_LIBSYNCTHING)
endif ()
# apply basic configuration
include(BasicConfig)

View File

@ -11,6 +11,10 @@
#include <syncthingconnector/syncthingservice.h>
#endif
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
#include <syncthing/interface.h>
#endif
#include "resources/config.h"
#include "resources/qtconfig.h"
@ -172,9 +176,23 @@ int runApplication(int argc, const char *const *argv)
widgetsGuiArg.addSubArgument(&configPathArg);
widgetsGuiArg.addSubArgument(&singleInstance);
widgetsGuiArg.addSubArgument(&newInstanceArg);
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
auto cliArg = OperationArgument("cli", 'c', "run Syncthing's CLI");
cliArg.setRequiredValueCount(Argument::varValueCount);
#endif
parser.setMainArguments({ &qtConfigArgs.qtWidgetsGuiArg(), &parser.noColorArg(), &parser.helpArg() });
parser.setMainArguments({ &qtConfigArgs.qtWidgetsGuiArg(),
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
&cliArg,
#endif
&parser.noColorArg(), &parser.helpArg() });
parser.parseArgs(argc, argv);
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
if (cliArg.isPresent()) {
CMD_UTILS_START_CONSOLE;
return static_cast<int>(LibSyncthing::runCli(cliArg.values()));
}
#endif
if (!qtConfigArgs.qtWidgetsGuiArg().isPresent()) {
return 0;
}