Skip to main content

virtuoso_cli/commands/
init.rs

1use crate::error::{Result, VirtuosoError};
2use serde_json::{json, Value};
3use std::io::Write;
4use std::path::Path;
5
6const ENV_TEMPLATE: &str = r#"# Virtuoso CLI Configuration
7# Remote host (SSH alias or hostname)
8VB_REMOTE_HOST=
9
10# Remote user (optional, defaults to current user)
11# VB_REMOTE_USER=
12
13# SSH port (default: 65432)
14VB_PORT=65432
15
16# Jump/bastion host (optional)
17# VB_JUMP_HOST=
18# VB_JUMP_USER=
19
20# Timeout in seconds (default: 30)
21VB_TIMEOUT=30
22
23# Keep remote files after stopping (default: false)
24VB_KEEP_REMOTE_FILES=false
25
26# Spectre command (default: spectre)
27# VB_SPECTRE_CMD=spectre
28
29# Spectre extra arguments
30# VB_SPECTRE_ARGS=
31"#;
32
33pub fn run(if_not_exists: bool) -> Result<Value> {
34    let env_path = Path::new(".env");
35
36    if env_path.exists() {
37        if if_not_exists {
38            return Ok(json!({
39                "status": "skipped",
40                "reason": ".env already exists",
41                "path": ".env",
42            }));
43        }
44        return Err(VirtuosoError::Conflict(
45            ".env already exists (use --if-not-exists to skip)".into(),
46        ));
47    }
48
49    let mut file = std::fs::File::create(env_path)?;
50    file.write_all(ENV_TEMPLATE.as_bytes())?;
51
52    Ok(json!({
53        "status": "created",
54        "path": ".env",
55        "next_step": "Edit .env and set VB_REMOTE_HOST, then run: virtuoso tunnel start",
56    }))
57}