rusta-cli 1.3.1

macOS arm64 CLI for creating and managing Ubuntu VMs on Tart
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
use std::collections::BTreeMap;
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::paths;

/// The image source seeded when no sources are configured. Preserves the
/// original single-source behavior (clone Ubuntu from cirruslabs).
pub const DEFAULT_SOURCE_REGISTRY: &str = "ghcr.io/cirruslabs";

/// The primary image name — the `create` default and the first seeded image.
pub const DEFAULT_IMAGE: &str = "ubuntu";

/// The image names seeded when none are configured, in priority order. The
/// first (`ubuntu`) is the `create` default; `ubuntu-desktop` is supported
/// out of the box too.
pub const DEFAULT_IMAGES: &[&str] = &["ubuntu", "ubuntu-desktop"];

/// The seeded image list as owned strings.
fn default_images() -> Vec<String> {
    DEFAULT_IMAGES.iter().map(|s| s.to_string()).collect()
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct State {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_vm: Option<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub vms: BTreeMap<String, VmState>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub update: Option<UpdateState>,
    /// Ordered list of image sources (registry+namespace prefixes). Position is
    /// priority; the first source advertising a requested version wins. An empty
    /// list means "use the seeded default" — see [`State::effective_sources`].
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub sources: Vec<Source>,
    /// Ordered list of image names (repositories under each source). Position is
    /// priority; the first is the `create` default. An empty list means "use the
    /// seeded default" (`ubuntu`) — see [`State::effective_images`].
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub images: Vec<String>,
}

/// An image source: a registry host + namespace prefix, e.g. `ghcr.io/cirruslabs`.
/// rusta always appends `/ubuntu` to form the repository.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
    pub registry: String,
}

impl Source {
    pub fn new(registry: impl Into<String>) -> Self {
        Self {
            registry: registry.into(),
        }
    }

    /// Human-readable label: the last path segment of the prefix
    /// (`ghcr.io/cirruslabs` → `cirruslabs`). Not a unique identifier.
    pub fn label(&self) -> &str {
        self.registry.rsplit('/').next().unwrap_or(&self.registry)
    }

    /// Full image reference for a given image + version, e.g.
    /// `ghcr.io/cirruslabs/ubuntu:24.04`.
    pub fn image_ref(&self, image: &str, version: &str) -> String {
        format!("{}/{}:{}", self.registry, image, version)
    }

    /// `(host, "<namespace>/<image>")` for building registry v2 API URLs, e.g.
    /// `("ghcr.io", "cirruslabs/ubuntu")`. Returns `None` if there is no `/`.
    pub fn host_and_repo_path(&self, image: &str) -> Option<(&str, String)> {
        let (host, ns) = self.registry.split_once('/')?;
        Some((host, format!("{ns}/{image}")))
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct VmState {
    #[serde(default)]
    pub gui: bool,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct UpdateState {
    #[serde(default)]
    pub last_checked_at: u64,
    #[serde(default)]
    pub last_notified_at: u64,
    #[serde(default)]
    pub latest_known: Option<String>,
}

impl State {
    pub fn load() -> Self {
        let path = paths::state_file();
        if !path.exists() {
            return Self::default();
        }
        match std::fs::read_to_string(&path) {
            Ok(s) => toml::from_str(&s).unwrap_or_default(),
            Err(_) => Self::default(),
        }
    }

    pub fn save(&self) -> std::io::Result<()> {
        paths::ensure_dirs()?;
        let s = toml::to_string(self).expect("serialize state");
        write_atomically(&paths::state_file(), &s)
    }

    /// Configured sources, or the seeded default when none are configured.
    /// rusta is never sourceless.
    pub fn effective_sources(&self) -> Vec<Source> {
        if self.sources.is_empty() {
            vec![Source::new(DEFAULT_SOURCE_REGISTRY)]
        } else {
            self.sources.clone()
        }
    }

    /// Configured images, or the seeded defaults (`ubuntu`, `ubuntu-desktop`)
    /// when none are configured. rusta is never imageless.
    pub fn effective_images(&self) -> Vec<String> {
        if self.images.is_empty() {
            default_images()
        } else {
            self.images.clone()
        }
    }
}

/// Effective image sources in priority order (seeded default when none configured).
pub fn sources() -> Vec<Source> {
    State::load().effective_sources()
}

/// Effective image names in priority order (seeded default when none configured).
pub fn images() -> Vec<String> {
    State::load().effective_images()
}

/// Materialize the seeded default into the stored list before a mutation, so the
/// user starts from `[cirruslabs]` and adds to it.
fn materialize(s: &mut State) {
    if s.sources.is_empty() {
        s.sources = s.effective_sources();
    }
}

/// Append a source. Returns `Ok(false)` if the registry is already present.
pub fn add_source(registry: &str) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize(&mut s);
    if s.sources.iter().any(|x| x.registry == registry) {
        return Ok(false);
    }
    s.sources.push(Source::new(registry));
    s.save()?;
    Ok(true)
}

/// Remove a source by exact registry prefix. Returns `Ok(false)` if absent.
/// Removing the last remaining source re-seeds the default (never sourceless).
pub fn remove_source(registry: &str) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize(&mut s);
    let before = s.sources.len();
    s.sources.retain(|x| x.registry != registry);
    if s.sources.len() == before {
        return Ok(false);
    }
    if s.sources.is_empty() {
        s.sources = vec![Source::new(DEFAULT_SOURCE_REGISTRY)];
    }
    s.save()?;
    Ok(true)
}

/// Move a source to a new 1-based priority position (clamped). Returns
/// `Ok(false)` if the registry is not present.
pub fn move_source(registry: &str, position: usize) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize(&mut s);
    let Some(cur) = s.sources.iter().position(|x| x.registry == registry) else {
        return Ok(false);
    };
    let item = s.sources.remove(cur);
    let target = position.saturating_sub(1).min(s.sources.len());
    s.sources.insert(target, item);
    s.save()?;
    Ok(true)
}

/// Materialize the seeded default image into the stored list before a mutation,
/// so the user starts from `[ubuntu]` and adds to it.
fn materialize_images(s: &mut State) {
    if s.images.is_empty() {
        s.images = s.effective_images();
    }
}

/// Append an image. Returns `Ok(false)` if the name is already present.
pub fn add_image(name: &str) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize_images(&mut s);
    if s.images.iter().any(|x| x == name) {
        return Ok(false);
    }
    s.images.push(name.to_string());
    s.save()?;
    Ok(true)
}

/// Remove an image by exact name. Returns `Ok(false)` if absent. Removing the
/// last remaining image re-seeds the default (never imageless).
pub fn remove_image(name: &str) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize_images(&mut s);
    let before = s.images.len();
    s.images.retain(|x| x != name);
    if s.images.len() == before {
        return Ok(false);
    }
    if s.images.is_empty() {
        s.images = default_images();
    }
    s.save()?;
    Ok(true)
}

/// Move an image to a new 1-based priority position (clamped). Returns
/// `Ok(false)` if the name is not present.
pub fn move_image(name: &str, position: usize) -> std::io::Result<bool> {
    let mut s = State::load();
    materialize_images(&mut s);
    let Some(cur) = s.images.iter().position(|x| x == name) else {
        return Ok(false);
    };
    let item = s.images.remove(cur);
    let target = position.saturating_sub(1).min(s.images.len());
    s.images.insert(target, item);
    s.save()?;
    Ok(true)
}

fn write_atomically(path: &Path, contents: &str) -> std::io::Result<()> {
    let dir = path.parent().unwrap_or_else(|| Path::new("."));
    std::fs::create_dir_all(dir)?;
    let tmp = dir.join(format!(".{}.tmp", path.file_name().unwrap().to_string_lossy()));
    std::fs::write(&tmp, contents)?;
    std::fs::rename(&tmp, path)
}

pub fn set_default(vm: &str) -> std::io::Result<()> {
    let mut s = State::load();
    s.default_vm = Some(vm.to_string());
    s.save()
}

pub fn clear_default_if_matches(vm: &str) -> std::io::Result<()> {
    let mut s = State::load();
    if s.default_vm.as_deref() == Some(vm) {
        s.default_vm = None;
        s.save()
    } else {
        Ok(())
    }
}

pub fn set_vm_gui(vm: &str, gui: bool) -> std::io::Result<()> {
    let mut s = State::load();
    s.vms.insert(vm.to_string(), VmState { gui });
    s.save()
}

pub fn vm_gui(vm: &str) -> Option<bool> {
    State::load().vms.get(vm).map(|v| v.gui)
}

pub fn forget_vm(vm: &str) -> std::io::Result<()> {
    let mut s = State::load();
    if s.vms.remove(vm).is_some() {
        s.save()
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn with_temp_root<F: FnOnce()>(f: F) {
        let _g = ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        let prev = std::env::var_os("RUSTA_STATE_ROOT");
        std::env::set_var("RUSTA_STATE_ROOT", tmp.path());
        f();
        match prev {
            Some(v) => std::env::set_var("RUSTA_STATE_ROOT", v),
            None => std::env::remove_var("RUSTA_STATE_ROOT"),
        }
    }

    #[test]
    fn load_returns_default_when_missing() {
        with_temp_root(|| {
            let s = State::load();
            assert!(s.default_vm.is_none());
        });
    }

    #[test]
    fn save_and_reload_roundtrip() {
        with_temp_root(|| {
            set_default("hello").unwrap();
            let s = State::load();
            assert_eq!(s.default_vm.as_deref(), Some("hello"));
        });
    }

    #[test]
    fn clear_default_only_when_match() {
        with_temp_root(|| {
            set_default("a").unwrap();
            clear_default_if_matches("b").unwrap();
            assert_eq!(State::load().default_vm.as_deref(), Some("a"));
            clear_default_if_matches("a").unwrap();
            assert!(State::load().default_vm.is_none());
        });
    }

    #[test]
    fn vm_gui_roundtrip() {
        with_temp_root(|| {
            assert_eq!(vm_gui("lab"), None);
            set_vm_gui("lab", true).unwrap();
            assert_eq!(vm_gui("lab"), Some(true));
            set_vm_gui("lab", false).unwrap();
            assert_eq!(vm_gui("lab"), Some(false));
            forget_vm("lab").unwrap();
            assert_eq!(vm_gui("lab"), None);
        });
    }

    #[test]
    fn old_schema_without_vms_table_still_loads() {
        with_temp_root(|| {
            paths::ensure_dirs().unwrap();
            std::fs::write(paths::state_file(), b"default_vm = \"hello\"\n").unwrap();
            let s = State::load();
            assert_eq!(s.default_vm.as_deref(), Some("hello"));
            assert!(s.vms.is_empty());
            assert_eq!(vm_gui("hello"), None);
        });
    }

    #[test]
    fn forget_vm_is_noop_when_absent() {
        with_temp_root(|| {
            forget_vm("missing").unwrap();
            assert!(State::load().vms.is_empty());
        });
    }

    #[test]
    fn load_corrupt_file_returns_default() {
        with_temp_root(|| {
            paths::ensure_dirs().unwrap();
            std::fs::write(paths::state_file(), b"@@@not toml@@@").unwrap();
            let s = State::load();
            assert!(s.default_vm.is_none());
        });
    }

    #[test]
    fn source_label_and_refs() {
        let s = Source::new("ghcr.io/cirruslabs");
        assert_eq!(s.label(), "cirruslabs");
        assert_eq!(
            s.image_ref("ubuntu", "24.04"),
            "ghcr.io/cirruslabs/ubuntu:24.04"
        );
        assert_eq!(
            s.image_ref("ubuntu-desktop", "24.04"),
            "ghcr.io/cirruslabs/ubuntu-desktop:24.04"
        );
        assert_eq!(
            s.host_and_repo_path("ubuntu"),
            Some(("ghcr.io", "cirruslabs/ubuntu".to_string()))
        );
        assert_eq!(
            s.host_and_repo_path("ubuntu-desktop"),
            Some(("ghcr.io", "cirruslabs/ubuntu-desktop".to_string()))
        );
    }

    #[test]
    fn effective_images_seeds_defaults_when_empty() {
        with_temp_root(|| {
            assert_eq!(
                images(),
                vec!["ubuntu".to_string(), "ubuntu-desktop".to_string()]
            );
        });
    }

    #[test]
    fn add_image_materializes_defaults_then_appends() {
        with_temp_root(|| {
            assert!(add_image("ubuntu-kairos").unwrap());
            assert_eq!(
                images(),
                vec![
                    "ubuntu".to_string(),
                    "ubuntu-desktop".to_string(),
                    "ubuntu-kairos".to_string()
                ]
            );
        });
    }

    #[test]
    fn add_image_is_idempotent() {
        with_temp_root(|| {
            // ubuntu-desktop is already a seeded default → adding is a no-op.
            assert!(!add_image("ubuntu-desktop").unwrap());
            assert!(add_image("ubuntu-kairos").unwrap());
            assert!(!add_image("ubuntu-kairos").unwrap());
            assert_eq!(
                images().iter().filter(|i| *i == "ubuntu-kairos").count(),
                1
            );
        });
    }

    #[test]
    fn remove_image_and_reseed_when_last() {
        with_temp_root(|| {
            // Remove both seeded defaults; the list must never become empty.
            assert!(remove_image("ubuntu-desktop").unwrap());
            assert_eq!(images(), vec!["ubuntu".to_string()]);
            assert!(remove_image("ubuntu").unwrap());
            // Removing the last remaining image re-seeds the defaults.
            assert_eq!(
                images(),
                vec!["ubuntu".to_string(), "ubuntu-desktop".to_string()]
            );
            // Removing something absent is a no-op false.
            assert!(!remove_image("nope").unwrap());
        });
    }

    #[test]
    fn move_image_changes_priority() {
        with_temp_root(|| {
            add_image("ubuntu-kairos").unwrap();
            // [ubuntu, ubuntu-desktop, ubuntu-kairos] -> move kairos to position 1
            assert!(move_image("ubuntu-kairos", 1).unwrap());
            assert_eq!(
                images(),
                vec![
                    "ubuntu-kairos".to_string(),
                    "ubuntu".to_string(),
                    "ubuntu-desktop".to_string()
                ]
            );
            // Out-of-range position clamps to the end.
            assert!(move_image("ubuntu-kairos", 99).unwrap());
            assert_eq!(images().last().unwrap(), "ubuntu-kairos");
            assert!(!move_image("absent", 1).unwrap());
        });
    }

    #[test]
    fn old_schema_without_images_still_loads() {
        with_temp_root(|| {
            paths::ensure_dirs().unwrap();
            std::fs::write(paths::state_file(), b"default_vm = \"hello\"\n").unwrap();
            let s = State::load();
            assert!(s.images.is_empty());
            assert_eq!(
                images(),
                vec!["ubuntu".to_string(), "ubuntu-desktop".to_string()]
            );
        });
    }

    #[test]
    fn effective_sources_seeds_default_when_empty() {
        with_temp_root(|| {
            let s = sources();
            assert_eq!(s.len(), 1);
            assert_eq!(s[0].registry, DEFAULT_SOURCE_REGISTRY);
        });
    }

    #[test]
    fn add_source_materializes_default_then_appends() {
        with_temp_root(|| {
            assert!(add_source("ghcr.io/pallewela").unwrap());
            let s = sources();
            assert_eq!(s.len(), 2);
            assert_eq!(s[0].registry, DEFAULT_SOURCE_REGISTRY);
            assert_eq!(s[1].registry, "ghcr.io/pallewela");
        });
    }

    #[test]
    fn add_source_is_idempotent() {
        with_temp_root(|| {
            assert!(add_source("ghcr.io/pallewela").unwrap());
            assert!(!add_source("ghcr.io/pallewela").unwrap());
            assert_eq!(
                sources()
                    .iter()
                    .filter(|s| s.registry == "ghcr.io/pallewela")
                    .count(),
                1
            );
        });
    }

    #[test]
    fn remove_source_and_reseed_when_last() {
        with_temp_root(|| {
            add_source("ghcr.io/pallewela").unwrap();
            assert!(remove_source("ghcr.io/pallewela").unwrap());
            assert_eq!(sources().len(), 1);
            // Removing the last remaining source re-seeds the default.
            assert!(remove_source(DEFAULT_SOURCE_REGISTRY).unwrap());
            let s = sources();
            assert_eq!(s.len(), 1);
            assert_eq!(s[0].registry, DEFAULT_SOURCE_REGISTRY);
            // Removing something absent is a no-op false.
            assert!(!remove_source("ghcr.io/nope").unwrap());
        });
    }

    #[test]
    fn move_source_changes_priority() {
        with_temp_root(|| {
            add_source("ghcr.io/pallewela").unwrap();
            add_source("ghcr.io/third").unwrap();
            // [cirruslabs, pallewela, third] -> move third to position 1
            assert!(move_source("ghcr.io/third", 1).unwrap());
            let regs: Vec<_> = sources().into_iter().map(|s| s.registry).collect();
            assert_eq!(
                regs,
                vec!["ghcr.io/third", "ghcr.io/cirruslabs", "ghcr.io/pallewela"]
            );
            // Out-of-range position clamps to the end.
            assert!(move_source("ghcr.io/third", 99).unwrap());
            let regs: Vec<_> = sources().into_iter().map(|s| s.registry).collect();
            assert_eq!(regs.last().unwrap(), "ghcr.io/third");
            assert!(!move_source("ghcr.io/absent", 1).unwrap());
        });
    }

    #[test]
    fn old_schema_without_sources_still_loads() {
        with_temp_root(|| {
            paths::ensure_dirs().unwrap();
            std::fs::write(paths::state_file(), b"default_vm = \"hello\"\n").unwrap();
            let s = State::load();
            assert!(s.sources.is_empty());
            assert_eq!(sources().len(), 1);
        });
    }
}