shep 0.24.2

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! `shep lookout <sheep> [tab] [dog]`: the screen the operator asked for,
//! resolved against the first flock answer.
//!
//! The command line can name a sheep the shepherd is still restoring, so the
//! ask is held until the first `ListFlock` reply can say whether it is there.
//! A sheep the flock does not have is reported and dropped: the dashboard
//! opens on the flock table with a notice naming it, and the operator's own
//! keys are the only way to try again.

use shep_core::config::GROUP_ORDER;

use super::*;

/// The screen `shep lookout <sheep> [tab] [dog]` asked for.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Focus {
    /// The sheep's name, as `shep describe` takes it.
    pub sheep: String,
    /// The config pane's group to open on, or `None` for the sheep's own
    /// status page.
    pub tab: Option<&'static str>,
    /// The dog whose table to open. The parse admits one only with the
    /// `dogs` group as its tab.
    pub dog: Option<String>,
}

impl Focus {
    /// Builds the ask from what the parse accepted.
    ///
    /// `tab` is resolved against [`GROUP_ORDER`], the same list the pane
    /// draws its tabs from. A name outside it cannot reach here, because
    /// clap's own possible values are that list; one that somehow did opens
    /// the pane on its first group rather than on a group that does not
    /// exist.
    #[must_use]
    pub(crate) fn new(sheep: String, tab: Option<&str>, dog: Option<String>) -> Self {
        Self {
            sheep,
            tab: tab.map(|name| {
                GROUP_ORDER
                    .iter()
                    .copied()
                    .find(|group| *group == name)
                    .unwrap_or(GROUP_ORDER[0])
            }),
            dog,
        }
    }
}

/// The half of the ask that outlives the first flock answer, keyed to the
/// sheep it was asked for.
///
/// The key is the point. A keystroke can ask for another sheep's config
/// while this one's read is in flight. A tab and a dog left over from that
/// ask would open the next sheep's pane on a tab its operator never named.
#[derive(Debug, Clone)]
pub(crate) struct FocusAsk {
    /// The sheep the command line named, and the only pane this ask may open.
    pub sheep: String,
    /// The config pane's group to open on.
    pub tab: Option<&'static str>,
    /// The dog whose table to open, once that tab's listing names it.
    pub dog: Option<String>,
}

impl App {
    /// Says which screen this lookout should open on, before it has a flock
    /// to open it on. Called once, from `crate::lookout::entry`.
    pub(crate) fn set_focus(&mut self, focus: Focus) {
        self.focus = Some(focus);
    }

    /// The first flock answer's chance to open the screen the command line
    /// asked for. `None` when nothing was asked for, when the answer has been
    /// spent, and when it could not be honoured, in which case the notice
    /// names what was not found. Spent on the first answer whatever it says.
    ///
    /// The sheep is the one the flock answers with, not the row the table
    /// selects: an app with instances has several, and the lowest id wins.
    pub(super) fn resolve_focus(&mut self) -> Option<Effect> {
        let focus = self.focus.take()?;
        let Some((id, name)) = self
            .flock
            .iter()
            .find(|(_, row)| row.info.dog.is_none() && row.info.name == focus.sheep)
            .map(|(id, row)| (*id, row.info.name.clone()))
        else {
            self.notice = Some(Notice::grave(format!("no sheep named {}", focus.sheep)));
            return None;
        };
        self.selected = Some(RowKey::Sheep(id));
        let Some(tab) = focus.tab else {
            return Some(self.open_sheep_pane());
        };
        self.focus_ask = Some(FocusAsk {
            sheep: name.clone(),
            tab: Some(tab),
            dog: focus.dog,
        });
        Some(self.ask_for_sheep_config(name, ConfigFor::Editor))
    }

    /// The tab [`Self::resolve_focus`] asked the config pane to open on,
    /// spent by the pane that opens.
    pub(super) fn take_focus_tab(&mut self) -> Option<&'static str> {
        self.focus_ask.as_mut()?.tab.take()
    }

    /// The dog [`Self::resolve_focus`] asked the `dogs` tab to open, spent by
    /// the listing that names it.
    pub(super) fn take_focus_dog(&mut self) -> Option<String> {
        self.focus_ask.as_mut()?.dog.take()
    }
}

#[cfg(test)]
mod tests {
    use super::super::testing::*;
    use super::*;
    use crate::lookout::view::fixtures;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::layout::Rect;
    use shep_core::config::AppConfig;
    use shep_core::protocol::{RpcError, RpcErrorCode};

    /// A dashboard that was told to open on `focus`, with no flock yet: the
    /// state `crate::lookout::entry` leaves `App` in before the first poll.
    fn awaiting(focus: Focus) -> App {
        let mut app = App::new(
            Palette::detect(None, None, None),
            Control::ReadOnly,
            "/home/ada/.shep".to_string(),
            Instant::now(),
        );
        app.set_focus(focus);
        app
    }

    /// `web`, `api` and `worker`, the flock every test here polls.
    fn flock() -> Vec<ProcessInfo> {
        vec![
            sheep(1, "web", ProcStatus::Online),
            sheep(2, "api", ProcStatus::Online),
            sheep(3, "worker", ProcStatus::Online),
        ]
    }

    /// The first frame, sized and prepared the way `run_ui` prepares one.
    fn first_frame(app: &mut App) -> String {
        let area = Rect::new(0, 0, 160, 48);
        app.note_body_rows(crate::lookout::view::body_rows(area));
        app.note_body_width(area.width);
        let mut terminal = Terminal::new(TestBackend::new(area.width, area.height)).unwrap();
        terminal
            .draw(|frame| crate::lookout::view::draw(app, frame))
            .unwrap();
        crate::lookout::frames::render_text(terminal.backend().buffer())
    }

    /// The `jobs` row of the open pane.
    fn jobs_row(app: &App) -> DogRow {
        app.config_pane()
            .expect("a pane")
            .dogs_tab()
            .rows()
            .iter()
            .find(|row| row.name() == "jobs")
            .expect("jobs is listed")
            .clone()
    }

    /// `<sheep>` opens the sheep pane and asks for the config its own column
    /// has nothing to draw without, the same pair `↵` raises.
    #[test]
    fn a_named_sheep_opens_its_status_page_on_the_first_answer() {
        let mut app = awaiting(Focus::new("web".into(), None, None));
        let effect = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        assert_eq!(
            effect,
            Effect::Send(Sent::SheepConfig {
                name: "web".to_string()
            }),
            "the pane's read goes out with the answer that found the sheep"
        );
        assert!(matches!(app.body(), Body::Sheep(_)));
        assert_eq!(app.selected(), Some(RowKey::Sheep(1)));

        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(
                fixtures::sheep_config_view(),
            ))),
        });
        let frame = first_frame(&mut app);
        assert!(
            frame.contains("██ SHEEP 1  web   online"),
            "the first frame is the sheep pane:\n{frame}"
        );
        assert!(!frame.contains("STATUS"), "and not the flock table");
    }

    /// `<sheep> <tab>` opens the config pane `e` opens, already on that tab.
    #[test]
    fn a_named_sheep_and_a_tab_open_the_config_pane_on_that_tab() {
        let mut app = awaiting(Focus::new("web".into(), Some("watch"), None));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(
                fixtures::sheep_config_view(),
            ))),
        });
        assert_eq!(
            app.config_pane().expect("the pane is open").group(),
            "watch"
        );

        let frame = first_frame(&mut app);
        assert!(
            frame.contains("██ web  (sheep config)"),
            "the first frame is the config pane:\n{frame}"
        );
        assert!(
            frame.contains(">  ignore_watch") && frame.contains("watch_delay"),
            "and it is drawn on the watch tab's own fields:\n{frame}"
        );
        assert!(
            !frame.contains(">  cwd"),
            "the cursor is not on the process tab's first field:\n{frame}"
        );
    }

    /// `<sheep> dogs <dog>` asks for the listing the tab needs and then for
    /// that dog's schema, without a keystroke between the two.
    #[test]
    fn a_named_dog_opens_its_table_on_the_dogs_tab() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("jobs".into())));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let effect = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        let Effect::ListSheepDogs { sheep, ask } = effect else {
            panic!("the dogs tab asks for its listing: {effect:?}");
        };
        assert_eq!(sheep, "web");

        let effect = app.update(Msg::SheepDogNames {
            sheep: "web".into(),
            ask,
            dogs: listing(),
        });
        assert_eq!(
            effect,
            Effect::LoadSheepDog {
                sheep: "web".into(),
                dog: "jobs".into(),
                adopted_path: Some("/opt/jobs".into()),
            },
            "the listing's answer opens the asked-for row"
        );
        let _ = app.update(Msg::SheepDog {
            sheep: "web".into(),
            entry: jobs_entry(),
        });

        let frame = first_frame(&mut app);
        assert!(
            frame.contains("██ web  (sheep config)"),
            "the first frame is the config pane:\n{frame}"
        );
        assert!(frame.contains("â–¾ jobs"), "with jobs's table open");
        assert!(frame.contains("concurrency"), "and its rows drawn");
    }

    /// A sheep the first answer does not have is reported, not retried, and
    /// the dashboard is what opens.
    #[test]
    fn an_unknown_sheep_lands_on_the_dashboard_with_a_notice() {
        let mut app = awaiting(Focus::new("ghost".into(), None, None));
        let effect = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        assert_eq!(effect, Effect::RefreshFeed, "the dashboard's own read");
        assert!(matches!(app.body(), Body::FlockTable));
        assert_eq!(
            app.notice().map(ToString::to_string).as_deref(),
            Some("no sheep named ghost")
        );

        let frame = first_frame(&mut app);
        assert!(
            frame.contains("STATUS"),
            "the flock table is drawn:\n{frame}"
        );
        assert!(
            frame.contains("no sheep named ghost"),
            "and the notice names what was not found:\n{frame}"
        );
    }

    /// A dog the tab's listing does not name is reported the same way, and
    /// the tab it was asked for stays open.
    #[test]
    fn an_unknown_dog_lands_on_the_dogs_tab_with_a_notice() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("nope".into())));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let Effect::ListSheepDogs { ask, .. } = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        }) else {
            panic!("the dogs tab asks for its listing");
        };
        let effect = app.update(Msg::SheepDogNames {
            sheep: "web".into(),
            ask,
            dogs: listing(),
        });
        assert_eq!(effect, Effect::None);
        let notice = app.notice().expect("a refusal").to_string();
        assert_eq!(notice, "web: no dog named nope", "{notice}");
        assert_eq!(
            app.config_pane().expect("the tab stays open").group(),
            "dogs"
        );
    }

    /// Over `--host` the pane still opens on the tab the command line named.
    /// The listing a dog's table needs is read off this machine's files, so
    /// the tab says that instead of opening it, exactly as `tab` over a host
    /// does.
    #[test]
    fn a_dog_ask_over_a_host_stops_at_the_tab() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("jobs".into())));
        app.set_remote_host("build-01");
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let effect = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        assert_eq!(effect, Effect::None, "no listing ask over a host");
        assert_eq!(
            app.config_pane().expect("the tab still opens").group(),
            "dogs"
        );
        assert_eq!(
            app.notice().map(ToString::to_string).as_deref(),
            Some(NOT_OVER_HOST),
            "and the tab says why it has no rows"
        );
    }

    /// `api`, with no dogs and nothing overridden: the sheep an operator
    /// walked to with `j`.
    fn api_view() -> SheepConfigView {
        SheepConfigView::new(
            AppConfig {
                name: "api".into(),
                ..AppConfig::default()
            },
            Vec::new(),
            Vec::new(),
        )
    }

    /// `shep lookout web watch`, then `j` and `e` on another sheep before
    /// `web`'s read lands. That read is dropped, and the pane that opens next
    /// is the operator's own, on its own first tab.
    #[test]
    fn a_walk_to_another_sheep_retires_the_tab_it_was_asked_for() {
        let mut app = awaiting(Focus::new("web".into(), Some("watch"), None));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        // Rows sit in name order, `api`, `web`, `worker`, so `k` from the
        // sheep the command line named lands on another one.
        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        let effect = app.update(Msg::Key(KeyPress::Edit));
        assert_eq!(
            effect,
            Effect::Send(Sent::SheepConfig {
                name: "api".to_string()
            }),
            "the keystroke asks for the sheep it moved to"
        );

        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(false)))),
        });
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "api".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(api_view()))),
        });

        let pane = app.config_pane().expect("api's pane opens");
        assert_eq!(pane.target().name(), "api");
        assert_eq!(pane.group(), "process", "not the tab web was named for");
    }

    /// The same walk with `shep lookout web dogs jobs`: the dog's table is
    /// retired with the tab, so the other sheep's `dogs` tab lists `jobs` as
    /// any other row and opens nothing.
    #[test]
    fn a_walk_to_another_sheep_retires_the_dog_it_was_asked_for() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("jobs".into())));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        let _ = app.update(Msg::Key(KeyPress::Edit));
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "api".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(api_view()))),
        });

        let Effect::ListSheepDogs { sheep, ask } = app.update(Msg::Key(KeyPress::Group(9))) else {
            panic!(
                "the tab asks for its own listing: {:?}",
                app.config_pane().map(|p| p.group())
            );
        };
        assert_eq!(sheep, "api");
        let effect = app.update(Msg::SheepDogNames {
            sheep,
            ask,
            dogs: listing(),
        });
        assert_eq!(
            effect,
            Effect::None,
            "no table opens on a sheep that was not named"
        );
        assert!(
            app.notice().is_none(),
            "{:?}",
            app.notice().map(ToString::to_string)
        );
        assert!(!jobs_row(&app).is_open(), "jobs is a row like any other");
    }

    /// The ask is spent on the first answer, so a later poll cannot reopen a
    /// screen the operator has since closed.
    #[test]
    fn the_ask_is_spent_once() {
        let mut app = awaiting(Focus::new("web".into(), None, None));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert!(matches!(app.body(), Body::FlockTable));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        assert!(matches!(app.body(), Body::FlockTable), "no reopening");
    }

    /// Closing a pane the command line opened drops the rest of the ask: the
    /// operator is driving from here, and a dog left unopened is not a promise
    /// the next pane has to keep.
    #[test]
    fn closing_a_pane_drops_the_rest_of_the_ask() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("jobs".into())));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert!(app.config_pane().is_none(), "the pane closed");

        let _ = app.update(Msg::Key(KeyPress::Edit));
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        let Effect::ListSheepDogs { ask, .. } = app.update(Msg::Key(KeyPress::Group(9))) else {
            panic!("the tab asks for its listing again");
        };
        let _ = app.update(Msg::SheepDogNames {
            sheep: "web".into(),
            ask,
            dogs: listing(),
        });
        assert!(
            app.notice().is_none(),
            "{:?}",
            app.notice().map(ToString::to_string)
        );
        assert!(!jobs_row(&app).is_open(), "jobs stays closed");
    }

    /// A refused config read reports itself and drops the tab and the dog
    /// that were waiting on it, so a later `e` opens on the pane's own first
    /// tab rather than on a stale ask.
    #[test]
    fn a_refused_config_read_drops_the_ask_it_was_waiting_on() {
        let mut app = awaiting(Focus::new("web".into(), Some("dogs"), Some("jobs".into())));
        let _ = app.update(Msg::Snapshot {
            rows: flock(),
            at: Instant::now(),
        });
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Err(RequestError::Rpc(RpcError {
                code: RpcErrorCode::NotFound,
                message: "no sheep named web".to_string(),
                daemon_version: None,
            })),
        });
        assert!(app.config_pane().is_none());

        let _ = app.update(Msg::Key(KeyPress::Edit));
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig {
                name: "web".to_string(),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        assert_eq!(
            app.config_pane().expect("the pane opens").group(),
            "process",
            "the pane opens on its own first tab"
        );
    }

    /// A name the flock answers with as a dog is not a sheep, and the notice
    /// says the same sentence it says for a name in no flock at all.
    #[test]
    fn a_dog_is_not_a_sheep() {
        let mut app = awaiting(Focus::new("otel".into(), None, None));
        let _ = app.update(Msg::Snapshot {
            rows: vec![
                ProcessInfo::builder(90, "otel", ProcStatus::Online)
                    .dog(Some(DogSource::BuiltIn {
                        grants: None,
                        events: Vec::new(),
                    }))
                    .build(),
            ],
            at: Instant::now(),
        });
        assert!(matches!(app.body(), Body::FlockTable));
        assert_eq!(
            app.notice().map(ToString::to_string).as_deref(),
            Some("no sheep named otel")
        );
    }
}