ssh-cli 0.5.2

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
// 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, SftpBatchJson, SftpFsOpJson, SftpListEntryJson, SftpListJson, SftpTransferJson,
    TunnelListeningJson,
};
use crate::ssh::sftp_types::{SftpListEntry, SftpStat};
use crate::sftp::batch::HostSftpResult;
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.
pub fn print_scp_batch(
    direction: &str,
    results: &[crate::scp::HostScpResult],
    max_concurrency: usize,
    json: bool,
) -> io::Result<()> {
    if json {
        let batch_run_id = BatchRunId::new().to_string_canonical();
        let v = ScpBatchJson {
            event: "scp-batch".into(),
            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).
pub fn print_transfer_json(
    direction: &str,
    vps: &str,
    local: &str,
    remote: &str,
    bytes: u64,
    duration_ms: u64,
) -> io::Result<()> {
    // GAP-SSH-IO-009: event discriminator (parity with tunnel_listening).
    let v = ScpTransferJson {
        ok: true,
        event: "scp-transfer".into(),
        direction: direction.to_string(),
        vps: vps.to_string(),
        local: local.to_string(),
        remote: remote.to_string(),
        bytes,
        duration_ms,
    };
    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.
pub fn print_sftp_transfer_json(
    direction: &str,
    vps: &str,
    local: &str,
    remote: &str,
    bytes: u64,
    duration_ms: u64,
    recursive: bool,
) -> io::Result<()> {
    let v = SftpTransferJson {
        ok: true,
        event: "sftp-transfer".into(),
        direction: direction.to_string(),
        vps: vps.to_string(),
        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.
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.
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.
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.
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(),
            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()
}

/// 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,
) -> io::Result<()> {
    let v = TunnelListeningJson {
        ok: true,
        event: "tunnel_listening".into(),
        vps: vps.to_string(),
        local_port,
        remote_host: remote_host.to_string(),
        remote_port,
        timeout_ms,
    };
    match json_wire::print_json_line(&v) {
        Ok(()) => Ok(()),
        Err(e) => {
            report_json_serialize_error(&e);
            Err(e)
        }
    }
}