libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
exec.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/config.hpp>
5
6#include <sshpp/channel.hpp>
7#include <sshpp/export.hpp>
8#include <sshpp/fwd.hpp>
9#include <sshpp/result.hpp>
10#include <sshpp/session.hpp>
11#include <sshpp/types.hpp>
12
13#include <chrono>
14#include <functional>
15#include <optional>
16#include <string>
17#include <vector>
18
19namespace sshpp {
20
21struct ExecResult {
22 int exit_code = -1;
23 std::optional<std::string> exit_signal;
24 std::string stdout_text;
25 std::string stderr_text;
26 bool stdout_truncated = false;
27 bool stderr_truncated = false;
28
29 explicit operator bool() const noexcept { return exit_code == 0; }
31 const ExecResult& check() const;
32};
33
34using OutputSink = std::function<void(Stream, ByteView)>;
35
39class SSHPP_API Exec {
40public:
41 explicit Exec(Session& session) : session_(session) {}
42
43 Exec& env(std::string_view name, std::string_view value);
44 Exec& pty(bool enable = true, std::string_view term = "xterm-256color", PtySize size = {});
45 Exec& stdin_data(std::string data);
46 Exec& timeout(std::chrono::milliseconds t) { timeout_ = t; return *this; }
47 Exec& max_output(std::size_t bytes) { max_output_ = bytes; return *this; }
48 Exec& merge_stderr(bool v = true) { merge_stderr_ = v; return *this; }
49 Exec& sink(OutputSink s) { sink_ = std::move(s); return *this; }
50
51 Result<ExecResult> try_run(std::string_view command);
52 ExecResult run(std::string_view command);
53
55 Result<ExecResult> try_run(const std::vector<std::string>& argv);
56
57private:
58 Session& session_;
59 std::vector<std::pair<std::string, std::string>> env_;
60 bool pty_ = false;
61 std::string pty_term_ = "xterm-256color";
62 PtySize pty_size_{};
63 std::optional<std::string> stdin_data_;
64 std::optional<std::chrono::milliseconds> timeout_;
65 std::size_t max_output_ = 16u << 20;
66 bool merge_stderr_ = false;
67 OutputSink sink_;
68};
69
71SSHPP_API std::string shell_quote(std::string_view arg);
72
73} // namespace sshpp
74
75#if SSHPP_HEADER_ONLY
76#include <sshpp/detail/exec.ipp>
77#endif
Input byte range. Implicitly constructible from std::string_view / std::vector<std::byte>.
Definition types.hpp:54
Definition exec.hpp:39
Exec & merge_stderr(bool v=true)
Definition exec.hpp:48
Exec & timeout(std::chrono::milliseconds t)
Definition exec.hpp:46
Exec(Session &session)
Definition exec.hpp:41
Exec & max_output(std::size_t bytes)
Definition exec.hpp:47
Exec & sink(OutputSink s)
Definition exec.hpp:49
Definition result.hpp:16
Definition session.hpp:41
Definition auth.hpp:18
std::function< void(Stream, ByteView)> OutputSink
Definition exec.hpp:34
Stream
Definition channel.hpp:21
SSHPP_INLINE std::string shell_quote(std::string_view arg)
Escapes a single argument for a POSIX shell.
Definition exec.ipp:143
Definition exec.hpp:21
const ExecResult & check() const
Throws ChannelError with the captured stderr if exit_code != 0.
Definition exec.ipp:12
std::string stdout_text
Definition exec.hpp:24
std::optional< std::string > exit_signal
Definition exec.hpp:23
int exit_code
Definition exec.hpp:22
std::string stderr_text
Definition exec.hpp:25
bool stderr_truncated
Definition exec.hpp:27
bool stdout_truncated
Definition exec.hpp:26
Definition channel.hpp:32