| 816 | } |
| 817 | |
| 818 | QByteArray Application::editConfigViaEditor() const |
| 819 | { |
| 820 | // read editor command and options |
| 821 | const auto *const editorArgValue(m_args.editor.firstValue()); |
| 822 | const auto editorCommand(editorArgValue ? QString::fromLocal8Bit(editorArgValue) : QString()); |
| 823 | if (editorCommand.isEmpty()) { |
| 824 | cerr << Phrases::Error << "No editor command specified. It must be either passed via --editor argument or EDITOR environment variable." |
| 825 | << Phrases::EndFlush; |
| 826 | return QByteArray(); |
| 827 | } |
| 828 | QStringList editorOptions; |
| 829 | if (m_args.editor.isPresent()) { |
| 830 | const auto &editorArgValues(m_args.editor.values()); |
| 831 | if (!editorArgValues.empty()) { |
| 832 | editorOptions.reserve(trQuandity(editorArgValues.size())); |
| 833 | for (auto i = editorArgValues.cbegin() + 1, end = editorArgValues.cend(); i != end; ++i) { |
| 834 | editorOptions << QString::fromLocal8Bit(*i); |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | // write config to temporary file |
| 840 | QTemporaryFile tempFile(QStringLiteral("syncthing-config-XXXXXX.json")); |
| 841 | if (!tempFile.open() || !tempFile.write(QJsonDocument(m_connection.rawConfig()).toJson(QJsonDocument::Indented))) { |
| 842 | cerr << Phrases::Error << "Unable to write the configuration to a temporary file." << Phrases::EndFlush; |
| 843 | return QByteArray(); |
| 844 | } |
| 845 | editorOptions << tempFile.fileName(); |
| 846 | tempFile.close(); |
| 847 | |
| 848 | // open editor and wait until it has finished |
| 849 | cerr << Phrases::Info << "Waiting till editor closed ..." << TextAttribute::Reset << flush; |
| 850 | QProcess editor; |
| 851 | editor.setProcessChannelMode(QProcess::ForwardedChannels); |
| 852 | editor.setInputChannelMode(QProcess::ForwardedInputChannel); |
| 853 | editor.start(editorCommand, editorOptions); |
| 854 | editor.waitForFinished(-1); |
| 855 | cerr << Phrases::Override; |
| 856 | |
| 857 | // handle editor crash |
| 858 | if (editor.exitStatus() == QProcess::CrashExit) { |
| 859 | cerr << Phrases::Error << "Editor crashed with exit code " << editor.exitCode() << Phrases::End << "invocation command: " << editorArgValue; |
| 860 | if (m_args.editor.isPresent()) { |
| 861 | const auto &editorArgValues(m_args.editor.values()); |
| 862 | if (!editorArgValues.empty()) { |
| 863 | for (auto i = editorArgValues.cbegin() + 1, end = editorArgValues.cend(); i != end; ++i) { |
| 864 | cerr << ' ' << *i; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | cerr << endl; |
| 869 | return QByteArray(); |
| 870 | } |
| 871 | |
| 872 | // read (altered) configuration again |
| 873 | QFile tempFile2(editorOptions.back()); |
| 874 | if (!tempFile2.open(QIODevice::ReadOnly)) { |
| 875 | cerr << Phrases::Error << "Unable to open temporary file containing the configuration again." << Phrases::EndFlush; |
nothing calls this directly
no test coverage detected