starling-devex 0.1.0

Starling: a local dev orchestrator with a central daemon, shared named-URL proxy, and a k9s-style TUI (a Rust port of Tilt + portless)
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
//! k9s-style terminal dashboard for Starling.
//!
//! Connects to the daemon, polls the aggregated state across all `starling up`
//! instances, and renders a navigable resource table with a live log pane.
//!
//! Keys:
//!   j/k, ↑/↓   move selection
//!   /          filter (Enter apply, Esc clear)
//!   Enter      detail view for the selected resource (Esc to exit)
//!   t          trigger the selected resource
//!   R          restart the selected resource's serve_cmd
//!   PgUp/PgDn  scroll logs (G / End jumps back to follow)
//!   r          refresh now
//!   q          quit

use std::io;
use std::time::{Duration, Instant};

use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use crossterm::execute;
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use ratatui::backend::CrosstermBackend;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Cell, Paragraph, Row, Table, TableState};
use ratatui::{Frame, Terminal};

use crate::daemon::client::DaemonClient;
use crate::daemon::protocol::{DashboardState, Request, Response};

/// One displayed row: a resource belonging to an instance.
#[derive(Clone)]
struct RowItem {
    instance_id: String,
    instance_name: String,
    name: String,
    kind: String,
    update: String,
    runtime: String,
    pod: String,
    url: String,
}

#[derive(PartialEq)]
enum Mode {
    Normal,
    Filter,
    Detail,
}

pub async fn run(proxy_port: u16, tld: &str, tls: bool) {
    let client = DaemonClient::new();
    if let Err(e) = client.ensure_running(proxy_port, tld, tls).await {
        eprintln!("starling: {e}");
        return;
    }
    if let Err(e) = run_ui(client).await {
        eprintln!("starling tui error: {e}");
    }
}

async fn run_ui(client: DaemonClient) -> io::Result<()> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let res = event_loop(&mut terminal, &client).await;

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;
    res
}

struct App {
    state: DashboardState,
    rows: Vec<RowItem>,
    table: TableState,
    logs: Vec<String>,
    mode: Mode,
    filter: String,
    /// Lines scrolled up from the bottom; 0 = follow tail.
    log_scroll: usize,
    status_msg: String,
}

impl App {
    fn selected(&self) -> Option<&RowItem> {
        self.table.selected().and_then(|i| self.rows.get(i))
    }
}

async fn event_loop(
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    client: &DaemonClient,
) -> io::Result<()> {
    let mut app = App {
        state: DashboardState::default(),
        rows: vec![],
        table: TableState::default().with_selected(Some(0)),
        logs: vec![],
        mode: Mode::Normal,
        filter: String::new(),
        log_scroll: 0,
        status_msg: String::new(),
    };
    let mut last_refresh = Instant::now() - Duration::from_secs(1);

    loop {
        if last_refresh.elapsed() >= Duration::from_millis(500) {
            if let Ok(Response::State(s)) = client.call(&Request::GetState).await {
                app.state = s;
            }
            app.rows = filtered(&app.state, &app.filter);
            let sel = app
                .table
                .selected()
                .unwrap_or(0)
                .min(app.rows.len().saturating_sub(1));
            app.table.select(if app.rows.is_empty() { None } else { Some(sel) });
            app.logs = match app.selected() {
                Some(r) => fetch_logs(client, &r.instance_id, &r.name).await,
                None => vec![],
            };
            last_refresh = Instant::now();
        }

        terminal.draw(|f| draw(f, &mut app))?;

        if event::poll(Duration::from_millis(250))? {
            if let Event::Key(key) = event::read()? {
                if key.kind != KeyEventKind::Press {
                    continue;
                }
                let ctrl_c = key.modifiers.contains(KeyModifiers::CONTROL)
                    && key.code == KeyCode::Char('c');
                if ctrl_c {
                    break;
                }

                match app.mode {
                    Mode::Filter => match key.code {
                        KeyCode::Enter => app.mode = Mode::Normal,
                        KeyCode::Esc => {
                            app.filter.clear();
                            app.mode = Mode::Normal;
                        }
                        KeyCode::Backspace => {
                            app.filter.pop();
                        }
                        KeyCode::Char(c) => app.filter.push(c),
                        _ => {}
                    },
                    Mode::Normal | Mode::Detail => match key.code {
                        KeyCode::Char('q') => break,
                        KeyCode::Esc if app.mode == Mode::Detail => app.mode = Mode::Normal,
                        KeyCode::Esc => break,
                        KeyCode::Char('j') | KeyCode::Down => {
                            move_sel(&mut app, 1);
                        }
                        KeyCode::Char('k') | KeyCode::Up => {
                            move_sel(&mut app, -1);
                        }
                        KeyCode::Enter => {
                            app.mode = if app.mode == Mode::Detail {
                                Mode::Normal
                            } else {
                                Mode::Detail
                            };
                        }
                        KeyCode::Char('/') => {
                            app.mode = Mode::Filter;
                        }
                        KeyCode::Char('r') => {
                            last_refresh = Instant::now() - Duration::from_secs(1);
                        }
                        KeyCode::PageUp => app.log_scroll += 5,
                        KeyCode::PageDown => app.log_scroll = app.log_scroll.saturating_sub(5),
                        KeyCode::Char('G') | KeyCode::End => app.log_scroll = 0,
                        KeyCode::Char('t') => {
                            if let Some(r) = app.selected() {
                                let _ = client
                                    .call(&Request::Trigger {
                                        instance: r.instance_id.clone(),
                                        resource: r.name.clone(),
                                    })
                                    .await;
                                app.status_msg = format!("triggered {}", r.name);
                            }
                        }
                        KeyCode::Char('R') => {
                            if let Some(r) = app.selected() {
                                let _ = client
                                    .call(&Request::Restart {
                                        instance: r.instance_id.clone(),
                                        resource: r.name.clone(),
                                    })
                                    .await;
                                app.status_msg = format!("restarting {}", r.name);
                            }
                        }
                        _ => {}
                    },
                }
            }
        }
    }
    Ok(())
}

fn move_sel(app: &mut App, delta: i32) {
    if app.rows.is_empty() {
        return;
    }
    let cur = app.table.selected().unwrap_or(0) as i32;
    let next = (cur + delta).rem_euclid(app.rows.len() as i32);
    app.table.select(Some(next as usize));
    app.log_scroll = 0;
}

fn filtered(state: &DashboardState, filter: &str) -> Vec<RowItem> {
    let f = filter.to_ascii_lowercase();
    let mut rows = vec![];
    for inst in &state.instances {
        for r in &inst.resources {
            let item = RowItem {
                instance_id: inst.id.clone(),
                instance_name: inst.name.clone(),
                name: r.name.clone(),
                kind: r.kind.clone(),
                update: r.update_status.clone(),
                runtime: r.runtime_status.clone(),
                pod: r.pod.clone().unwrap_or_default(),
                url: r.url.clone().unwrap_or_default(),
            };
            if f.is_empty()
                || item.name.to_ascii_lowercase().contains(&f)
                || item.instance_name.to_ascii_lowercase().contains(&f)
            {
                rows.push(item);
            }
        }
    }
    rows
}

async fn fetch_logs(client: &DaemonClient, instance: &str, resource: &str) -> Vec<String> {
    match client
        .call(&Request::GetLogs {
            instance: instance.to_string(),
            resource: resource.to_string(),
        })
        .await
    {
        Ok(Response::Logs(l)) => l,
        _ => vec![],
    }
}

fn status_style(s: &str) -> Style {
    match s {
        "ok" => Style::default().fg(Color::Green),
        "error" => Style::default().fg(Color::Red),
        "in_progress" => Style::default().fg(Color::Yellow),
        "pending" => Style::default().fg(Color::Cyan),
        "not_applicable" | "none" | "" => Style::default().fg(Color::DarkGray),
        _ => Style::default(),
    }
}

/// Render the tail of `logs` into a pane of inner height `h`, honoring scroll.
fn log_lines(logs: &[String], h: usize, scroll: usize) -> Vec<Line<'static>> {
    if logs.is_empty() {
        return vec![];
    }
    let max_scroll = logs.len().saturating_sub(h);
    let scroll = scroll.min(max_scroll);
    let end = logs.len() - scroll;
    let start = end.saturating_sub(h);
    logs[start..end].iter().map(|l| Line::raw(l.clone())).collect()
}

fn draw(f: &mut Frame, app: &mut App) {
    if app.mode == Mode::Detail {
        draw_detail(f, app);
        return;
    }

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(6),
            Constraint::Length(10),
            Constraint::Length(1),
        ])
        .split(f.area());

    let title = format!(
        " Starling  ·  {} instance(s)  ·  {} resource(s)  ·  shared proxy :{}  ·  .{} ",
        app.state.instances.len(),
        app.rows.len(),
        app.state.proxy_port,
        app.state.tld,
    );
    f.render_widget(
        Paragraph::new(title).style(
            Style::default()
                .fg(Color::White)
                .bg(Color::Blue)
                .add_modifier(Modifier::BOLD),
        ),
        chunks[0],
    );

    let header = Row::new(
        ["INSTANCE", "RESOURCE", "TYPE", "UPDATE", "RUNTIME", "POD", "URL"]
            .iter()
            .map(|h| Cell::from(*h).style(Style::default().add_modifier(Modifier::BOLD))),
    );
    let table_rows: Vec<Row> = app
        .rows
        .iter()
        .map(|r| {
            Row::new(vec![
                Cell::from(r.instance_name.clone()),
                Cell::from(r.name.clone()),
                Cell::from(r.kind.clone()),
                Cell::from(r.update.clone()).style(status_style(&r.update)),
                Cell::from(r.runtime.clone()).style(status_style(&r.runtime)),
                Cell::from(r.pod.clone()),
                Cell::from(r.url.clone()).style(Style::default().fg(Color::Blue)),
            ])
        })
        .collect();
    let widths = [
        Constraint::Length(14),
        Constraint::Length(18),
        Constraint::Length(6),
        Constraint::Length(12),
        Constraint::Length(14),
        Constraint::Length(20),
        Constraint::Min(20),
    ];
    let table = Table::new(table_rows, widths)
        .header(header)
        .block(Block::default().borders(Borders::ALL).title(" Resources "))
        .highlight_style(Style::default().bg(Color::DarkGray).add_modifier(Modifier::BOLD))
        .highlight_symbol("");
    f.render_stateful_widget(table, chunks[1], &mut app.table);

    let sel_name = app
        .selected()
        .map(|r| format!("{} / {}", r.instance_name, r.name))
        .unwrap_or_else(|| "".into());
    let h = chunks[2].height.saturating_sub(2) as usize;
    let follow = if app.log_scroll == 0 { "" } else { " (scrolled)" };
    f.render_widget(
        Paragraph::new(log_lines(&app.logs, h, app.log_scroll)).block(
            Block::default()
                .borders(Borders::ALL)
                .title(format!(" Logs · {sel_name}{follow} ")),
        ),
        chunks[2],
    );

    let footer = if app.mode == Mode::Filter {
        format!(" /{}\u{2588}   (Enter apply · Esc clear) ", app.filter)
    } else if app.rows.is_empty() {
        " No resources. Run `starling up` in a project.   [/] filter  [q] quit ".to_string()
    } else if !app.status_msg.is_empty() {
        format!(
            " {}   ·   [↵] detail [t] trigger [R] restart [/] filter [q] quit ",
            app.status_msg
        )
    } else {
        " [j/k] move  [↵] detail  [t] trigger  [R] restart  [/] filter  [PgUp/Dn] logs  [q] quit "
            .to_string()
    };
    f.render_widget(
        Paragraph::new(Span::styled(footer, Style::default().fg(Color::Gray))),
        chunks[3],
    );
}

fn draw_detail(f: &mut Frame, app: &mut App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(8),
            Constraint::Min(4),
            Constraint::Length(1),
        ])
        .split(f.area());

    let r = app.selected().cloned().unwrap_or(RowItem {
        instance_id: String::new(),
        instance_name: "".into(),
        name: "".into(),
        kind: String::new(),
        update: String::new(),
        runtime: String::new(),
        pod: String::new(),
        url: String::new(),
    });

    f.render_widget(
        Paragraph::new(format!(" Detail · {} / {} ", r.instance_name, r.name)).style(
            Style::default()
                .fg(Color::White)
                .bg(Color::Blue)
                .add_modifier(Modifier::BOLD),
        ),
        chunks[0],
    );

    let info = vec![
        Line::from(vec![Span::styled("instance: ", bold()), Span::raw(r.instance_name.clone())]),
        Line::from(vec![Span::styled("resource: ", bold()), Span::raw(r.name.clone())]),
        Line::from(vec![Span::styled("type:     ", bold()), Span::raw(r.kind.clone())]),
        Line::from(vec![Span::styled("update:   ", bold()), Span::styled(r.update.clone(), status_style(&r.update))]),
        Line::from(vec![Span::styled("runtime:  ", bold()), Span::styled(r.runtime.clone(), status_style(&r.runtime))]),
        Line::from(vec![Span::styled("pod:      ", bold()), Span::raw(r.pod.clone())]),
        Line::from(vec![Span::styled("url:      ", bold()), Span::styled(r.url.clone(), Style::default().fg(Color::Blue))]),
    ];
    f.render_widget(
        Paragraph::new(info).block(Block::default().borders(Borders::ALL).title(" Status ")),
        chunks[1],
    );

    let h = chunks[2].height.saturating_sub(2) as usize;
    f.render_widget(
        Paragraph::new(log_lines(&app.logs, h, app.log_scroll))
            .block(Block::default().borders(Borders::ALL).title(" Logs ")),
        chunks[2],
    );

    f.render_widget(
        Paragraph::new(Span::styled(
            " [Esc/↵] back   [t] trigger   [R] restart   [PgUp/Dn] scroll logs   [q] quit ",
            Style::default().fg(Color::Gray),
        )),
        chunks[3],
    );
}

fn bold() -> Style {
    Style::default().add_modifier(Modifier::BOLD)
}