ssh-cli 0.1.0

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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Definição de argumentos CLI via `clap` derive e dispatcher.
//!
//! O ssh-cli MVP tem os seguintes modos de operação:
//!
//! 1. **CRUD de VPS** — `ssh-cli vps add|list|remove|edit|show|path`.
//! 2. **Seleção de ativa** — `ssh-cli connect <NOME>` (grava em `config.toml.active`).
//! 3. **Execução remota** — `ssh-cli exec|sudo-exec|scp|tunnel`.
//! 4. **Health check** — `ssh-cli health-check [VPS]`.
//! 5. **Completions** — `ssh-cli completions <SHELL>`.
//!
//! ZERO arquivo `.env`. Toda configuração é gerenciada via comandos explícitos.

use anyhow::Result;
use clap::{Parser, Subcommand};
use clap_complete::Shell;
use std::path::PathBuf;

/// Formato de saída suportado pela CLI.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, clap::ValueEnum)]
pub enum FormatoSaida {
    /// Texto legível por humanos (padrão).
    #[default]
    Text,
    /// JSON estruturado.
    Json,
}

/// Argumentos globais do ssh-cli.
#[derive(Debug, Parser)]
#[command(
    name = "ssh-cli",
    version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("SSH_CLI_COMMIT_HASH"), ")"),
    about = "CLI Rust para LLMs operarem servidores via SSH.",
    long_about = None,
)]
pub struct Argumentos {
    /// Força o idioma da CLI (ex.: `pt-BR`, `en-US`).
    #[arg(long, global = true, value_name = "LOCALE")]
    pub lang: Option<String>,

    /// Aumenta a verbosidade de logs em stderr.
    #[arg(short, long, global = true)]
    pub verbose: bool,

    /// Suprime output não-JSON (modo silencioso).
    #[arg(short, long, global = true)]
    pub quiet: bool,

    /// Override do diretório de configuração (útil para testes).
    #[arg(long, global = true, value_name = "DIR")]
    pub config_dir: Option<PathBuf>,

    /// Desativa cores no output.
    #[arg(long, global = true)]
    pub no_color: bool,

    /// Formato global de saída (text, json).
    #[arg(long, global = true, value_enum, default_value_t = FormatoSaida::Text)]
    pub output_format: FormatoSaida,

    /// Subcomando a executar.
    #[command(subcommand)]
    pub comando: Comando,
}

/// Subcomandos de primeiro nível.
#[derive(Debug, Subcommand)]
pub enum Comando {
    /// Gerencia VPSs cadastradas (add, list, remove, edit, show, path).
    Vps {
        /// Ação específica do CRUD de VPS.
        #[command(subcommand)]
        acao: AcaoVps,
    },

    /// Define a VPS ativa (grava `active = "<NOME>"` no `config.toml`).
    Connect {
        /// Nome da VPS previamente adicionada via `vps add`.
        nome: String,
    },

    /// Executa um comando na VPS via SSH (stdout/stderr capturados).
    Exec {
        /// Nome da VPS previamente adicionada via `vps add`.
        vps_nome: String,

        /// Comando shell a executar.
        comando: String,

        /// Saída em JSON.
        #[arg(long)]
        json: bool,
    },

    /// Executa um comando com `sudo` na VPS via SSH.
    SudoExec {
        /// Nome da VPS previamente adicionada via `vps add`.
        vps_nome: String,

        /// Comando shell a executar com privilégios sudo.
        comando: String,

        /// Saída em JSON.
        #[arg(long)]
        json: bool,
    },

    /// Transferência de arquivos via SCP (upload/download).
    Scp {
        /// Ação específica do SCP.
        #[command(subcommand)]
        acao: AcaoScp,
    },

    /// Cria um tunnel SSH (port-forward local).
    Tunnel {
        /// Nome da VPS previamente adicionada via `vps add`.
        vps_nome: String,

        /// Porta local para escuta (ex.: 8080).
        porta_local: u16,

        /// Host remoto accesible via SSH (ex.: 127.0.0.1).
        host_remoto: String,

        /// Porta remota (ex.: 5432).
        porta_remota: u16,
    },

    /// Verifica conectividade SSH com uma VPS.
    HealthCheck {
        /// Nome da VPS a verificar (usa VPS ativa se omitido).
        vps_nome: Option<String>,
    },

    /// Gera completions de shell (bash, zsh, fish, powershell, elvish).
    Completions {
        /// Shell para gerar completions.
        #[arg(value_enum)]
        shell: Shell,
    },
}

/// Ações do subcomando `vps`.
#[derive(Debug, Subcommand)]
pub enum AcaoVps {
    /// Adiciona uma nova VPS ao registro.
    Add {
        /// Nome único da VPS.
        #[arg(long)]
        name: String,

        /// Hostname ou IP.
        #[arg(long)]
        host: String,

        /// Porta SSH.
        #[arg(long, default_value_t = 22)]
        port: u16,

        /// Usuário SSH.
        #[arg(long)]
        user: String,

        /// Senha SSH.
        #[arg(long)]
        password: Option<String>,

        /// Timeout em milissegundos para comandos.
        #[arg(long, default_value_t = 30_000)]
        timeout: u64,

        /// Limite de caracteres por output (`"none"` ou `"0"` = ilimitado).
        #[arg(long, default_value = "100000")]
        max_chars: String,

        /// Senha para `sudo`.
        #[arg(long)]
        sudo_password: Option<String>,

        /// Senha para `su -`.
        #[arg(long)]
        su_password: Option<String>,
    },

    /// Lista todas as VPSs (senhas mascaradas).
    List {
        /// Saída em JSON (útil para pipes).
        #[arg(long)]
        json: bool,
    },

    /// Remove uma VPS do registro.
    Remove {
        /// Nome da VPS a remover.
        nome: String,
    },

    /// Edita campos de uma VPS existente.
    Edit {
        /// Nome da VPS a editar.
        nome: String,

        /// Novo hostname/IP.
        #[arg(long)]
        host: Option<String>,

        /// Nova porta SSH.
        #[arg(long)]
        port: Option<u16>,

        /// Novo usuário.
        #[arg(long)]
        user: Option<String>,

        /// Nova senha.
        #[arg(long)]
        password: Option<String>,

        /// Novo timeout.
        #[arg(long)]
        timeout: Option<u64>,

        /// Novo limite de caracteres.
        #[arg(long)]
        max_chars: Option<String>,

        /// Nova senha sudo.
        #[arg(long)]
        sudo_password: Option<String>,

        /// Nova senha su.
        #[arg(long)]
        su_password: Option<String>,
    },

    /// Exibe detalhes de uma VPS (senhas mascaradas).
    Show {
        /// Nome da VPS a exibir.
        nome: String,

        /// Saída em JSON.
        #[arg(long)]
        json: bool,
    },

    /// Exibe o caminho do arquivo de configuração.
    Path,
}

/// Ações do subcomando `scp`.
#[derive(Debug, Subcommand)]
pub enum AcaoScp {
    /// Upload de arquivo local para remote.
    Upload {
        /// Nome da VPS previamente adicionada via `vps add`.
        vps_nome: String,

        /// Caminho do arquivo local a enviar.
        local: PathBuf,

        /// Caminho destino no servidor remote.
        remote: PathBuf,
    },

    /// Download de arquivo remote para local.
    Download {
        /// Nome da VPS previamente adicionada via `vps add`.
        vps_nome: String,

        /// Caminho do arquivo no servidor remote.
        remote: PathBuf,

        /// Caminho local de destino.
        local: PathBuf,
    },
}

/// Faz parsing dos argumentos da CLI.
#[must_use]
pub fn parse_args() -> Argumentos {
    Argumentos::parse()
}

/// Inicializa `tracing-subscriber`. Precedência: `RUST_LOG` > `--verbose` > `--quiet` > `info`.
pub fn inicializar_logs(args: &Argumentos) {
    use tracing_subscriber::{fmt, EnvFilter};

    let filter = if std::env::var("RUST_LOG").is_ok() {
        EnvFilter::from_default_env()
    } else if args.verbose {
        EnvFilter::new("debug")
    } else if args.quiet {
        EnvFilter::new("error")
    } else {
        EnvFilter::new("info")
    };

    let _ = fmt()
        .with_env_filter(filter)
        .with_writer(std::io::stderr)
        .with_target(false)
        .with_ansi(false)
        .try_init();
}

/// Gera completions de shell para stdout.
pub fn gerar_completions(shell: Shell) {
    use clap::CommandFactory;
    let mut cmd = Argumentos::command();
    clap_complete::generate(shell, &mut cmd, "ssh-cli", &mut std::io::stdout());
}

/// Executa o subcomando solicitado.
pub async fn executar(args: Argumentos) -> Result<()> {
    let config_override = args.config_dir.clone();
    let formato = args.output_format;

    match args.comando {
        Comando::Vps { acao } => {
            crate::vps::executar_comando_vps(acao, config_override, formato).await
        }
        Comando::Connect { nome } => crate::vps::executar_connect(&nome, config_override).await,
        Comando::Exec {
            vps_nome,
            comando,
            json,
        } => crate::vps::executar_exec(&vps_nome, &comando, config_override, formato, json).await,
        Comando::SudoExec {
            vps_nome,
            comando,
            json,
        } => {
            crate::vps::executar_sudo_exec(&vps_nome, &comando, config_override, formato, json)
                .await
        }
        Comando::Scp { acao } => crate::scp::executar_scp(acao, config_override).await,
        Comando::Tunnel {
            vps_nome,
            porta_local,
            host_remoto,
            porta_remota,
        } => {
            crate::tunnel::executar_tunnel(
                &vps_nome,
                porta_local,
                &host_remoto,
                porta_remota,
                config_override,
            )
            .await
        }
        Comando::HealthCheck { vps_nome } => {
            crate::vps::executar_health_check(vps_nome.as_deref(), config_override, formato).await
        }
        Comando::Completions { shell } => {
            gerar_completions(shell);
            Ok(())
        }
    }
}

#[cfg(test)]
mod testes {
    use super::*;
    use clap::Parser;
    use serial_test::serial;
    use tempfile::TempDir;

    fn argumentos_teste(comando: Comando, config_dir: Option<PathBuf>) -> Argumentos {
        Argumentos {
            lang: None,
            verbose: false,
            quiet: false,
            config_dir,
            no_color: false,
            output_format: FormatoSaida::Text,
            comando,
        }
    }

    #[test]
    fn parser_entende_tunnel() {
        let args =
            Argumentos::try_parse_from(["ssh-cli", "tunnel", "vps-a", "8080", "127.0.0.1", "5432"])
                .expect("parser deve aceitar subcomando tunnel");

        match args.comando {
            Comando::Tunnel {
                vps_nome,
                porta_local,
                host_remoto,
                porta_remota,
            } => {
                assert_eq!(vps_nome, "vps-a");
                assert_eq!(porta_local, 8080);
                assert_eq!(host_remoto, "127.0.0.1");
                assert_eq!(porta_remota, 5432);
            }
            outro => panic!("comando inesperado: {outro:?}"),
        }
    }

    #[test]
    fn parser_entende_scp_upload() {
        let args = Argumentos::try_parse_from([
            "ssh-cli",
            "scp",
            "upload",
            "vps-a",
            "./arquivo-local.txt",
            "/tmp/arquivo-remoto.txt",
        ])
        .expect("parser deve aceitar scp upload");

        match args.comando {
            Comando::Scp {
                acao:
                    AcaoScp::Upload {
                        vps_nome,
                        local,
                        remote,
                    },
            } => {
                assert_eq!(vps_nome, "vps-a");
                assert_eq!(local, PathBuf::from("./arquivo-local.txt"));
                assert_eq!(remote, PathBuf::from("/tmp/arquivo-remoto.txt"));
            }
            outro => panic!("comando inesperado: {outro:?}"),
        }
    }

    #[test]
    #[serial]
    fn inicializar_logs_sem_panic_com_rust_log_definido() {
        std::env::set_var("RUST_LOG", "trace");
        let args = argumentos_teste(
            Comando::Connect {
                nome: "vps-a".to_string(),
            },
            None,
        );
        inicializar_logs(&args);
        std::env::remove_var("RUST_LOG");
    }

    #[test]
    #[serial]
    fn inicializar_logs_sem_panic_com_verbose() {
        std::env::remove_var("RUST_LOG");
        let mut args = argumentos_teste(
            Comando::Connect {
                nome: "vps-a".to_string(),
            },
            None,
        );
        args.verbose = true;
        inicializar_logs(&args);
    }

    #[test]
    #[serial]
    fn inicializar_logs_sem_panic_com_quiet() {
        std::env::remove_var("RUST_LOG");
        let mut args = argumentos_teste(
            Comando::Connect {
                nome: "vps-a".to_string(),
            },
            None,
        );
        args.quiet = true;
        inicializar_logs(&args);
    }

    #[test]
    #[serial]
    fn inicializar_logs_sem_panic_no_padrao_info() {
        std::env::remove_var("RUST_LOG");
        let args = argumentos_teste(
            Comando::Connect {
                nome: "vps-a".to_string(),
            },
            None,
        );
        inicializar_logs(&args);
    }

    #[tokio::test]
    async fn executar_branch_exec_retorna_erro_para_vps_inexistente() {
        let tmp = TempDir::new().expect("tempdir");
        let args = argumentos_teste(
            Comando::Exec {
                vps_nome: "inexistente".to_string(),
                comando: "echo ok".to_string(),
                json: false,
            },
            Some(tmp.path().to_path_buf()),
        );

        let resultado = executar(args).await;
        assert!(resultado.is_err());
    }

    #[tokio::test]
    async fn executar_branch_sudo_exec_retorna_erro_para_vps_inexistente() {
        let tmp = TempDir::new().expect("tempdir");
        let args = argumentos_teste(
            Comando::SudoExec {
                vps_nome: "inexistente".to_string(),
                comando: "id".to_string(),
                json: false,
            },
            Some(tmp.path().to_path_buf()),
        );

        let resultado = executar(args).await;
        assert!(resultado.is_err());
    }

    #[tokio::test]
    async fn executar_branch_scp_retorna_erro_para_vps_inexistente() {
        let tmp = TempDir::new().expect("tempdir");
        let args = argumentos_teste(
            Comando::Scp {
                acao: AcaoScp::Upload {
                    vps_nome: "inexistente".to_string(),
                    local: PathBuf::from("./arquivo-local.txt"),
                    remote: PathBuf::from("/tmp/arquivo-remoto.txt"),
                },
            },
            Some(tmp.path().to_path_buf()),
        );

        let resultado = executar(args).await;
        assert!(resultado.is_err());
    }

    #[tokio::test]
    async fn executar_branch_tunnel_retorna_erro_para_vps_inexistente() {
        let tmp = TempDir::new().expect("tempdir");
        let args = argumentos_teste(
            Comando::Tunnel {
                vps_nome: "inexistente".to_string(),
                porta_local: 38080,
                host_remoto: "127.0.0.1".to_string(),
                porta_remota: 5432,
            },
            Some(tmp.path().to_path_buf()),
        );

        let resultado = executar(args).await;
        assert!(resultado.is_err());
    }

    #[test]
    fn test_parse_no_color() {
        let args = Argumentos::try_parse_from(["ssh-cli", "--no-color", "vps", "list"])
            .expect("parser deve aceitar --no-color");
        assert!(args.no_color);
    }

    #[test]
    fn test_parse_output_format_json() {
        let args =
            Argumentos::try_parse_from(["ssh-cli", "--output-format", "json", "vps", "list"])
                .expect("parser deve aceitar --output-format json");
        assert_eq!(args.output_format, FormatoSaida::Json);
    }

    #[test]
    fn test_parse_output_format_default() {
        let args = Argumentos::try_parse_from(["ssh-cli", "vps", "list"])
            .expect("parser deve aceitar subcomando sem output-format");
        assert_eq!(args.output_format, FormatoSaida::Text);
    }

    #[test]
    fn test_parse_completions_bash() {
        let args = Argumentos::try_parse_from(["ssh-cli", "completions", "bash"])
            .expect("parser deve aceitar completions bash");
        assert!(matches!(
            args.comando,
            Comando::Completions { shell: Shell::Bash }
        ));
    }

    #[test]
    fn test_parse_health_check_com_nome() {
        let args = Argumentos::try_parse_from(["ssh-cli", "health-check", "meu-vps"])
            .expect("parser deve aceitar health-check com nome");
        match args.comando {
            Comando::HealthCheck { vps_nome } => {
                assert_eq!(vps_nome, Some("meu-vps".to_string()));
            }
            outro => panic!("comando inesperado: {outro:?}"),
        }
    }

    #[test]
    fn test_parse_health_check_sem_nome() {
        let args = Argumentos::try_parse_from(["ssh-cli", "health-check"])
            .expect("parser deve aceitar health-check sem nome");
        match args.comando {
            Comando::HealthCheck { vps_nome } => {
                assert!(vps_nome.is_none());
            }
            outro => panic!("comando inesperado: {outro:?}"),
        }
    }

    #[test]
    fn test_parse_exec_json() {
        let args = Argumentos::try_parse_from(["ssh-cli", "exec", "vps1", "ls", "--json"])
            .expect("parser deve aceitar exec com --json");
        match args.comando {
            Comando::Exec {
                vps_nome,
                comando,
                json,
            } => {
                assert_eq!(vps_nome, "vps1");
                assert_eq!(comando, "ls");
                assert!(json);
            }
            outro => panic!("comando inesperado: {outro:?}"),
        }
    }
}