taba-cli 0.1.0-rc.3

The Taba client command line.
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
//! The Tokens tab (doc 34, section 6.5): the tokens of the server, creating, editing, cloning and revoking them.
//! This file is the state and the keys; `view.rs` draws it. The secret of a new token never leaves the popup that
//! shows it (TUI-TOK-03).

use taba_client::json;

use crate::tui::api::{ApiCall, Severity, Token};
use crate::tui::editor::{Editor, Field, Outcome, Purpose};
use crate::tui::effect::Effect;
use crate::tui::i18n;
use crate::tui::keymap::Action;
use crate::tui::model::{
    Confirm, Fail, Input, InputPurpose, Modal, Model, Pending, Secret, TokenSort,
};
use crate::tui::screens::cluster::moved;
use crate::tui::update::run;

pub mod view;

/// The permissions a token can have, in the order the form lists them.
pub const PERMISSIONS: [&str; 5] = ["publish", "consume", "create", "manage", "admin"];

/// After this long the secret on the screen is masked (TUI-TOK-03), in milliseconds.
pub const MASK_AFTER_MS: u64 = 30_000;

/// A token that expires within this many days is shown in yellow (TUI-TOK-01).
pub const SOON_DAYS: i64 = 7;

/// How many pages of tokens one read follows before it gives up (200 tokens each).
pub const MAX_PAGES: u32 = 25;

/// What the tab can do with this token and this server.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Availability {
    /// `GET /auth/me` has not answered yet.
    Unknown,
    /// The server runs without authentication: there are no tokens (`404`).
    NoAuth,
    /// Only the root token and the `admin` tokens manage tokens.
    NotAdmin,
    Ready,
}

pub fn availability(m: &Model) -> Availability {
    match &m.me.data {
        None => Availability::Unknown,
        Some(me) if !me.auth_enabled => Availability::NoAuth,
        Some(me) if !me.is_admin() => Availability::NotAdmin,
        Some(_) => Availability::Ready,
    }
}

/// The tokens shown: those whose name has the filter in it, in the chosen order.
pub fn visible(m: &Model) -> Vec<Token> {
    let wanted = m.tokens.filter.to_lowercase();
    let mut tokens: Vec<Token> = m
        .tokens
        .list
        .data
        .iter()
        .flatten()
        .filter(|t| wanted.is_empty() || t.name.to_lowercase().contains(&wanted))
        .cloned()
        .collect();
    match m.tokens.sort {
        TokenSort::NewestFirst => tokens.sort_by(|a, b| b.created_at.cmp(&a.created_at)),
        TokenSort::OldestFirst => tokens.sort_by(|a, b| a.created_at.cmp(&b.created_at)),
        TokenSort::Name => tokens.sort_by_key(|t| t.name.to_lowercase()),
        TokenSort::Id => tokens.sort_by(|a, b| a.id.cmp(&b.id)),
    }
    tokens
}

/// The index of the selected token in `tokens`; the first one if the selection is gone.
pub fn selected_index(m: &Model, tokens: &[Token]) -> Option<usize> {
    if tokens.is_empty() {
        return None;
    }
    let found = m
        .tokens
        .selected
        .as_ref()
        .and_then(|id| tokens.iter().position(|t| &t.id == id));
    Some(found.unwrap_or(0))
}

pub fn selected(m: &Model) -> Option<Token> {
    let tokens = visible(m);
    selected_index(m, &tokens).map(|i| tokens[i].clone())
}

/// Why an action cannot run now, as a catalog key; `None` means it can (TUI-FRM-10).
pub fn gate(m: &Model, action: Action) -> Option<&'static str> {
    match availability(m) {
        Availability::Unknown => return Some("gate.tokens-unknown"),
        Availability::NoAuth => return Some("gate.tokens-no-auth"),
        Availability::NotAdmin => return Some("gate.not-admin"),
        Availability::Ready => {}
    }
    if m.busy.is_some() {
        return Some("gate.busy");
    }
    match action {
        Action::TokenEdit | Action::TokenClone | Action::TokenRevoke if selected(m).is_none() => {
            Some("gate.no-token")
        }
        // An `admin` token is only changed and revoked by the root token (the server answers `403` otherwise).
        Action::TokenEdit | Action::TokenRevoke
            if !m.is_root()
                && selected(m).is_some_and(|t| t.permissions.iter().any(|p| p == "admin")) =>
        {
            Some("gate.root-for-admin")
        }
        _ => None,
    }
}

fn refuse(m: &mut Model, key: &str) -> Vec<Effect> {
    let text = m.tx().t(key);
    m.notify(Severity::Warning, text);
    Vec::new()
}

pub fn on_action(m: &mut Model, action: Action) -> Vec<Effect> {
    match action {
        Action::Up => step(m, -1),
        Action::Down => step(m, 1),
        Action::PageUp => step(m, -10),
        Action::PageDown => step(m, 10),
        Action::First => step(m, i32::MIN / 2),
        Action::Last => step(m, i32::MAX / 2),
        Action::Enter => crate::tui::menu::open(m),
        Action::Sort => {
            m.tokens.sort = m.tokens.sort.next();
            Vec::new()
        }
        Action::Filter => {
            m.modal = Some(Modal::Input(Input {
                purpose: InputPurpose::TokenFilter,
                value: m.tokens.filter.clone(),
            }));
            Vec::new()
        }
        Action::TokenNew | Action::TokenEdit | Action::TokenClone | Action::TokenRevoke => {
            if let Some(key) = gate(m, action) {
                return refuse(m, key);
            }
            match action {
                Action::TokenNew => open_create(m, None),
                Action::TokenEdit => open_edit(m),
                Action::TokenClone => open_create(m, selected(m)),
                _ => open_revoke(m),
            }
            Vec::new()
        }
        _ => Vec::new(),
    }
}

fn step(m: &mut Model, delta: i32) -> Vec<Effect> {
    let tokens = visible(m);
    if let Some(current) = selected_index(m, &tokens) {
        let next = moved(current, delta, tokens.len());
        m.tokens.selected = Some(tokens[next].id.clone());
    }
    Vec::new()
}

fn open_create(m: &mut Model, from: Option<Token>) {
    let (name, prefixes, perms, clone_of) = match &from {
        Some(t) => (
            format!("{}-copy", t.name),
            t.prefixes.join(" "),
            t.permissions.clone(),
            Some((t.id.clone(), t.name.clone())),
        ),
        None => (String::new(), String::new(), Vec::new(), None),
    };
    let mut fields = vec![Field::text("name", "editor.field.name", &name)];
    for permission in PERMISSIONS {
        let mut field = Field::check(
            permission,
            permission_label(permission),
            perms.iter().any(|p| p == permission) && (permission != "admin" || m.is_root()),
        );
        // Only the root token creates an `admin` token.
        if permission == "admin" && !m.is_root() {
            field = field.locked("editor.locked.root");
        }
        fields.push(field);
    }
    fields.push(Field::text("prefixes", "editor.field.prefixes", &prefixes));
    fields.push(Field::text("expires", "editor.field.expires", ""));
    m.modal = Some(Modal::Editor(Editor::new(
        Purpose::TokenCreate { clone_of },
        fields,
    )));
}

fn permission_label(permission: &str) -> &'static str {
    match permission {
        "publish" => "editor.perm.publish",
        "consume" => "editor.perm.consume",
        "create" => "editor.perm.create",
        "manage" => "editor.perm.manage",
        _ => "editor.perm.admin",
    }
}

fn open_edit(m: &mut Model) {
    let Some(token) = selected(m) else { return };
    let expires = token.expires_at.clone().unwrap_or_default();
    let mut fields = vec![Field::text("name", "editor.field.name", &token.name)];
    for permission in PERMISSIONS {
        fields.push(
            Field::check(
                permission,
                permission_label(permission),
                token.permissions.iter().any(|p| p == permission),
            )
            .locked("editor.locked.clone"),
        );
    }
    fields.push(
        Field::text(
            "prefixes",
            "editor.field.prefixes",
            &token.prefixes.join(" "),
        )
        .locked("editor.locked.clone"),
    );
    fields.push(Field::text("expires", "editor.field.expires", &expires));
    m.modal = Some(Modal::Editor(Editor::new(
        Purpose::TokenEdit {
            id: token.id,
            name: token.name,
            expires,
        },
        fields,
    )));
}

fn open_revoke(m: &mut Model) {
    if let Some(token) = selected(m) {
        m.modal = Some(Modal::Confirm(Confirm {
            pending: Pending::RevokeToken(token.id, token.name),
            typed: None,
            ok_focused: false,
        }));
    }
}

/// `2026-10-15T12:00:00Z` for a time in milliseconds since the epoch.
pub fn rfc3339(ms: i64) -> String {
    let c = i18n::civil_from_ms(ms);
    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        c.year, c.month, c.day, c.hour, c.minute, c.second
    )
}

/// The text typed as an expiry is not one of the forms `parse_expiry` reads.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BadExpiry;

/// What a person types as an expiry: nothing, `none` or `never` (no deadline), a time from now (`90m`, `12h`,
/// `30d`), a date (`2027-01-31`, the start of that day in UTC) or a full RFC 3339 instant. `Ok(None)` is no
/// deadline.
pub fn parse_expiry(text: &str, now_ms: i64) -> Result<Option<String>, BadExpiry> {
    let text = text.trim();
    if text.is_empty() || text.eq_ignore_ascii_case("none") || text.eq_ignore_ascii_case("never") {
        return Ok(None);
    }
    let relative = text.char_indices().last().and_then(|(at, unit)| {
        let number = text[..at].parse::<i64>().ok().filter(|n| *n > 0)?;
        let step = match unit {
            'm' => 60_000,
            'h' => 3_600_000,
            'd' => 86_400_000,
            _ => return None,
        };
        number.checked_mul(step)
    });
    if let Some(span) = relative {
        return Ok(Some(rfc3339(now_ms.saturating_add(span))));
    }
    let instant = if text.len() == 10 {
        format!("{text}T00:00:00Z")
    } else {
        text.to_owned()
    };
    i18n::parse_instant(&instant).ok_or(BadExpiry)?;
    Ok(Some(instant))
}

fn quoted_list(items: &[String]) -> String {
    let quoted: Vec<String> = items.iter().map(|s| json::quote(s)).collect();
    format!("[{}]", quoted.join(","))
}

/// The form was filled in and the person asked to save it.
pub fn submit(m: &mut Model, mut editor: Editor) -> Vec<Effect> {
    let tx = m.tx();
    let fail = |editor: &mut Editor, field: &str, key: &str, m: &mut Model| {
        editor.error = Some(m.tx().t(key));
        editor.focus_on(field);
        m.modal = Some(Modal::Editor(editor.clone()));
    };
    let name = editor.text("name").to_owned();
    if name.is_empty() {
        fail(&mut editor, "name", "editor.error.name", m);
        return Vec::new();
    }
    let call = match editor.purpose.clone() {
        Purpose::TokenCreate { .. } => {
            let permissions: Vec<String> = PERMISSIONS
                .iter()
                .filter(|p| editor.checked(p))
                .map(|p| (*p).to_owned())
                .collect();
            if permissions.is_empty() {
                fail(&mut editor, "publish", "editor.error.permissions", m);
                return Vec::new();
            }
            let expires = match parse_expiry(editor.text("expires"), m.wall_ms) {
                Ok(expires) => expires,
                Err(BadExpiry) => {
                    fail(&mut editor, "expires", "editor.error.expiry", m);
                    return Vec::new();
                }
            };
            let prefixes: Vec<String> = editor
                .text("prefixes")
                .split([',', ' '])
                .filter(|p| !p.is_empty())
                .map(str::to_owned)
                .collect();
            let mut body = vec![
                format!("\"name\":{}", json::quote(&name)),
                format!("\"permissions\":{}", quoted_list(&permissions)),
            ];
            if !prefixes.is_empty() {
                body.push(format!("\"topicPrefixes\":{}", quoted_list(&prefixes)));
            }
            if let Some(at) = expires {
                body.push(format!("\"expiresAt\":{}", json::quote(&at)));
            }
            ApiCall::TokenCreate(format!("{{{}}}", body.join(",")))
        }
        Purpose::TokenEdit {
            id,
            name: was_name,
            expires: was_expires,
        } => {
            let mut body = Vec::new();
            if name != was_name {
                body.push(format!("\"name\":{}", json::quote(&name)));
            }
            if editor.text("expires") != was_expires.trim() {
                match parse_expiry(editor.text("expires"), m.wall_ms) {
                    Ok(Some(at)) => body.push(format!("\"expiresAt\":{}", json::quote(&at))),
                    Ok(None) => body.push("\"expiresAt\":null".to_owned()),
                    Err(BadExpiry) => {
                        fail(&mut editor, "expires", "editor.error.expiry", m);
                        return Vec::new();
                    }
                }
            }
            if body.is_empty() {
                editor.error = Some(tx.t("editor.error.nothing"));
                m.modal = Some(Modal::Editor(editor));
                return Vec::new();
            }
            ApiCall::TokenUpdate(id, format!("{{{}}}", body.join(",")))
        }
        _ => return Vec::new(),
    };
    editor.busy = true;
    editor.error = None;
    m.modal = Some(Modal::Editor(editor));
    run(m, call)
}

/// The keys of the form of a token.
pub fn editor_key(m: &mut Model, mut editor: Editor, outcome: Outcome) -> Vec<Effect> {
    match outcome {
        Outcome::Cancel => {
            m.modal = None;
            Vec::new()
        }
        Outcome::Submit => submit(m, editor),
        Outcome::Test | Outcome::None => {
            editor.note = None;
            m.modal = Some(Modal::Editor(editor));
            Vec::new()
        }
    }
}

/// The server answered the form: the secret of a new token, or the change of a token.
pub fn saved(m: &mut Model, call: &ApiCall, text: &str) -> Vec<Effect> {
    let tx = m.tx();
    let editor = match m.modal.take() {
        Some(Modal::Editor(editor)) => Some(editor),
        other => {
            m.modal = other;
            None
        }
    };
    m.tokens.list.refresh_now();
    match call {
        ApiCall::TokenCreate(_) => match crate::tui::api::parse_token_created(text) {
            Ok((token, secret)) => {
                let replaces = match editor.map(|e| e.purpose) {
                    Some(Purpose::TokenCreate { clone_of }) => clone_of,
                    _ => None,
                };
                m.tokens.selected = Some(token.id.clone());
                m.modal = Some(Modal::Secret(Secret {
                    id: token.id,
                    name: token.name,
                    token: secret,
                    shown_at: m.now,
                    copied: false,
                    saved: false,
                    warned: false,
                    replaces,
                    revoke_old: false,
                }));
            }
            Err(e) => {
                m.notify(
                    Severity::Warning,
                    tx.tw("notice.bad-reply", &[("error", &e)]),
                );
            }
        },
        _ => {
            let text = tx.t("notice.token-updated");
            m.notify(Severity::Ok, text);
        }
    }
    Vec::new()
}

/// The server refused the form: the reason goes in the form, which stays open (TUI-TOK-02).
pub fn refused(m: &mut Model, fail: &Fail) -> bool {
    if !matches!(m.modal, Some(Modal::Editor(_))) {
        return false;
    }
    let text = if fail.is_denied() {
        crate::tui::view::widgets::denied_text(m, fail)
    } else {
        format!("{}: {}", fail.code, fail.message)
    };
    if let Some(Modal::Editor(editor)) = &mut m.modal {
        editor.busy = false;
        editor.error = Some(text);
    }
    true
}

/// A key in the popup of the secret (TUI-TOK-03).
pub fn secret_key(
    m: &mut Model,
    mut secret: Secret,
    code: ratatui::crossterm::event::KeyCode,
) -> Vec<Effect> {
    use ratatui::crossterm::event::KeyCode;
    let tx = m.tx();
    match code {
        KeyCode::Char('c') => {
            secret.copied = true;
            let text = tx.t("notice.secret-copied");
            let effect = Effect::Copy(secret.token.clone());
            m.modal = Some(Modal::Secret(secret));
            m.notify(Severity::Ok, text);
            vec![effect]
        }
        KeyCode::Char('s') => {
            let effect = Effect::WriteSecret {
                path: secret_file(&secret.id),
                contents: format!("{}\n", secret.token),
            };
            m.modal = Some(Modal::Secret(secret));
            vec![effect]
        }
        KeyCode::Char('r') if secret.replaces.is_some() => {
            secret.revoke_old = !secret.revoke_old;
            m.modal = Some(Modal::Secret(secret));
            Vec::new()
        }
        KeyCode::Esc | KeyCode::Enter => {
            if !(secret.copied || secret.saved || secret.warned) {
                secret.warned = true;
                m.modal = Some(Modal::Secret(secret));
                return Vec::new();
            }
            m.modal = None;
            match (&secret.replaces, secret.revoke_old) {
                (Some((old, _)), true) => run(m, ApiCall::TokenRevoke(old.clone())),
                _ => Vec::new(),
            }
        }
        _ => {
            m.modal = Some(Modal::Secret(secret));
            Vec::new()
        }
    }
}

/// The file `s` writes the secret to: in the folder the interface was opened from.
pub fn secret_file(id: &str) -> String {
    let safe: String = id
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
        .collect();
    format!("taba-token-{safe}.txt")
}

/// The secret was written to a file: it counts as kept.
pub fn secret_written(m: &mut Model, path: &str) {
    if let Some(Modal::Secret(secret)) = &mut m.modal {
        if secret_file(&secret.id) == path {
            secret.saved = true;
        }
    }
}

/// A token was revoked.
pub fn revoked(m: &mut Model) {
    let text = m.tx().t("notice.token-revoked");
    m.notify(Severity::Ok, text);
    m.tokens.list.refresh_now();
}

#[cfg(test)]
mod tests;