knixl 1.11.2

Compile opinionated KDL into maintainable, committed NixOS module source.
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
//! Updates screen: runs the `knixl updates` report in the background, shows it, and lets you
//! choose what to take: the whole nixpkgs refresh (optionally only if it fixes CVEs), or
//! version pins for packages the hosts install directly. Applying ends the session and the
//! CLI carries the choice out.

use bubbletea_rs::event::{KeyMsg, WindowSizeMsg};
use bubbletea_rs::{command, Cmd, Model as BubbleTeaModel, Msg};
use bubbletea_widgets::{spinner, viewport};
use crossterm::event::{KeyCode, KeyModifiers};
use lipgloss::{join_vertical, rounded_border, Style, LEFT};

use knixl_pipeline::updates::{direct_updates, render_markdown, HostReport};

use super::{config, theme, widgets, Nav, Step};

/// The background report, delivered back to `update`.
struct UpdatesDone(Result<Vec<HostReport>, String>);

/// One choice on the screen.
#[derive(Debug, Clone, PartialEq)]
pub enum Choice {
    Refresh,
    IfSecurity,
    Pin {
        host: String,
        attr: String,
        version: String,
    },
}

enum State {
    Running,
    Ready,
    Failed(String),
}

pub struct UpdatesModel {
    state: State,
    spinner: spinner::Model,
    preview: viewport::Model,
    choices: Vec<(Choice, bool)>,
    /// Index into `choices`, then Apply, then Cancel.
    focus: usize,
    summary: Vec<String>,
}

fn new_spinner() -> spinner::Model {
    spinner::new(&[
        spinner::with_spinner(spinner::DOT.clone()),
        spinner::with_style(theme::amber()),
    ])
}

fn dims(size: (u16, u16)) -> (usize, usize) {
    (
        (size.0 as usize).saturating_sub(8).max(20),
        (size.1 as usize).saturating_sub(16).max(4),
    )
}

impl UpdatesModel {
    /// Open the screen and start the report, if the project can produce one.
    pub fn enter(size: (u16, u16)) -> (Self, Option<Cmd>) {
        let (w, h) = dims(size);
        let mut m = UpdatesModel {
            state: State::Running,
            spinner: new_spinner(),
            preview: viewport::new(w, h),
            choices: Vec::new(),
            focus: 0,
            summary: Vec::new(),
        };
        let Some(report) = config().updates.clone() else {
            m.state = State::Failed(
                "updates need a generated flake: add a system block to knixl.kdl".into(),
            );
            return (m, None);
        };
        let run: Cmd = Box::pin(async move {
            match tokio::task::spawn_blocking(move || report()).await {
                Ok(r) => Some(Box::new(UpdatesDone(r)) as Msg),
                Err(_) => None,
            }
        });
        let tick = m.spinner.tick_msg();
        let spin: Cmd = Box::pin(async move { Some(Box::new(tick) as Msg) });
        (m, Some(command::batch(vec![run, spin])))
    }

    fn on_report(&mut self, reports: Vec<HostReport>) {
        self.preview.set_content(&render_markdown(&reports));
        self.summary = reports
            .iter()
            .map(|r| {
                let fixed: usize = r
                    .security
                    .as_ref()
                    .map(|s| s.fixed.iter().map(|f| f.cves.len()).sum())
                    .unwrap_or(0);
                let security = match &r.security {
                    Some(_) => format!("{fixed} CVEs fixed"),
                    None => "security not checked".into(),
                };
                format!(
                    "{}: {} version changes, {security}",
                    r.host,
                    r.diff.changed.len()
                )
            })
            .collect();
        if !reports.is_empty() {
            self.choices.push((Choice::Refresh, false));
            self.choices.push((Choice::IfSecurity, false));
            for r in &reports {
                for (attr, version) in direct_updates(r) {
                    self.choices.push((
                        Choice::Pin {
                            host: r.host.clone(),
                            attr,
                            version,
                        },
                        false,
                    ));
                }
            }
        }
        self.state = State::Ready;
    }

    fn is_on(&self, c: &Choice) -> bool {
        self.choices.iter().any(|(x, on)| x == c && *on)
    }

    /// The pins are part of the full refresh, so they only count when it isn't chosen.
    fn apply_nav(&self) -> Nav {
        let refresh = self.is_on(&Choice::Refresh);
        Nav::ApplyUpdates {
            refresh,
            if_security: refresh && self.is_on(&Choice::IfSecurity),
            pins: if refresh {
                Vec::new()
            } else {
                self.choices
                    .iter()
                    .filter(|(_, on)| *on)
                    .filter_map(|(c, _)| match c {
                        Choice::Pin {
                            host,
                            attr,
                            version,
                        } => Some((host.clone(), attr.clone(), version.clone())),
                        _ => None,
                    })
                    .collect()
            },
        }
    }

    fn anything_chosen(&self) -> bool {
        self.choices
            .iter()
            .any(|(c, on)| *on && *c != Choice::IfSecurity)
    }

    pub fn update(&mut self, msg: Msg, size: (u16, u16)) -> Step {
        if msg.downcast_ref::<WindowSizeMsg>().is_some() {
            let (w, h) = dims(size);
            self.preview.width = w;
            self.preview.height = h;
            return Step::stay();
        }
        let msg = match msg.downcast::<UpdatesDone>() {
            Ok(done) => {
                match done.0 {
                    Ok(reports) => self.on_report(reports),
                    Err(e) => self.state = State::Failed(e),
                }
                return Step::stay();
            }
            Err(msg) => msg,
        };
        if msg.downcast_ref::<spinner::TickMsg>().is_some() {
            let cmd = self.spinner.update(msg);
            return Step {
                nav: Nav::Stay,
                cmd: matches!(self.state, State::Running)
                    .then_some(cmd)
                    .flatten(),
            };
        }
        let Some(key) = msg.downcast_ref::<KeyMsg>() else {
            return Step::stay();
        };
        let (code, mods) = (key.key, key.modifiers);
        if code == KeyCode::Esc
            || (code == KeyCode::Char('c') && mods.contains(KeyModifiers::CONTROL))
        {
            return Step::nav(Nav::Back);
        }
        if !matches!(self.state, State::Ready) {
            return Step::stay();
        }
        let slots = self.choices.len() + 2;
        match code {
            KeyCode::Tab | KeyCode::Down => self.focus = (self.focus + 1) % slots,
            KeyCode::BackTab | KeyCode::Up => self.focus = (self.focus + slots - 1) % slots,
            KeyCode::PageUp | KeyCode::PageDown => {
                let cmd = self.preview.update(msg);
                return Step {
                    nav: Nav::Stay,
                    cmd,
                };
            }
            KeyCode::Char(' ') | KeyCode::Enter if self.focus < self.choices.len() => {
                self.choices[self.focus].1 = !self.choices[self.focus].1;
            }
            KeyCode::Enter if self.focus == self.choices.len() && self.anything_chosen() => {
                return Step::nav(self.apply_nav());
            }
            KeyCode::Enter if self.focus == self.choices.len() + 1 => {
                return Step::nav(Nav::Back);
            }
            _ => {}
        }
        Step::stay()
    }

    fn label(&self, c: &Choice) -> String {
        let refresh = self.is_on(&Choice::Refresh);
        match c {
            Choice::Refresh => "take the whole nixpkgs update".into(),
            Choice::IfSecurity => "  ...only if it fixes CVEs".into(),
            Choice::Pin {
                host,
                attr,
                version,
            } => {
                let text = format!("pin {attr} to {version} on {host}");
                if refresh {
                    theme::dim().render(&format!("{text} (part of the update)"))
                } else {
                    text
                }
            }
        }
    }

    pub fn view(&self, _size: (u16, u16)) -> String {
        let mut lines = vec![theme::chip(" updates ")];
        match &self.state {
            State::Running => lines.push(format!(
                "{} checking for updates (this takes a few minutes)",
                self.spinner.view()
            )),
            State::Failed(e) => lines.push(theme::bad().render(&format!("\u{2717} {e}"))),
            State::Ready if self.choices.is_empty() => lines.push(
                theme::good().render("\u{2713} every host is on the newest commit of its release"),
            ),
            State::Ready => {
                for s in &self.summary {
                    lines.push(s.clone());
                }
                lines.push(String::new());
                // One left-aligned block, so the toggles line up when the screen is centred.
                let mut block = Vec::new();
                for (i, (c, on)) in self.choices.iter().enumerate() {
                    let marker = if self.focus == i {
                        theme::accent().render("\u{25b8} ")
                    } else {
                        "  ".into()
                    };
                    block.push(format!("{marker}{} {}", theme::toggle(*on), self.label(c)));
                }
                let button = |label: &str, focused: bool, enabled: bool| {
                    let text = format!(" {label} ");
                    if focused {
                        theme::selected().render(&text)
                    } else if enabled {
                        theme::accent().render(&text)
                    } else {
                        theme::dim().render(&text)
                    }
                };
                block.push(format!(
                    "  {}  {}",
                    button(
                        "apply",
                        self.focus == self.choices.len(),
                        self.anything_chosen()
                    ),
                    button("cancel", self.focus == self.choices.len() + 1, true)
                ));
                let refs: Vec<&str> = block.iter().map(String::as_str).collect();
                lines.push(join_vertical(LEFT, &refs));
                lines.push(
                    Style::new()
                        .border(rounded_border())
                        .border_foreground(theme::border(false))
                        .render(&self.preview.view()),
                );
            }
        }
        lines.push(widgets::footer(&[
            ("tab", "move"),
            ("space", "choose"),
            ("pgup/pgdn", "report"),
            ("esc", "back"),
        ]));
        lines.join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use knixl_pipeline::updates::{Bump, ClosureDiff, VersionChange};

    fn model() -> UpdatesModel {
        let mut m = UpdatesModel {
            state: State::Running,
            spinner: new_spinner(),
            preview: viewport::new(40, 5),
            choices: Vec::new(),
            focus: 0,
            summary: Vec::new(),
        };
        m.on_report(vec![HostReport {
            host: "fw13".into(),
            release: "25.11".into(),
            old_rev: "a".repeat(40),
            new_rev: "b".repeat(40),
            diff: ClosureDiff {
                changed: vec![VersionChange {
                    pname: "curl".into(),
                    old: vec!["8.19.0".into()],
                    new: vec!["8.20.0".into()],
                    bump: Bump::Minor,
                }],
                ..Default::default()
            },
            security: None,
            direct: vec!["curl".into()],
        }]);
        m
    }

    fn key(code: KeyCode) -> Msg {
        Box::new(KeyMsg {
            key: code,
            modifiers: KeyModifiers::NONE,
        }) as Msg
    }

    #[test]
    fn choices_render_as_one_left_aligned_block() {
        let v = model().view((120, 40));
        let strip = |l: &str| {
            let mut out = String::new();
            let mut esc = false;
            for c in l.chars() {
                match (esc, c) {
                    (false, '\u{1b}') => esc = true,
                    (true, 'm') => esc = false,
                    (true, _) => {}
                    (false, c) => out.push(c),
                }
            }
            out
        };
        let rows: Vec<String> = v
            .lines()
            .map(strip)
            .filter(|l| l.contains("take the whole") || l.contains("pin curl"))
            .collect();
        assert_eq!(rows.len(), 2, "{v}");
        let toggle_at = |l: &str| l.chars().position(|c| c == '(');
        assert_eq!(toggle_at(&rows[0]), toggle_at(&rows[1]), "{rows:?}");
    }

    #[test]
    fn offers_the_refresh_and_a_pin_per_direct_update() {
        let m = model();
        assert_eq!(m.choices.len(), 3);
        assert!(
            matches!(&m.choices[2].0, Choice::Pin { attr, version, .. } if attr == "curl" && version == "8.20.0")
        );
        assert!(!m.anything_chosen(), "apply starts off");
    }

    #[test]
    fn choosing_a_pin_applies_just_that_pin() {
        let mut m = model();
        m.focus = 2;
        m.update(key(KeyCode::Char(' ')), (80, 24));
        m.focus = 3; // apply
        match m.update(key(KeyCode::Enter), (80, 24)).nav {
            Nav::ApplyUpdates { refresh, pins, .. } => {
                assert!(!refresh);
                assert_eq!(pins, vec![("fw13".into(), "curl".into(), "8.20.0".into())]);
            }
            _ => panic!("expected apply"),
        }
    }

    #[test]
    fn the_refresh_supersedes_pins() {
        let mut m = model();
        for i in [0, 1, 2] {
            m.focus = i;
            m.update(key(KeyCode::Char(' ')), (80, 24));
        }
        m.focus = 3;
        match m.update(key(KeyCode::Enter), (80, 24)).nav {
            Nav::ApplyUpdates {
                refresh,
                if_security,
                pins,
            } => {
                assert!(refresh && if_security);
                assert!(pins.is_empty());
            }
            _ => panic!("expected apply"),
        }
    }
}