ssh-cli 0.4.1

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
//! Transferência de arquivos via SCP sobre SSH (one-shot).
//!
//! Wrapper que usa os métodos `upload` e `download` do [`ClienteSsh`].
//! Somente arquivos regulares (sem `-r` / sem SFTP subsystem).

use crate::cli::AcaoScp;
use crate::erros::ErroSshCli;
use crate::i18n::{self, Mensagem};
use crate::output;
use crate::ssh::cliente::{ClienteSsh, ClienteSshTrait};
use crate::vps;
use std::path::PathBuf;

/// Overrides de runtime para o subcomando `scp` (paridade com exec).
#[derive(Debug, Default, Clone)]
pub struct OpcoesScp {
    /// Senha SSH (já resolvida de flag ou stdin).
    pub password: Option<String>,
    /// Caminho da chave privada.
    pub key: Option<String>,
    /// Passphrase da chave (já resolvida).
    pub key_passphrase: Option<String>,
    /// Timeout total connect+transfer em ms.
    pub timeout: Option<u64>,
    /// Substitui host key divergente (global `--replace-host-key`).
    pub replace_host_key: bool,
    /// Emite JSON de sucesso (flag local ou formato global).
    pub json: bool,
}

/// Executa o subcomando SCP (upload/download).
pub async fn executar_scp(
    acao: AcaoScp,
    config_override: Option<PathBuf>,
    opts: OpcoesScp,
) -> anyhow::Result<()> {
    if crate::signals::cancelado() {
        return Err(anyhow::anyhow!(i18n::t(Mensagem::OperacaoCancelada)));
    }

    match acao {
        AcaoScp::Upload {
            vps_nome,
            local,
            remote,
            ..
        } => {
            // GAP-SSH-SCP-001 / SCP-019: validar arquivo local antes do connect.
            if local.is_dir() {
                return Err(ErroSshCli::ArgumentoInvalido(i18n::t(
                    Mensagem::ScpUploadSomenteArquivo,
                ))
                .into());
            }
            if !local.is_file() {
                return Err(ErroSshCli::ArquivoNaoEncontrado(local.display().to_string()).into());
            }

            let mut registro = vps::buscar_por_nome(config_override.clone(), &vps_nome)?
                .ok_or_else(|| ErroSshCli::VpsNaoEncontrada(vps_nome.clone()))?;

            aplicar_opcoes_scp(&mut registro, &opts);

            let caminho = crate::vps::resolver_caminho_config(config_override.clone())?;
            let cfg = crate::vps::construir_configuracao(
                &registro,
                Some(&caminho),
                opts.replace_host_key,
            );

            let cliente: Box<dyn ClienteSshTrait> =
                <ClienteSsh as ClienteSshTrait>::conectar(cfg).await?;
            executar_scp_upload_with_client(&vps_nome, &local, &remote, cliente, opts.json).await?;
        }
        AcaoScp::Download {
            vps_nome,
            remote,
            local,
            ..
        } => {
            if local.is_dir() {
                return Err(ErroSshCli::ArgumentoInvalido(i18n::t(
                    Mensagem::ScpDownloadLocalNaoDiretorio,
                ))
                .into());
            }

            let mut registro = vps::buscar_por_nome(config_override.clone(), &vps_nome)?
                .ok_or_else(|| ErroSshCli::VpsNaoEncontrada(vps_nome.clone()))?;

            aplicar_opcoes_scp(&mut registro, &opts);

            let caminho = crate::vps::resolver_caminho_config(config_override.clone())?;
            let cfg = crate::vps::construir_configuracao(
                &registro,
                Some(&caminho),
                opts.replace_host_key,
            );

            let cliente: Box<dyn ClienteSshTrait> =
                <ClienteSsh as ClienteSshTrait>::conectar(cfg).await?;
            executar_scp_download_with_client(&vps_nome, &remote, &local, cliente, opts.json)
                .await?;
        }
    }
    Ok(())
}

fn aplicar_opcoes_scp(registro: &mut crate::vps::modelo::VpsRegistro, opts: &OpcoesScp) {
    if let Some(ref pwd) = opts.password {
        registro.senha = secrecy::SecretString::from(pwd.clone());
    }
    if let Some(ref k) = opts.key {
        registro.key_path = Some(k.clone());
    }
    if let Some(ref kp) = opts.key_passphrase {
        registro.key_passphrase = Some(secrecy::SecretString::from(kp.clone()));
    }
    if let Some(t) = opts.timeout {
        registro.timeout_ms = t;
    }
}

/// Versão testável de upload SCP que aceita o cliente como parâmetro.
pub async fn executar_scp_upload_with_client(
    vps_nome: &str,
    local: &std::path::Path,
    remote: &std::path::Path,
    mut cliente: Box<dyn ClienteSshTrait>,
    json: bool,
) -> anyhow::Result<()> {
    let resultado = cliente.upload(local, remote).await?;
    cliente.desconectar().await?;
    if json {
        output::imprimir_transferencia_json(
            "upload",
            vps_nome,
            &local.display().to_string(),
            &remote.display().to_string(),
            resultado.bytes_transferidos,
            resultado.duracao_ms,
        );
    } else {
        output::imprimir_sucesso(&i18n::t(Mensagem::ScpUploadConcluido {
            bytes: resultado.bytes_transferidos,
            ms: resultado.duracao_ms,
        }));
    }
    Ok(())
}

/// Versão testável de download SCP que aceita o cliente como parâmetro.
pub async fn executar_scp_download_with_client(
    vps_nome: &str,
    remote: &std::path::Path,
    local: &std::path::Path,
    mut cliente: Box<dyn ClienteSshTrait>,
    json: bool,
) -> anyhow::Result<()> {
    let resultado = cliente.download(remote, local).await?;
    cliente.desconectar().await?;
    if json {
        output::imprimir_transferencia_json(
            "download",
            vps_nome,
            &local.display().to_string(),
            &remote.display().to_string(),
            resultado.bytes_transferidos,
            resultado.duracao_ms,
        );
    } else {
        output::imprimir_sucesso(&i18n::t(Mensagem::ScpDownloadConcluido {
            bytes: resultado.bytes_transferidos,
            ms: resultado.duracao_ms,
        }));
    }
    Ok(())
}

#[cfg(test)]
mod testes {
    use super::*;
    use crate::erros::ErroSshCli;
    use crate::ssh::cliente::{
        CanalTunel, ConfiguracaoConexao, SaidaExecucao, TransferenciaResultado,
    };
    use crate::vps::modelo::{VpsRegistro, SCHEMA_VERSION_ATUAL};
    use crate::vps::{self, ArquivoConfig};
    use async_trait::async_trait;
    use secrecy::SecretString;
    use serial_test::serial;
    use std::collections::BTreeMap;
    use std::path::Path;
    use tempfile::TempDir;

    struct ClienteFakeScp {
        upload_ok: bool,
        download_ok: bool,
        bytes_upload: u64,
        bytes_download: u64,
    }

    #[async_trait]
    impl ClienteSshTrait for ClienteFakeScp {
        async fn conectar(_cfg: ConfiguracaoConexao) -> Result<Box<Self>, ErroSshCli> {
            Err(ErroSshCli::ConexaoFalhou(
                "não implementado em teste".to_string(),
            ))
        }

        async fn executar_comando(
            &mut self,
            _cmd: &str,
            _max_chars: usize,
            _stdin_data: Option<Vec<u8>>,
        ) -> Result<SaidaExecucao, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "não implementado em teste".to_string(),
            ))
        }

        async fn upload(
            &mut self,
            _local: &Path,
            _remote: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            if self.upload_ok {
                Ok(TransferenciaResultado {
                    bytes_transferidos: self.bytes_upload,
                    duracao_ms: 10,
                })
            } else {
                Err(ErroSshCli::CanalFalhou("upload falhou".to_string()))
            }
        }

        async fn download(
            &mut self,
            _remote: &Path,
            _local: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            if self.download_ok {
                Ok(TransferenciaResultado {
                    bytes_transferidos: self.bytes_download,
                    duracao_ms: 20,
                })
            } else {
                Err(ErroSshCli::CanalFalhou("download falhou".to_string()))
            }
        }

        async fn abrir_canal_tunel(
            &self,
            _host_remoto: &str,
            _porta_remota: u16,
            _endereco_origem: &str,
            _porta_origem: u16,
        ) -> Result<Box<dyn CanalTunel>, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "não implementado em teste".to_string(),
            ))
        }

        async fn desconectar(&self) -> Result<(), ErroSshCli> {
            Ok(())
        }
    }

    fn registro_teste(nome: &str) -> VpsRegistro {
        VpsRegistro::novo(
            nome.to_string(),
            "127.0.0.1".to_string(),
            1,
            "root".to_string(),
            SecretString::from("senha-teste".to_string()),
            None,
            None,
            Some(100),
            Some(1000),
            Some(1000),
            None,
            None,
            false,
        )
    }

    fn salvar_config_com_vps(tmp: &TempDir, nome: &str) {
        let mut hosts = BTreeMap::new();
        hosts.insert(nome.to_string(), registro_teste(nome));
        let arquivo = ArquivoConfig {
            schema_version: SCHEMA_VERSION_ATUAL,
            hosts,
        };
        let caminho = tmp.path().join("config.toml");
        vps::salvar(&caminho, &arquivo).expect("salvar config teste");
    }

    #[tokio::test]
    async fn executar_scp_upload_with_client_retorna_ok() {
        let cliente = Box::new(ClienteFakeScp {
            upload_ok: true,
            download_ok: true,
            bytes_upload: 128,
            bytes_download: 0,
        });
        let local = Path::new("/tmp/local.txt");
        let remote = Path::new("/tmp/remote.txt");
        let resultado = executar_scp_upload_with_client("v1", local, remote, cliente, false).await;
        assert!(resultado.is_ok());
    }

    #[tokio::test]
    async fn executar_scp_download_with_client_retorna_ok() {
        let cliente = Box::new(ClienteFakeScp {
            upload_ok: true,
            download_ok: true,
            bytes_upload: 0,
            bytes_download: 256,
        });
        let resultado = executar_scp_download_with_client(
            "v1",
            Path::new("/tmp/remote.txt"),
            Path::new("/tmp/local.txt"),
            cliente,
            false,
        )
        .await;
        assert!(resultado.is_ok());
    }

    #[tokio::test]
    async fn executar_scp_upload_with_client_retorna_erro() {
        let cliente = Box::new(ClienteFakeScp {
            upload_ok: false,
            download_ok: true,
            bytes_upload: 0,
            bytes_download: 0,
        });
        let resultado = executar_scp_upload_with_client(
            "v1",
            Path::new("/tmp/local.txt"),
            Path::new("/tmp/remote.txt"),
            cliente,
            false,
        )
        .await;
        assert!(resultado.is_err());
    }

    #[tokio::test]
    async fn executar_scp_download_with_client_retorna_erro() {
        let cliente = Box::new(ClienteFakeScp {
            upload_ok: true,
            download_ok: false,
            bytes_upload: 0,
            bytes_download: 0,
        });
        let resultado = executar_scp_download_with_client(
            "v1",
            Path::new("/tmp/remote.txt"),
            Path::new("/tmp/local.txt"),
            cliente,
            false,
        )
        .await;
        assert!(resultado.is_err());
    }

    #[tokio::test]
    #[serial]
    async fn executar_scp_upload_tenta_conectar_quando_vps_existe() {
        let tmp = TempDir::new().unwrap();
        salvar_config_com_vps(&tmp, "vps-upload");
        let local = tmp.path().join("local.bin");
        std::fs::write(&local, b"abc").unwrap();
        let acao = AcaoScp::Upload {
            vps_nome: "vps-upload".to_string(),
            local,
            remote: PathBuf::from("/tmp/x"),
            password: None,
            password_stdin: false,
            key: None,
            key_passphrase: None,
            key_passphrase_stdin: false,
            timeout: Some(100),
            json: false,
        };
        let r = executar_scp(
            acao,
            Some(tmp.path().to_path_buf()),
            OpcoesScp {
                timeout: Some(100),
                ..Default::default()
            },
        )
        .await;
        // Conexão a 127.0.0.1:1 deve falhar (sem hang longo por timeout 100ms).
        assert!(r.is_err());
    }

    #[tokio::test]
    #[serial]
    async fn executar_scp_download_tenta_conectar_quando_vps_existe() {
        let tmp = TempDir::new().unwrap();
        salvar_config_com_vps(&tmp, "vps-download");
        let acao = AcaoScp::Download {
            vps_nome: "vps-download".to_string(),
            remote: PathBuf::from("/tmp/x"),
            local: tmp.path().join("out.bin"),
            password: None,
            password_stdin: false,
            key: None,
            key_passphrase: None,
            key_passphrase_stdin: false,
            timeout: Some(100),
            json: false,
        };
        let r = executar_scp(
            acao,
            Some(tmp.path().to_path_buf()),
            OpcoesScp {
                timeout: Some(100),
                ..Default::default()
            },
        )
        .await;
        assert!(r.is_err());
    }

    #[tokio::test]
    #[serial]
    async fn executar_scp_upload_rejeita_diretorio() {
        let tmp = TempDir::new().unwrap();
        salvar_config_com_vps(&tmp, "vps-dir");
        let acao = AcaoScp::Upload {
            vps_nome: "vps-dir".to_string(),
            local: tmp.path().to_path_buf(),
            remote: PathBuf::from("/tmp/x"),
            password: None,
            password_stdin: false,
            key: None,
            key_passphrase: None,
            key_passphrase_stdin: false,
            timeout: None,
            json: false,
        };
        let r = executar_scp(acao, Some(tmp.path().to_path_buf()), OpcoesScp::default()).await;
        assert!(r.is_err());
        let msg = format!("{:?}", r.err().unwrap());
        assert!(
            msg.contains("regular files")
                || msg.contains("arquivos regulares")
                || msg.contains("ArgumentoInvalido"),
            "msg={msg}"
        );
    }
}