easysql 0.1.3

getting to the sql prompt, quick and easy - saved connections, their passwords and their tunnels in one CLI + TUI
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
//! The wizards: a list of fields, what kind each one is, how the prompt steps
//! through them, and how it draws.

use crate::tunnels::Tunnel;
use ratatui::prelude::*;
use ratatui::widgets::{Clear, Paragraph, Wrap};

use super::*;

/// One editable line in a wizard. `default` is shown in brackets and used when
/// the field is left blank on submit (the semantics differ per action).
pub(crate) struct Field {
    pub(crate) label: String,
    pub(crate) default: String,
    pub(crate) value: String,
    pub(crate) kind: Kind,
    /// Which option a `Choice` field has selected. Unused by the other kinds.
    pub(crate) choice: usize,
}

pub(crate) enum Kind {
    Text,
    /// A fixed set of answers cycled in place with `h`/`l` or the arrows. Nothing
    /// is typed here, which is what frees up plain `h`/`l` inside a wizard.
    Choice(Vec<String>),
    /// A typed value that is never painted back: the one field in easysql that
    /// carries a password. It goes straight into the file the client reads and
    /// is never echoed, never previewed and never put in an argv.
    Secret,
}

impl Field {
    pub(super) fn new(label: &str, default: &str) -> Self {
        Self {
            label: label.into(),
            default: default.into(),
            value: String::new(),
            kind: Kind::Text,
            choice: 0,
        }
    }
    /// A field that starts pre-filled with `value` - for the edit wizard.
    pub(super) fn filled(label: &str, value: &str) -> Self {
        Self {
            value: value.into(),
            ..Self::new(label, "")
        }
    }
    pub(super) fn secret(label: &str) -> Self {
        Self {
            kind: Kind::Secret,
            ..Self::new(label, "")
        }
    }
    /// A cycled answer, starting on the option at `at`.
    pub(super) fn choice(label: &str, options: &[&str], at: usize) -> Self {
        let options: Vec<String> = options.iter().map(|o| o.to_string()).collect();
        Self {
            choice: at.min(options.len().saturating_sub(1)),
            kind: Kind::Choice(options),
            ..Self::new(label, "")
        }
    }

    /// What the wizard paints for this field's value.
    pub(super) fn display(&self) -> String {
        match &self.kind {
            Kind::Text => self.value.clone(),
            Kind::Choice(options) => format!("{}", options[self.choice]),
            Kind::Secret => "".repeat(self.value.chars().count()),
        }
    }

    pub(super) fn is_choice(&self) -> bool {
        matches!(self.kind, Kind::Choice(_))
    }
}

/// What a wizard does once submitted. Cloned out before we move the prompt, so
/// each variant owns whatever it needs.
#[derive(Clone)]
pub(crate) enum Action {
    AddConn {
        engine: Engine,
    },
    EditConn {
        engine: Engine,
        original: String,
    },
    /// Write a password into the file that engine's client reads.
    SetPassword {
        engine: Engine,
        /// The connection whose group takes it (MySQL); unused for Postgres,
        /// where a `.pgpass` line stands on its own four fields.
        name: String,
    },
    /// Write a saved query. `original` is set when renaming an existing one.
    Snippet {
        original: Option<String>,
    },
    /// Move a `.pgpass` entry to different match fields, keeping its secret.
    EditPassword {
        idx: usize,
    },
    /// Open an `ssh -L` to a database that is not routable from here. Carries
    /// the connection it was opened for, because once the forward is up that
    /// connection is one field away from working and should not have to fail a
    /// second time to be told so.
    Forward {
        key: String,
    },
    /// Change one typed setting; cycled ones never open a wizard.
    EditSetting {
        key: String,
        label: String,
    },
}

/// A modal wizard: a titled stack of fields plus the action to run on submit.
pub(crate) struct Prompt {
    pub(crate) title: String,
    pub(crate) fields: Vec<Field>,
    pub(crate) idx: usize,
    pub(crate) action: Action,
}

/// What `sslmode` can be, in libpq's own words, with "leave it alone" first so
/// a connection that never mentioned SSL does not silently gain an opinion.
/// This is exactly the kind of value nobody remembers the spelling of, which is
/// why it is picked rather than typed.
pub(super) const SSLMODES: [&str; 6] = [
    "(unset)",
    "prefer",
    "require",
    "verify-ca",
    "verify-full",
    "disable",
];

/// What SQL Server's certificate answer can be. Validating is first because it
/// is `sqlcmd`'s own default under ODBC driver 18, and silently waiving it would
/// be easysql weakening somebody's connection for them.
pub(super) const TRUST_CERT: [&str; 2] = [
    "validate the certificate",
    "trust it (-C, for a self-signed server)",
];

/// The fields that describe a connection, which differ by engine because a
/// SQLite database is a file with no host, port, user or transport to secure.
fn conn_fields(engine: Engine, from: Option<&Conn>) -> Vec<Field> {
    let get = |pick: fn(&Conn) -> &String| from.map(pick).cloned().unwrap_or_default();
    if engine == Engine::Sqlite {
        return vec![
            Field::filled("Name (what you type after esql)", &get(|c| &c.name)),
            Field::filled("Database file", &get(|c| &c.database)),
        ];
    }
    let port = Field {
        default: engine.default_port().to_string(),
        ..Field::filled("Port", &get(|c| &c.port))
    };
    let mut fields = vec![
        Field::filled("Name (what you type after esql)", &get(|c| &c.name)),
        Field::filled("Host (IP or DNS name)", &get(|c| &c.host)),
        port,
        Field::filled("Database", &get(|c| &c.database)),
        Field::filled("User", &get(|c| &c.user)),
    ];
    // Start on whatever the block already says, so an edit that does not touch
    // this field cannot change how the connection is encrypted.
    let extra = |key: &str| {
        from.and_then(|c| c.extra.iter().find(|(k, _)| k.eq_ignore_ascii_case(key)))
            .map(|(_, v)| v.as_str())
            .unwrap_or("")
    };
    match engine {
        Engine::Pg => {
            let at = SSLMODES
                .iter()
                .position(|m| *m == extra("sslmode"))
                .unwrap_or(0);
            fields.push(Field::choice("Encryption (sslmode)", &SSLMODES, at));
        }
        Engine::MsSql => {
            let at = usize::from(extra("trust_cert") == "yes");
            fields.push(Field::choice("Certificate", &TRUST_CERT, at));
        }
        _ => {}
    }
    fields
}

impl Prompt {
    pub(super) fn cur_mut(&mut self) -> &mut Field {
        &mut self.fields[self.idx]
    }

    pub(super) fn add_conn(engine: Engine) -> Self {
        Self {
            title: format!(
                "Add a {} connection to {}",
                engine.label(),
                crate::ini::collapse_tilde(&engine.store().to_string_lossy())
            ),
            idx: 0,
            action: Action::AddConn { engine },
            fields: conn_fields(engine, None),
        }
    }

    pub(super) fn edit_conn(c: &Conn) -> Self {
        Self {
            title: format!("edit {} connection '{}'", c.engine.label(), c.name),
            idx: 0,
            action: Action::EditConn {
                engine: c.engine,
                original: c.name.clone(),
            },
            fields: conn_fields(c.engine, Some(c)),
        }
    }

    /// The password wizard. Postgres gets the four fields a `.pgpass` line
    /// matches on, pre-filled from the connection, because one line can be made
    /// to cover a whole cluster by widening a field to `*`. MySQL keeps its
    /// password inside the connection's own group, so there is nothing to match
    /// and nothing to ask.
    pub(super) fn password(c: &Conn) -> Self {
        let fields = match c.engine {
            Engine::Pg => vec![
                Field::filled(
                    "Host (* matches any)",
                    if c.host.is_empty() {
                        "localhost"
                    } else {
                        &c.host
                    },
                ),
                Field::filled("Port (* matches any)", &c.port_or_default()),
                // `*` even when the connection names a database, because a
                // Postgres password belongs to the *role* and roles are
                // cluster-wide: the same secret unlocks every database on that
                // server, and narrowing it here only means `\c elsewhere`
                // prompts. It stays editable, because behind a pooler
                // (pgbouncer, an RDS proxy) the database name really does
                // select a different backend with different credentials.
                Field::filled("Database (* = every database on this server)", "*"),
                Field::filled("User (* matches any)", &c.user),
                Field::secret("Password (never shown, never in an argv)"),
            ],
            _ => vec![Field::secret("Password (never shown, never in an argv)")],
        };
        Self {
            title: format!("save the password for '{}'", c.name),
            idx: 0,
            action: Action::SetPassword {
                engine: c.engine,
                name: c.name.clone(),
            },
            fields,
        }
    }

    /// The tunnel wizard, opened against a connection so every field already
    /// knows its answer: the only thing you normally type is nothing at all.
    pub(super) fn forward(c: &Conn, s: &Settings) -> Self {
        // The setting wins when it is set, because somebody who named a bastion
        // meant it; otherwise fall back to the host this connection, or the rest
        // of the machine, already tunnels through.
        let via = match s.tunnel_host.is_empty() {
            true => crate::vias::default_host(&c.key()).unwrap_or_default(),
            false => s.tunnel_host.clone(),
        };
        let db_host = forward_target(&via, &c.host);
        Self {
            title: format!("reach {} through an ssh host (ssh -L)", c.name),
            idx: 0,
            action: Action::Forward { key: c.key() },
            fields: vec![
                Field::filled("Tunnel through (ssh host, Ctrl-o to pick)", &via),
                Field::filled("Database host, as that machine sees it", &db_host),
                Field::filled("Database port", &c.port_or_default()),
                Field::new("Local port (where you'll reach it)", "= database port"),
            ],
        }
    }
}

/// What the far end of a `-L` should say, which is *not* always the address you
/// use from here. The host in the spec is resolved by the ssh host, so when the
/// database sits on that very machine the answer is its loopback: a server bound
/// to `127.0.0.1`, the usual reason a connection was refused in the first place,
/// will not answer on its own LAN address even from itself. Anywhere else the
/// address stays as it was, because then the ssh host really is a hop.
pub(super) fn forward_target(via: &str, db_host: &str) -> String {
    if db_host.is_empty() {
        return "localhost".to_string();
    }
    if crate::sshhosts::is_same_machine(via, db_host) {
        return "127.0.0.1".to_string();
    }
    db_host.to_string()
}

impl Prompt {
    /// Change which connection a saved password answers for, without ever
    /// asking for the password again: easysql cannot show you the one on file,
    /// but it can carry it across to the corrected line.
    pub(super) fn edit_password(cred: &crate::creds::Cred, idx: usize) -> Self {
        Self {
            title: format!("which connection is {}'s password for?", cred.user),
            idx: 0,
            action: Action::EditPassword { idx },
            fields: vec![
                Field::filled("Host (* matches any)", &cred.host),
                Field::filled("Port (* matches any)", &cred.port),
                Field::filled(
                    "Database (* = every database on this server)",
                    &cred.database,
                ),
                Field::filled("User (* matches any)", &cred.user),
            ],
        }
    }

    /// Name a saved query and give it its SQL. One line here on purpose: this
    /// is for writing a short one quickly, and anything longer is what `o` and
    /// a real editor are for.
    pub(super) fn snippet(from: Option<&crate::snippets::Snippet>) -> Self {
        Self {
            title: match from {
                Some(s) => format!("Edit the snippet '{}'", s.name),
                None => "New saved query".to_string(),
            },
            idx: 0,
            action: Action::Snippet {
                original: from.map(|s| s.name.clone()),
            },
            fields: vec![
                Field::filled(
                    "Name (what you type after the connection, as :name)",
                    from.map(|s| s.name.as_str()).unwrap_or(""),
                ),
                Field::filled(
                    "SQL (o opens it in $EDITOR afterwards, for anything longer)",
                    &from.map(|s| s.summary()).unwrap_or_default(),
                ),
            ],
        }
    }

    /// A one-field wizard for a typed setting, pre-filled with what it is now.
    pub(super) fn edit_setting(row: &settings::Row) -> Self {
        let mut field = Field::filled(row.help, &row.value);
        field.default = row.default.clone();
        Self {
            title: format!("setting: {}", row.label),
            idx: 0,
            action: Action::EditSetting {
                key: row.key.to_string(),
                label: row.label.to_string(),
            },
            fields: vec![field],
        }
    }

    /// The next field in `dir` (+1/-1), wrapping.
    pub(super) fn step(&self, dir: isize) -> usize {
        let len = self.fields.len() as isize;
        (self.idx as isize + dir).rem_euclid(len) as usize
    }

    /// True on the last field, so Enter submits rather than moving on.
    pub(super) fn on_last_field(&self) -> bool {
        self.idx + 1 == self.fields.len()
    }

    /// The exact command this wizard will run, or the exact line it will write,
    /// rebuilt from the current field values so it updates live as you type.
    /// Resolution mirrors `submit_prompt`, or the preview lies.
    /// What that command will actually open, spelled out from the fields as
    /// they stand. `psql "service=raspi"` is the honest argv - libpq reads the
    /// host, port, database and user out of the service file itself, and passing
    /// them again would override the file rather than describe it - but an argv
    /// that never changes while you edit looks broken. This is the line that
    /// moves, so the wizard shows both what runs and what it means.
    pub(super) fn resolves_to(&self) -> Option<(String, String)> {
        let v = |i: usize| self.fields[i].value.trim();
        let (Action::AddConn { engine } | Action::EditConn { engine, .. }) = &self.action else {
            return None;
        };
        let (host, port, db, user) = match engine {
            Engine::Sqlite => return None,
            _ => (v(1), v(2), v(3), v(4)),
        };
        if host.is_empty() {
            return None;
        }
        let port = if port.is_empty() {
            engine.default_port()
        } else {
            port
        };
        let mut out = String::new();
        if !user.is_empty() {
            out.push_str(user);
            out.push('@');
        }
        out.push_str(host);
        out.push(':');
        out.push_str(port);
        if !db.is_empty() {
            out.push('/');
            out.push_str(db);
        }
        Some((out, port.to_string()))
    }

    pub(super) fn command_preview(&self) -> Option<String> {
        let v = |i: usize| self.fields[i].value.trim();
        match &self.action {
            // What you will type afterwards is the point of saving it at all,
            // so the preview is the connect command, not the file we write.
            Action::AddConn { engine } | Action::EditConn { engine, .. } => {
                let name = v(0);
                if name.is_empty() {
                    return None;
                }
                Some(match engine {
                    Engine::Pg => format!("esql {name}   →   psql \"service={name}\""),
                    Engine::MySql => {
                        format!("esql {name}   →   mysql --defaults-group-suffix={name}")
                    }
                    Engine::Sqlite => format!("esql {name}   →   sqlite3 {}", v(1)),
                    // SQL Server has no named block to point at, so the preview
                    // is the flags themselves.
                    Engine::MsSql => {
                        let port = if v(2).is_empty() { "1433" } else { v(2) };
                        let host = if v(1).is_empty() { "localhost" } else { v(1) };
                        format!(
                            "esql {name}   →   sqlcmd -S {host},{port} -d {} -U {}",
                            v(3),
                            v(4)
                        )
                    }
                })
            }
            // The shape of the line, never its secret: the point is to teach the
            // file's format so you can read and edit it yourself afterwards.
            Action::Snippet { .. } => {
                let name = v(0);
                (!name.is_empty()).then(|| format!("esql <connection> :{name}"))
            }
            Action::EditPassword { .. } => Some(format!(
                "~/.pgpass   {}:{}:{}:{}:•••• (kept)",
                v(0),
                v(1),
                v(2),
                v(3)
            )),
            Action::SetPassword { engine, name } => Some(match engine {
                Engine::Pg => format!("~/.pgpass   {}:{}:{}:{}:••••", v(0), v(1), v(2), v(3)),
                _ => format!("~/.my.cnf   [client{name}]  password=••••"),
            }),
            Action::Forward { .. } => {
                let via = v(0);
                let db_host = v(1);
                let db_port = v(2);
                let local = if v(3).is_empty() { db_port } else { v(3) };
                if via.is_empty() {
                    return None;
                }
                Some(format!("ssh -N -L {local}:{db_host}:{db_port} {via}"))
            }
            Action::EditSetting { .. } => None,
        }
    }
}

pub(super) fn render_prompt(f: &mut Frame, area: Rect, p: &Prompt, tunnels: &[Tunnel]) {
    // No leading blank: the box's own top padding is that row.
    let mut lines: Vec<Line> = Vec::new();
    // Plain text of every line, kept alongside so the box can be sized against
    // what the lines wrap to rather than how many there are.
    let mut texts: Vec<String> = Vec::new();
    for (i, field) in p.fields.iter().enumerate() {
        let active = i == p.idx;
        let head = if field.default.is_empty() {
            format!("{}: ", field.label)
        } else {
            format!("{} [{}]: ", field.label, field.default)
        };
        let label_style = if active {
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().add_modifier(Modifier::DIM)
        };
        // A choice has no text cursor; it shows its options key instead, so the
        // way to change it is on screen rather than something you must know.
        let (value_style, tail) = if field.is_choice() {
            let style = if active {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };
            (style, if active { "   h/l or ←/→" } else { "" })
        } else {
            (Style::default(), if active { "" } else { "" })
        };
        texts.push(format!(
            "{}{}{}{}",
            if active { "" } else { "  " },
            head,
            field.display(),
            tail
        ));
        lines.push(Line::from(vec![
            Span::raw(if active { "" } else { "  " }),
            Span::styled(head, label_style),
            Span::styled(field.display(), value_style),
            Span::styled(
                tail.to_string(),
                Style::default().add_modifier(Modifier::DIM),
            ),
        ]));
    }
    // Live command preview: shows the exact command being built as you type, so
    // the wizard teaches the underlying tool instead of hiding it.
    if let Some(cmd) = p.command_preview() {
        lines.push(Line::raw(""));
        texts.push(String::new());
        texts.push(format!("  runs  {cmd}"));
        lines.push(Line::from(vec![
            Span::styled("  runs  ", Style::default().add_modifier(Modifier::DIM)),
            Span::styled(cmd, Style::default().fg(Color::Green)),
        ]));
    }
    // What that command opens, which is the half that moves while you edit. A
    // connection pointed at a forward is only good while the forward is alive,
    // so say which one is carrying it rather than letting that be discovered the
    // next time it is not.
    if let Some((target, port)) = p.resolves_to() {
        let via = tunnels.iter().find_map(|t| {
            let (open, _, _) = t.ports()?;
            (t.kind == 'L' && open == port).then(|| t.host.clone())
        });
        let line = match via {
            Some(host) => format!("        {target}   · through the tunnel to {host}"),
            None => format!("        {target}"),
        };
        texts.push(line.clone());
        lines.push(Line::from(Span::styled(
            line,
            Style::default().add_modifier(Modifier::DIM),
        )));
    }
    let hint = "Enter next/submit · Ctrl-j/k · Ctrl-↑↓ · Tab move field · Esc cancel";
    lines.push(Line::raw(""));
    lines.push(box_hint(hint));
    texts.push(String::new());
    texts.push(hint.to_string());

    // Size to the *wrapped* content: a preview can be far wider than the box,
    // and counting lines instead of rows pushes the keys out through the
    // bottom border.
    let width = box_width(area.width);
    let rows: usize = texts
        .iter()
        .map(|t| wrapped_line_count(t, box_inner_width(width)))
        .sum();
    let rect = box_area(area, width, box_height(rows as u16, area.height));
    f.render_widget(Clear, rect);

    let para = Paragraph::new(lines)
        .block(super::widgets::box_block(Color::Cyan, &p.title))
        .wrap(Wrap { trim: false });
    f.render_widget(para, rect);
}