Skip to main content

rbnx/cmd/
init.rs

1// SPDX-License-Identifier: MulanPSL-2.0
2// `rbnx init <name>` — scaffold a robot deployment directory.
3
4use anyhow::{Context, Result};
5use robonix_cli::output;
6use std::path::Path;
7
8/// Reject names containing path separators or traversal components.
9fn validate_name(name: &str) -> Result<()> {
10    if name.is_empty() {
11        anyhow::bail!("name must not be empty");
12    }
13    if name.contains('/') || name.contains('\\') || name == ".." || name.starts_with("../") {
14        anyhow::bail!("invalid name '{name}': must not contain path separators or '..' components");
15    }
16    Ok(())
17}
18
19fn robot_catalog_name(name: &str) -> String {
20    let suffix = name.strip_prefix("robot-").unwrap_or(name);
21    format!("robonix.robot.{}", suffix.replace('-', "."))
22}
23
24fn deployment_manifest(name: &str) -> String {
25    let catalog_name = robot_catalog_name(name);
26    format!(
27        r#"manifestVersion: 1
28catalog:
29  name: {catalog_name}
30  version: 0.1.0
31  description: "TODO: describe this robot deployment."
32  license: Apache-2.0
33  tags:
34    - robot
35    - deploy
36    - robonix
37  maintainers:
38    - Your Name <you@example.com>
39
40name: {name}
41
42# Optional model catalog override. rbnx validates it and exports its key
43# variables; omit the pilot vlm block below to let Pilot resolve from it.
44# model_catalog: ./models.yaml
45
46env:
47  LOG: "INFO"
48
49system:
50  atlas:
51    listen: 127.0.0.1:50051
52    log: info
53  executor:
54    listen: 127.0.0.1:50061
55    log: info
56  pilot:
57    listen: 127.0.0.1:50071
58    log: info
59    vlm:
60      upstream: ${{VLM_BASE_URL}}
61      api_key: ${{VLM_API_KEY}}
62      model: ${{VLM_MODEL}}
63      api_format: openai
64  liaison:
65    listen: 0.0.0.0:50081
66    log: info
67
68primitive: []
69
70service: []
71
72skill: []
73"#
74    )
75}
76
77pub async fn execute(name: &str, path: Option<&Path>) -> Result<()> {
78    validate_name(name)?;
79
80    let base = match path {
81        Some(p) => p.to_path_buf(),
82        None => std::env::current_dir()?,
83    };
84    let project_dir = base.join(name);
85
86    if project_dir.exists() {
87        anyhow::bail!("directory '{}' already exists", project_dir.display());
88    }
89
90    output::action("Init", &format!("creating robot deployment '{name}'"));
91
92    std::fs::create_dir_all(&project_dir).context("failed to create deployment directory")?;
93
94    // robonix_manifest.yaml (deployment manifest).
95    let manifest = deployment_manifest(name);
96    std::fs::write(project_dir.join("robonix_manifest.yaml"), manifest)
97        .context("failed to write robonix_manifest.yaml")?;
98
99    // .gitignore
100    let gitignore = "\
101rbnx-build/
102rbnx-boot/
103__pycache__/
104*.pyc
105.venv/
106";
107    std::fs::write(project_dir.join(".gitignore"), gitignore)
108        .context("failed to write .gitignore")?;
109
110    output::success(&format!(
111        "Robot deployment '{name}' created at {}",
112        project_dir.display()
113    ));
114    output::sub_step("robonix_manifest.yaml");
115    output::sub_step(".gitignore");
116
117    Ok(())
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn generated_manifest_has_complete_robot_catalog_metadata() {
126        let root: serde_yaml::Value =
127            serde_yaml::from_str(&deployment_manifest("robot-example-mobile")).unwrap();
128        let catalog = root
129            .get("catalog")
130            .and_then(|value| value.as_mapping())
131            .unwrap();
132
133        assert_eq!(
134            root.get("manifestVersion").and_then(|value| value.as_u64()),
135            Some(1)
136        );
137        for key in [
138            "name",
139            "version",
140            "description",
141            "license",
142            "tags",
143            "maintainers",
144        ] {
145            assert!(
146                catalog.contains_key(serde_yaml::Value::String(key.into())),
147                "missing catalog.{key}"
148            );
149        }
150    }
151}
浙ICP备2023005800号-2 浙公网安备33010902005384号