robotrt-cli 0.1.0-beta.2

RobotRT modular robotics runtime and middleware components.
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
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

const DEFAULT_DAEMON_ENDPOINT: &str = "127.0.0.1:7588";
const DEFAULT_SIDECAR_RUNTIME_PATH: &str = "artifacts/sidecar/gateway-sidecar-runtime.json";

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CliRuntimeMode {
    Embedded,
    Daemon,
}

fn parse_runtime_mode(args: &[String]) -> Result<CliRuntimeMode, String> {
    match option_value(args, "--mode").as_deref() {
        None => Ok(CliRuntimeMode::Embedded),
        Some("embedded") => Ok(CliRuntimeMode::Embedded),
        Some("daemon") => Ok(CliRuntimeMode::Daemon),
        Some(other) => Err(format!(
            "unsupported --mode value: {other} (expected embedded|daemon)"
        )),
    }
}

pub fn resolve_runtime_endpoint(
    args: &[String],
    daemon_default: &str,
    embedded_missing_endpoint_message: &str,
) -> Result<String, String> {
    let endpoint = resolve_runtime_endpoint_optional(args, daemon_default)?
        .unwrap_or_else(|| daemon_default.to_string());
    let _ = embedded_missing_endpoint_message;
    Ok(endpoint)
}

pub fn resolve_runtime_endpoint_optional(
    args: &[String],
    daemon_default: &str,
) -> Result<Option<String>, String> {
    let mode = parse_runtime_mode(args)?;
    let endpoint = option_value(args, "--endpoint");

    match (mode, endpoint) {
        (CliRuntimeMode::Daemon, Some(raw)) => {
            crate::gateway::normalize_udp_endpoint(&raw).map(Some)
        }
        (CliRuntimeMode::Daemon, None) => {
            resolve_automatic_endpoint(args, daemon_default).map(Some)
        }
        (CliRuntimeMode::Embedded, Some(raw)) => {
            crate::gateway::normalize_udp_endpoint(&raw).map(Some)
        }
        (CliRuntimeMode::Embedded, None) => {
            resolve_automatic_endpoint(args, daemon_default).map(Some)
        }
    }
}

pub fn resolve_runtime_endpoint_from_hint(
    args: &[String],
    endpoint_hint: Option<String>,
    daemon_default: &str,
) -> Result<String, String> {
    if let Some(raw) = endpoint_hint {
        return crate::gateway::normalize_udp_endpoint(&raw);
    }
    resolve_automatic_endpoint(args, daemon_default)
}

pub fn discover_cluster_endpoints(args: &[String]) -> Vec<String> {
    let mut endpoints = Vec::new();
    if let Some(runtime) = load_sidecar_runtime_json(args) {
        push_unique_endpoint(
            &mut endpoints,
            runtime
                .get("advertise_endpoint")
                .and_then(serde_json::Value::as_str),
        );
        push_unique_endpoint(
            &mut endpoints,
            runtime
                .get("endpoint")
                .and_then(serde_json::Value::as_str),
        );
        if let Some(peers) = runtime
            .get("cluster")
            .and_then(|item| item.get("peers"))
            .and_then(serde_json::Value::as_array)
        {
            for peer in peers {
                push_unique_endpoint(&mut endpoints, peer.as_str());
            }
        }
    }
    endpoints
}

fn resolve_automatic_endpoint(args: &[String], daemon_default: &str) -> Result<String, String> {
    if let Some(raw) = std::env::var("ROBOTRT_GATEWAY_ENDPOINT").ok()
        && !raw.trim().is_empty()
    {
        return crate::gateway::normalize_udp_endpoint(raw.trim());
    }

    if let Some(raw) = load_sidecar_runtime_json(args).and_then(|runtime| {
        runtime
            .get("advertise_endpoint")
            .and_then(serde_json::Value::as_str)
            .or_else(|| runtime.get("endpoint").and_then(serde_json::Value::as_str))
            .map(ToString::to_string)
    }) {
        return crate::gateway::normalize_udp_endpoint(&raw);
    }

    crate::gateway::normalize_udp_endpoint(if daemon_default.trim().is_empty() {
        DEFAULT_DAEMON_ENDPOINT
    } else {
        daemon_default
    })
}

fn load_sidecar_runtime_json(args: &[String]) -> Option<serde_json::Value> {
    let from_args = option_value(args, "--sidecar-registry");
    let from_env = std::env::var("ROBOTRT_SIDECAR_REGISTRY").ok();
    let path = from_args
        .or(from_env)
        .unwrap_or_else(|| String::from(DEFAULT_SIDECAR_RUNTIME_PATH));
    let body = fs::read_to_string(path).ok()?;
    serde_json::from_str::<serde_json::Value>(&body).ok()
}

fn push_unique_endpoint(items: &mut Vec<String>, maybe_raw: Option<&str>) {
    let Some(raw) = maybe_raw else {
        return;
    };
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return;
    }
    let normalized = match crate::gateway::normalize_udp_endpoint(trimmed) {
        Ok(value) => value,
        Err(_) => return,
    };
    if !items.iter().any(|item| item == &normalized) {
        items.push(normalized);
    }
}

pub fn parse_report_path(args: &[String], default_path: &str) -> Result<PathBuf, String> {
    let mut idx = 0usize;
    while idx < args.len() {
        if args[idx] == "--report" {
            if idx + 1 >= args.len() {
                return Err(String::from("missing value for --report"));
            }
            return Ok(PathBuf::from(&args[idx + 1]));
        }
        idx += 1;
    }

    let default = PathBuf::from(default_path);
    if default.exists() {
        return Ok(default);
    }

    if let Some(discovered) = discover_report_path(&default) {
        return Ok(discovered);
    }

    Ok(default)
}

fn discover_report_path(default_path: &Path) -> Option<PathBuf> {
    let parent = default_path.parent()?;
    let stem = default_path.file_stem()?.to_string_lossy();
    let stem_suffix = stem.rsplit('-').next().unwrap_or(stem.as_ref()).to_string();

    let mut latest: Option<(SystemTime, PathBuf)> = None;

    let entries = fs::read_dir(parent).ok()?;
    for entry in entries {
        let Ok(entry) = entry else {
            continue;
        };
        let path = entry.path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
            continue;
        }

        let Some(candidate_stem) = path.file_stem().and_then(|item| item.to_str()) else {
            continue;
        };
        let matches = candidate_stem == stem_suffix
            || candidate_stem.ends_with(&format!("-{stem_suffix}"));
        if !matches {
            continue;
        }

        let modified = entry
            .metadata()
            .and_then(|metadata| metadata.modified())
            .unwrap_or(SystemTime::UNIX_EPOCH);

        match &latest {
            Some((latest_modified, _)) if modified <= *latest_modified => {}
            _ => latest = Some((modified, path)),
        }
    }

    latest.map(|(_, path)| path)
}

pub fn has_flag(args: &[String], flag: &str) -> bool {
    args.iter().any(|arg| arg == flag)
}

pub fn is_help_token(arg: &str) -> bool {
    matches!(arg, "help" | "--help" | "-h")
}

pub fn option_value(args: &[String], option: &str) -> Option<String> {
    let mut idx = 0usize;
    while idx + 1 < args.len() {
        if args[idx] == option {
            return Some(args[idx + 1].clone());
        }
        idx += 1;
    }
    None
}

pub fn parse_usize_option(args: &[String], option: &str, default: usize) -> Result<usize, String> {
    let Some(raw) = option_value(args, option) else {
        return Ok(default);
    };
    raw.parse::<usize>()
        .map_err(|err| format!("invalid value for {option}: {raw} ({err})"))
}

pub fn parse_u64_option(args: &[String], option: &str, default: u64) -> Result<u64, String> {
    let Some(raw) = option_value(args, option) else {
        return Ok(default);
    };
    raw.parse::<u64>()
        .map_err(|err| format!("invalid value for {option}: {raw} ({err})"))
}

pub fn first_positional(args: &[String]) -> Option<String> {
    let mut idx = 0usize;
    while idx < args.len() {
        let token = &args[idx];
        if token.starts_with('-') {
            if option_takes_value(token) {
                idx += 2;
            } else {
                idx += 1;
            }
            continue;
        }
        return Some(token.clone());
    }
    None
}

fn option_takes_value(option: &str) -> bool {
    matches!(
        option,
        "--report"
            | "--endpoint"
            | "--timeout-ms"
            | "--input"
            | "--output"
            | "--bind"
            | "--source"
            | "--count"
            | "--topic"
            | "--domain"
            | "--speed"
            | "--limit"
            | "--iterations"
            | "--interval-ms"
            | "--format"
            | "--alert-template"
            | "--template"
            | "--topic-warn-utilization"
            | "--topic-critical-utilization"
            | "--baseline-report"
            | "--baseline-endpoint"
            | "--baseline-timeout-ms"
            | "--policy"
            | "--project"
            | "--reports"
            | "--endpoints"
            | "--baseline-reports"
            | "--baseline-endpoints"
            | "--input-bag"
            | "--obs-format"
            | "--profile-template"
            | "--target-schema"
            | "--projects"
            | "--projects-file"
            | "--request"
            | "--backup"
            | "--scan-root"
            | "--filter-schema"
            | "--filter-template"
            | "--batch-report"
            | "--severity-map"
            | "--file"
            | "--task"
            | "--profile"
            | "--overlay"
            | "--group"
            | "--max-parallel"
            | "--state-file"
            | "--mode"
            | "--sidecar-registry"
            | "--enabled"
            | "--rate-max"
            | "--rate-burst"
            | "--atomic-write"
            | "--include"
            | "--goal"
            | "--goal-id"
            | "--message"
            | "--max-items"
            | "--status-report"
            | "--runtime-report"
            | "--middleware-report"
            | "--resource-report"
            | "--field"
            | "--service-retry-timeout-ms"
            | "--action-retry-timeout-ms"
            | "--mission-retry-timeout-ms"
            | "--topic-dedupe-window"
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_file_path(prefix: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("{prefix}-{nanos}.json"))
    }

    #[test]
    fn resolve_runtime_endpoint_from_hint_uses_sidecar_registry() {
        let registry = temp_file_path("robotrt-sidecar-runtime");
        let body = serde_json::json!({
            "api_version": "robotrt.gateway.sidecar.runtime.v1",
            "endpoint": "127.0.0.1:7588",
            "advertise_endpoint": "127.0.0.1:7688",
            "cluster": {
                "id": "cluster-alpha",
                "zone": "zone-a",
                "peers": ["127.0.0.1:7788"]
            }
        });
        fs::write(
            &registry,
            serde_json::to_string(&body).expect("serialize sidecar runtime"),
        )
        .expect("write sidecar runtime file");

        let args = vec![
            String::from("runtime"),
            String::from("load"),
            String::from("--sidecar-registry"),
            registry.display().to_string(),
        ];

        let resolved = resolve_runtime_endpoint_from_hint(&args, None, DEFAULT_DAEMON_ENDPOINT)
            .expect("resolve runtime endpoint");
        assert_eq!(resolved, "127.0.0.1:7688");

        let _ = fs::remove_file(registry);
    }

    #[test]
    fn discover_cluster_endpoints_reads_registry_and_peers() {
        let registry = temp_file_path("robotrt-sidecar-cluster");
        let body = serde_json::json!({
            "api_version": "robotrt.gateway.sidecar.runtime.v1",
            "endpoint": "127.0.0.1:7588",
            "advertise_endpoint": "127.0.0.1:7688",
            "cluster": {
                "id": "cluster-alpha",
                "zone": "zone-a",
                "peers": ["127.0.0.1:7788", "127.0.0.1:7888"]
            }
        });
        fs::write(
            &registry,
            serde_json::to_string(&body).expect("serialize sidecar cluster runtime"),
        )
        .expect("write sidecar cluster runtime");

        let args = vec![
            String::from("ops"),
            String::from("fleet"),
            String::from("--sidecar-registry"),
            registry.display().to_string(),
        ];

        let endpoints = discover_cluster_endpoints(&args);
        assert_eq!(
            endpoints,
            vec![
                String::from("127.0.0.1:7688"),
                String::from("127.0.0.1:7588"),
                String::from("127.0.0.1:7788"),
                String::from("127.0.0.1:7888"),
            ]
        );

        let _ = fs::remove_file(registry);
    }
}