ssh-cli 0.5.5

Native Rust CLI that gives LLMs (Claude Code, Cursor, Windsurf) the ability to operate remote servers via SSH over stdin/stdout
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Multi-host / multi-result batch emitters (G-COMP-06d).
//!
//! Keeps fan-out JSON/text formatting separate from single-record CRUD emitters.
#![forbid(unsafe_code)]

use super::{is_quiet, report_json_serialize_error};
use crate::domain::BatchRunId;
use crate::json_wire::{
    self, ExecBatchJson, ExecHostJson, HealthBatchJson, HealthHostJson, ScpBatchJson, ScpHostJson,
    ScpTransferJson, TunnelCloseReason, TunnelClosedJson, TunnelListeningJson,
};
#[cfg(feature = "ssh-real")]
use crate::json_wire::{
    SftpBatchJson, SftpFsOpJson, SftpListEntryJson, SftpListJson, SftpTransferJson,
};
// A6: the SFTP emitters below take types owned by the russh-backed subsystem, so
// they only exist when that stack is compiled in.
#[cfg(feature = "ssh-real")]
use crate::sftp::batch::HostSftpResult;
#[cfg(feature = "ssh-real")]
use crate::ssh::sftp_types::{SftpListEntry, SftpStat};
use crate::vps::{HostExecResult, HostHealthResult};
use std::io::{self, Write};

/// Prints multi-host health-check results (text or single-root JSON batch).
///
/// # Errors
/// Serialization or stdout I/O.
pub fn print_health_batch(
    results: &[HostHealthResult],
    max_concurrency: usize,
    json: bool,
) -> io::Result<()> {
    if json {
        // One v7 id per fan-out command (before/with emit; not per host).
        let batch_run_id = BatchRunId::new().to_string_canonical();
        let v = HealthBatchJson {
            event: "health-check-batch".into(),
            batch_run_id,
            max_concurrency: u32::try_from(max_concurrency).unwrap_or(u32::MAX),
            results: results
                .iter()
                .map(|h| HealthHostJson {
                    name: h.name.clone(),
                    status: if h.ok { "ok".into() } else { "error".into() },
                    latency_ms: h.latency_ms,
                    error: h.error.clone(),
                })
                .collect(),
        };
        return match json_wire::print_json_line(&v) {
            Ok(()) => Ok(()),
            Err(e) => {
                report_json_serialize_error(&e);
                Err(e)
            }
        };
    }
    if is_quiet() {
        return Ok(());
    }
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    writeln!(
        out,
        "health-check --all (max_concurrency={max_concurrency}, hosts={})",
        results.len()
    )?;
    for h in results {
        match (h.ok, h.latency_ms) {
            (true, Some(ms)) => writeln!(out, "  ok  {}  {ms}ms", h.name)?,
            (true, None) => writeln!(out, "  ok  {}", h.name)?,
            (false, _) => {
                let err = h.error.as_deref().unwrap_or("error");
                writeln!(out, "  ERR {}  {err}", h.name)?;
            }
        }
    }
    out.flush()
}

/// Prints multi-host exec results (text or single-root JSON batch).
///
/// # Errors
/// Serialization or stdout I/O.
pub fn print_exec_batch(
    results: &[HostExecResult],
    max_concurrency: usize,
    json: bool,
) -> io::Result<()> {
    if json {
        let batch_run_id = BatchRunId::new().to_string_canonical();
        let v = ExecBatchJson {
            event: "exec-batch".into(),
            batch_run_id,
            max_concurrency: u32::try_from(max_concurrency).unwrap_or(u32::MAX),
            results: results
                .iter()
                .map(|h| ExecHostJson {
                    name: h.name.clone(),
                    ok: h.ok,
                    exit_code: h.exit_code,
                    stdout: h.stdout.clone(),
                    stderr: h.stderr.clone(),
                    duration_ms: h.duration_ms,
                    error: h.error.clone(),
                })
                .collect(),
        };
        return match json_wire::print_json_line(&v) {
            Ok(()) => Ok(()),
            Err(e) => {
                report_json_serialize_error(&e);
                Err(e)
            }
        };
    }
    if is_quiet() {
        return Ok(());
    }
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    writeln!(
        out,
        "exec --all (max_concurrency={max_concurrency}, hosts={})",
        results.len()
    )?;
    for h in results {
        let status = if h.ok { "ok" } else { "ERR" };
        writeln!(
            out,
            "  {status}  {}  exit={:?}  {}ms",
            h.name, h.exit_code, h.duration_ms
        )?;
        if !h.stdout.is_empty() {
            for line in h.stdout.lines() {
                writeln!(out, "    | {line}")?;
            }
        }
        if !h.stderr.is_empty() {
            for line in h.stderr.lines() {
                writeln!(out, "    ! {line}")?;
            }
        }
    }
    out.flush()
}

/// Prints multi-host SCP batch results.
///
/// # Errors
/// Serialization or stdout I/O.
/// `source` is a parameter rather than a constant because this envelope serves two
/// different designations: the fleet paths (`--all` / `--hosts`) are `selector`, while
/// single-host multi-file (G-PAR-37 / G-PAR-47) reuses the same shape for a host the
/// caller typed. Hardcoding `selector` here would make the second case lie about how
/// its target was chosen — the precise class of silence this field exists to end.
pub fn print_scp_batch(
    direction: &str,
    results: &[crate::scp::HostScpResult],
    max_concurrency: usize,
    json: bool,
    source: json_wire::TargetSource,
) -> io::Result<()> {
    if json {
        let batch_run_id = BatchRunId::new().to_string_canonical();
        let v = ScpBatchJson {
            event: "scp-batch".into(),
            target_source: source,
            host_source: source,
            batch_run_id,
            direction: direction.into(),
            max_concurrency: u32::try_from(max_concurrency).unwrap_or(u32::MAX),
            results: results
                .iter()
                .map(|h| ScpHostJson {
                    name: h.name.clone(),
                    ok: h.ok,
                    bytes: h.bytes,
                    duration_ms: h.duration_ms,
                    local: h.local.clone(),
                    error: h.error.clone(),
                })
                .collect(),
        };
        return match json_wire::print_json_line(&v) {
            Ok(()) => Ok(()),
            Err(e) => {
                report_json_serialize_error(&e);
                Err(e)
            }
        };
    }
    if is_quiet() {
        return Ok(());
    }
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    writeln!(
        out,
        "scp {direction} --all (max_concurrency={max_concurrency}, hosts={})",
        results.len()
    )?;
    for h in results {
        if h.ok {
            writeln!(
                out,
                "  ok  {}  bytes={:?}  {:?}ms",
                h.name, h.bytes, h.duration_ms
            )?;
        } else {
            let err = h.error.as_deref().unwrap_or("error");
            writeln!(out, "  ERR {}  {err}", h.name)?;
        }
    }
    out.flush()
}

/// Prints an SCP transfer result as JSON (GAP-SSH-IO-007 / SCP-021 / IO-009).
///
/// # Errors
/// Serialization or stdout I/O (including BrokenPipe).
///
/// Takes the resolved [`json_wire::ExecTarget`] rather than a bare name so the
/// provenance travels with the host instead of as an eighth parameter: this function
/// already sits at the `clippy::too_many_arguments` ceiling that
/// `gaps_v062_component_budget` refuses to spend another allow on.
pub fn print_transfer_json(
    direction: &str,
    target: &json_wire::ExecTarget,
    local: &str,
    remote: &str,
    result: &crate::ssh::client::TransferResult,
) -> io::Result<()> {
    // GAP-SSH-IO-009: event discriminator (parity with tunnel_listening).
    let v = ScpTransferJson {
        ok: true,
        event: "scp-transfer".into(),
        target: json_wire::TargetEcho::new(target),
        direction: direction.to_string(),
        vps: target.host.clone(),
        local: local.to_string(),
        remote: remote.to_string(),
        bytes: result.bytes_transferred,
        duration_ms: result.duration_ms,
        mtime_preserved: result.mtime_preserved,
        durable: result.durable,
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Prints an SFTP transfer result as JSON (G-SFTP-09).
///
/// # Errors
/// Serialization or stdout I/O.
#[cfg(feature = "ssh-real")]
pub fn print_sftp_transfer_json(
    direction: &str,
    target: &json_wire::ExecTarget,
    local: &str,
    remote: &str,
    bytes: u64,
    duration_ms: u64,
    recursive: bool,
) -> io::Result<()> {
    let v = SftpTransferJson {
        ok: true,
        event: "sftp-transfer".into(),
        target: json_wire::TargetEcho::new(target),
        direction: direction.to_string(),
        vps: target.host.clone(),
        local: local.to_string(),
        remote: remote.to_string(),
        bytes,
        duration_ms,
        recursive,
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Prints `sftp ls` JSON.
///
/// # Errors
/// Serialization or stdout I/O.
#[cfg(feature = "ssh-real")]
pub fn print_sftp_list_json(vps: &str, path: &str, entries: &[SftpListEntry]) -> io::Result<()> {
    let v = SftpListJson {
        ok: true,
        event: "sftp-list".into(),
        vps: vps.to_string(),
        path: path.to_string(),
        entries: entries
            .iter()
            .map(|e| SftpListEntryJson {
                name: e.name.clone(),
                path: e.path.clone(),
                kind: e.kind.clone(),
                size: e.size,
                mode: e.mode,
            })
            .collect(),
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Prints `sftp` fs-op JSON (mkdir/rmdir/rm/rename).
///
/// # Errors
/// Serialization or stdout I/O.
#[cfg(feature = "ssh-real")]
pub fn print_sftp_fs_op_json(
    op: &str,
    vps: &str,
    path: &str,
    to: Option<&str>,
    duration_ms: u64,
) -> io::Result<()> {
    let v = SftpFsOpJson {
        ok: true,
        event: "sftp-fs-op".into(),
        op: op.to_string(),
        vps: vps.to_string(),
        path: path.to_string(),
        to: to.map(str::to_owned),
        duration_ms,
        kind: None,
        size: None,
        mode: None,
        mtime: None,
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Prints `sftp stat` JSON.
///
/// # Errors
/// Serialization or stdout I/O.
#[cfg(feature = "ssh-real")]
pub fn print_sftp_stat_json(vps: &str, st: &SftpStat) -> io::Result<()> {
    let v = SftpFsOpJson {
        ok: true,
        event: "sftp-fs-op".into(),
        op: "stat".into(),
        vps: vps.to_string(),
        path: st.path.clone(),
        to: None,
        duration_ms: 0,
        kind: Some(st.kind.clone()),
        size: st.size,
        mode: st.mode,
        mtime: st.mtime,
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Prints multi-host SFTP batch results.
///
/// # Errors
/// Serialization or stdout I/O.
#[cfg(feature = "ssh-real")]
pub fn print_sftp_batch(
    direction: &str,
    results: &[HostSftpResult],
    max_concurrency: usize,
    json: bool,
) -> io::Result<()> {
    if json {
        let batch_run_id = BatchRunId::new().to_string_canonical();
        let v = SftpBatchJson {
            event: "sftp-batch".into(),
            // See `print_scp_batch`: a batch is by construction selector-produced.
            target_source: json_wire::TargetSource::Selector,
            host_source: json_wire::TargetSource::Selector,
            batch_run_id,
            direction: direction.to_string(),
            max_concurrency: u32::try_from(max_concurrency).unwrap_or(u32::MAX),
            results: results
                .iter()
                .map(|h| ScpHostJson {
                    name: h.name.clone(),
                    ok: h.ok,
                    bytes: h.bytes,
                    duration_ms: h.duration_ms,
                    local: h.local.clone(),
                    error: h.error.clone(),
                })
                .collect(),
        };
        return match json_wire::print_json_line(&v) {
            Ok(()) => Ok(()),
            Err(e) => {
                report_json_serialize_error(&e);
                Err(e)
            }
        };
    }
    if is_quiet() {
        return Ok(());
    }
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    writeln!(
        out,
        "sftp {direction} --all (max_concurrency={max_concurrency}, hosts={})",
        results.len()
    )?;
    for h in results {
        if h.ok {
            writeln!(
                out,
                "  ok  {}  bytes={:?}  ms={:?}",
                h.name, h.bytes, h.duration_ms
            )?;
        } else {
            let err = h.error.as_deref().unwrap_or("error");
            writeln!(out, "  ERR {}  {err}", h.name)?;
        }
    }
    out.flush()
}

/// Builds the `tunnel_listening` payload without writing it anywhere.
///
/// Split out from the printer so the document can be asserted on. Every field of this
/// event was previously reachable only by driving a real listener and reading stdout,
/// which is why `bind` — added by G-TUN-R06 precisely so an agent could audit whether a
/// service had been published beyond loopback — shipped covered by nothing but a schema
/// file and a mention in prose.
#[must_use]
pub fn build_tunnel_listening(
    vps: &str,
    local_port: u16,
    remote_host: &str,
    remote_port: u16,
    timeout_ms: u64,
    bind: &str,
    mode: &str,
) -> TunnelListeningJson {
    TunnelListeningJson {
        ok: true,
        event: "tunnel_listening".into(),
        vps: vps.to_string(),
        local_port,
        remote_host: remote_host.to_string(),
        remote_port,
        timeout_ms,
        bind: bind.to_string(),
        mode: mode.to_string(),
    }
}

/// JSON event when the local tunnel listener comes up (GAP-SSH-IO-008).
///
/// # Errors
/// Serialization or stdout I/O (including BrokenPipe).
pub fn print_tunnel_listening_json(
    vps: &str,
    local_port: u16,
    remote_host: &str,
    remote_port: u16,
    timeout_ms: u64,
    bind: &str,
    mode: &str,
) -> io::Result<()> {
    let v = build_tunnel_listening(
        vps,
        local_port,
        remote_host,
        remote_port,
        timeout_ms,
        bind,
        mode,
    );
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}

/// Builds the `tunnel_closed` payload without writing it anywhere.
///
/// The whole event — `reason`, `forwards_served`, `capacity_waits`, `ok` — used to be
/// constructed inside the printer, so nothing could inspect it. The 0.5.4 audit found
/// the three field names appearing in exactly one place outside the emitter: a
/// documentation test asserting the CHANGELOG mentions them. Deleting the emission
/// would not have turned the suite red, which is the same failure mode G-QA-R01 was
/// written to stop.
#[must_use]
pub fn build_tunnel_closed(input: TunnelClosedInput<'_>) -> TunnelClosedJson {
    TunnelClosedJson {
        // An accept-error shutdown is not a clean lifetime, even though the process
        // still exits 0 for having bound successfully.
        ok: !matches!(input.reason, TunnelCloseReason::AcceptError),
        event: "tunnel_closed".into(),
        vps: input.vps.to_string(),
        reason: input.reason,
        bind: input.bind.to_string(),
        local_port: input.local_port,
        forwards_served: input.forwards_served,
        capacity_waits: input.capacity_waits,
        duration_ms: input.duration_ms,
        mode: input.mode.to_string(),
    }
}

/// Inputs for [`build_tunnel_closed`].
///
/// B3: three of the eight fields are bare `u64` counters and one is a `u16`
/// port. Passed positionally, swapping `forwards_served` with `capacity_waits`
/// compiles and produces a plausible-looking event that misreports the tunnel's
/// lifetime — the exact class of silent wrongness the suppressed
/// `too_many_arguments` lint was pointing at.
pub struct TunnelClosedInput<'a> {
    /// Registry name of the relay host.
    pub vps: &'a str,
    /// Why the tunnel stopped.
    pub reason: TunnelCloseReason,
    /// Effective local bind address.
    pub bind: &'a str,
    /// Effective local port (OS-assigned when `0` was requested).
    pub local_port: u16,
    /// Connections accepted over the tunnel's lifetime.
    pub forwards_served: u64,
    /// Times an accept waited on the concurrency semaphore.
    pub capacity_waits: u64,
    /// Wall lifetime in milliseconds.
    pub duration_ms: u64,
    /// Tunnel mode label (`local`, `reverse`, `socks5`, `streamlocal`).
    pub mode: &'a str,
}

/// Emits the `tunnel_closed` shutdown event (G-TUN-R07).
///
/// Always emitted, including on the happy deadline path, so an agent can tell the
/// three endings apart instead of inferring them from a shared exit 0.
///
/// # Errors
/// Serialization or stdout I/O (including BrokenPipe).
pub fn print_tunnel_closed_json(event: &TunnelClosedJson) -> io::Result<()> {
    match json_wire::print_json_line(event) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}