auberge 0.14.23

CLI tool for managing self-hosted infrastructure with Ansible
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
use crate::output::should_use_colors;
use dialoguer::{Confirm, Input, MultiSelect, Select, theme::ColorfulTheme, theme::SimpleTheme};
use eyre::Result;
use skim::prelude::*;
use std::io::{Cursor, IsTerminal, Write};

#[derive(Debug, PartialEq, Eq)]
enum ThemeKind {
    Colorful,
    Simple,
}

fn theme_kind() -> ThemeKind {
    if should_use_colors() {
        ThemeKind::Colorful
    } else {
        ThemeKind::Simple
    }
}

fn dialoguer_theme() -> Box<dyn dialoguer::theme::Theme> {
    match theme_kind() {
        ThemeKind::Colorful => Box::new(ColorfulTheme::default()),
        ThemeKind::Simple => Box::new(SimpleTheme),
    }
}

fn has_skim_support() -> bool {
    std::io::stdin().is_terminal() && std::io::stderr().is_terminal()
}

fn select_with_skim(items: &[String], prompt: &str) -> Option<String> {
    if items.is_empty() {
        return None;
    }

    let prompt_str = format!("{}> ", prompt);

    let options = SkimOptionsBuilder::default()
        .prompt(Some(&prompt_str))
        .height(Some("40%"))
        .multi(false)
        .reverse(true)
        .build()
        .ok()?;

    let input = items.join("\n");
    let item_reader = SkimItemReader::default();
    let items = item_reader.of_bufread(Cursor::new(input));

    let output = Skim::run_with(&options, Some(items))?;

    // Skim leaves terminal background color set after exit.
    // \x1b[0m resets all SGR attributes (fixes colored bands on subsequent lines).
    // \x1b[J clears from cursor to end of screen (removes phantom blank lines).
    if should_use_colors() {
        let mut stderr = std::io::stderr().lock();
        let _ = stderr.write_all(b"\x1b[0m\x1b[J");
        let _ = stderr.flush();
    }

    if output.is_abort {
        return None;
    }

    output
        .selected_items
        .first()
        .map(|item| item.output().to_string())
}

fn select_with_dialoguer(items: &[String], prompt: &str) -> Option<String> {
    if items.is_empty() {
        return None;
    }

    let theme = dialoguer_theme();
    let selection = Select::with_theme(theme.as_ref())
        .with_prompt(prompt)
        .items(items)
        .default(0)
        .interact_opt()
        .ok()
        .flatten()?;

    Some(items[selection].clone())
}

/// Describes what is being chosen so `select_item` can render each of the
/// three ways a pick can fail to happen as its own actionable error.
///
/// Returning one `Option` for all three let call sites collapse "nothing to
/// choose from", "cannot draw a picker" and "user dismissed the picker" into
/// one misleading message.
///
/// `noun` is pluralised as `{noun}s`; every candidate kind in this tree
/// pluralises regularly.
pub struct Choice {
    prompt: String,
    noun: String,
    argument: Option<String>,
    populate: Option<String>,
}

impl Choice {
    pub fn new(noun: &str) -> Self {
        Self {
            prompt: format!("Select {}", noun),
            noun: noun.to_string(),
            argument: None,
            populate: None,
        }
    }

    pub fn with_prompt(mut self, prompt: &str) -> Self {
        self.prompt = prompt.to_string();
        self
    }

    /// How the caller supplies the value when no picker can be drawn, e.g.
    /// `-H <host>`.
    pub fn resolved_by(mut self, argument: &str) -> Self {
        self.argument = Some(argument.to_string());
        self
    }

    /// Command that creates candidates when there are none, e.g.
    /// `auberge host add`.
    pub fn populated_by(mut self, command: &str) -> Self {
        self.populate = Some(command.to_string());
        self
    }

    fn no_candidates(&self) -> eyre::Report {
        match &self.populate {
            Some(command) => {
                eyre::eyre!("No {}s configured — run `{}`", self.noun, command)
            }
            None => eyre::eyre!("No {}s configured", self.noun),
        }
    }

    fn not_interactive(&self, candidates: usize) -> eyre::Report {
        match &self.argument {
            Some(argument) => eyre::eyre!(
                "{} {}s to choose from and stdin is not a terminal — pass {}",
                candidates,
                self.noun,
                argument
            ),
            None => eyre::eyre!(
                "{} {}s to choose from and stdin is not a terminal",
                candidates,
                self.noun
            ),
        }
    }

    fn aborted(&self) -> eyre::Report {
        eyre::eyre!("No {} selected", self.noun)
    }
}

/// Picks one of `items`, or fails with the reason no pick happened.
///
/// A lone candidate is auto-selected without a TTY: the choice is not a choice.
pub fn select_item<T, F>(items: &[T], display_fn: F, choice: Choice) -> Result<T>
where
    T: Clone,
    F: Fn(&T) -> String,
{
    if items.is_empty() {
        return Err(choice.no_candidates());
    }

    if !has_skim_support() {
        if let [only] = items {
            return Ok(only.clone());
        }
        return Err(choice.not_interactive(items.len()));
    }

    let display_items: Vec<String> = items.iter().map(&display_fn).collect();
    let selected = select_with_skim(&display_items, &choice.prompt)
        .or_else(|| select_with_dialoguer(&display_items, &choice.prompt))
        .ok_or_else(|| choice.aborted())?;

    display_items
        .iter()
        .position(|d| d == &selected)
        .map(|i| items[i].clone())
        .ok_or_else(|| choice.aborted())
}

fn select_multi_with_skim(items: &[String], prompt: &str) -> Option<Vec<String>> {
    if items.is_empty() {
        return None;
    }

    let prompt_str = format!("{}> ", prompt);

    let options = SkimOptionsBuilder::default()
        .prompt(Some(&prompt_str))
        .height(Some("40%"))
        .multi(true)
        .reverse(true)
        .build()
        .ok()?;

    let input = items.join("\n");
    let item_reader = SkimItemReader::default();
    let items = item_reader.of_bufread(Cursor::new(input));

    let output = Skim::run_with(&options, Some(items))?;

    if should_use_colors() {
        let mut stderr = std::io::stderr().lock();
        let _ = stderr.write_all(b"\x1b[0m\x1b[J");
        let _ = stderr.flush();
    }

    if output.is_abort {
        return None;
    }

    let selected: Vec<String> = output
        .selected_items
        .iter()
        .map(|item| item.output().to_string())
        .collect();

    if selected.is_empty() {
        None
    } else {
        Some(selected)
    }
}

fn select_multi_with_dialoguer(items: &[String], prompt: &str) -> Option<Vec<String>> {
    if items.is_empty() {
        return None;
    }

    let theme = dialoguer_theme();
    let selections = MultiSelect::with_theme(theme.as_ref())
        .with_prompt(prompt)
        .items(items)
        .interact_opt()
        .ok()
        .flatten()?;

    if selections.is_empty() {
        return None;
    }

    Some(selections.iter().map(|&i| items[i].clone()).collect())
}

pub fn select_multi(items: &[String], prompt: &str) -> Option<Vec<String>> {
    if items.is_empty() {
        return None;
    }

    if !has_skim_support() {
        if items.len() == 1 {
            return Some(vec![items[0].clone()]);
        }
        return None;
    }

    if let Some(result) = select_multi_with_skim(items, prompt) {
        return Some(result);
    }

    select_multi_with_dialoguer(items, prompt)
}

pub fn confirm(msg: &str, yes_flag: bool) -> bool {
    if yes_flag {
        return true;
    }

    if !std::io::stdin().is_terminal() {
        return false;
    }

    let theme = dialoguer_theme();
    Confirm::with_theme(theme.as_ref())
        .with_prompt(msg)
        .default(false)
        .interact()
        .unwrap_or(false)
}

/// Severe confirmation: the user must type `expected` exactly to proceed.
/// Use for irreversible / production-impacting actions.
///
/// Honors `yes_flag` (skip prompt, proceed) and non-TTY stdin (refuse, return
/// `Ok(false)` so callers can bail with an actionable message instead of
/// hanging on a prompt that nobody can answer).
pub fn confirm_typed(prompt_msg: &str, expected: &str, yes_flag: bool) -> Result<bool> {
    if yes_flag {
        return Ok(true);
    }

    if !std::io::stdin().is_terminal() {
        return Ok(false);
    }

    let theme = dialoguer_theme();
    let typed: String = Input::with_theme(theme.as_ref())
        .with_prompt(prompt_msg)
        .allow_empty(true)
        .interact_text()?;

    Ok(typed.trim() == expected)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::output::{TEST_LOCK, set_no_color};

    fn hosts() -> Vec<String> {
        vec!["auberge".to_string(), "hermes".to_string()]
    }

    #[test]
    fn select_item_reports_nothing_configured_for_an_empty_list() {
        // The bug behind #468: an empty candidate list used to yield the same
        // "No host selected" as a dismissed picker, so `sync music`'s bogus
        // group filter looked like the user had declined to choose.
        let empty: Vec<String> = vec![];
        let err = select_item(
            &empty,
            |s: &String| s.clone(),
            Choice::new("host").populated_by("auberge host add"),
        )
        .unwrap_err();

        assert_eq!(
            err.to_string(),
            "No hosts configured — run `auberge host add`"
        );
    }

    #[test]
    fn select_item_omits_the_populate_hint_when_none_is_given() {
        let empty: Vec<String> = vec![];
        let err = select_item(&empty, |s: &String| s.clone(), Choice::new("playbook")).unwrap_err();

        assert_eq!(err.to_string(), "No playbooks configured");
    }

    #[test]
    fn select_item_names_the_argument_when_it_cannot_prompt() {
        // `cargo test` runs without a TTY, so this is the scripted path: two
        // candidates, no picker possible, and the error must name the flag
        // that resolves it.
        let err = select_item(
            &hosts(),
            |s: &String| s.clone(),
            Choice::new("host").resolved_by("-H <host>"),
        )
        .unwrap_err();

        assert_eq!(
            err.to_string(),
            "2 hosts to choose from and stdin is not a terminal — pass -H <host>"
        );
    }

    #[test]
    fn select_item_still_reports_the_count_without_an_argument_hint() {
        let err = select_item(&hosts(), |s: &String| s.clone(), Choice::new("host")).unwrap_err();

        assert_eq!(
            err.to_string(),
            "2 hosts to choose from and stdin is not a terminal"
        );
    }

    #[test]
    fn select_item_auto_selects_a_lone_candidate_without_a_tty() {
        // Deliberate, and matches `backup verify`'s sole_configured_host: one
        // candidate is not a choice, so scripts need no flag.
        let only = vec!["auberge".to_string()];
        let selected = select_item(
            &only,
            |s: &String| s.clone(),
            Choice::new("host").resolved_by("-H <host>"),
        )
        .unwrap();

        assert_eq!(selected, "auberge");
    }

    #[test]
    fn choice_defaults_its_prompt_from_the_noun() {
        assert_eq!(Choice::new("subdomain").prompt, "Select subdomain");
        assert_eq!(
            Choice::new("subdomain").with_prompt("Pick one").prompt,
            "Pick one"
        );
    }

    #[test]
    fn confirm_short_circuits_to_true_when_yes_flag_set() {
        assert!(confirm("anything", true));
    }

    #[test]
    fn confirm_returns_false_in_non_tty_without_yes_flag() {
        // `cargo test` runs with non-TTY stdin, so the is_terminal() guard
        // takes effect.  This is the path that prevents `dns set-all` and
        // `dns delete` from hanging in CI when --yes is omitted.
        assert!(!confirm("anything", false));
    }

    #[test]
    fn confirm_typed_short_circuits_to_true_when_yes_flag_set() {
        // --yes must bypass the typed-confirmation gate so CI can run without
        // a TTY attached.  Expected value is irrelevant on this path.
        assert!(confirm_typed("type the name", "freshrss", true).unwrap());
    }

    #[test]
    fn confirm_typed_returns_false_in_non_tty_without_yes_flag() {
        // Without --yes and without a TTY, severe confirmation cannot be
        // satisfied — callers should treat this as cancellation and surface
        // an actionable error rather than dispatching the destructive op.
        assert!(!confirm_typed("type the name", "freshrss", false).unwrap());
    }

    #[test]
    fn theme_kind_is_simple_when_no_color_flag_set() {
        let _guard = TEST_LOCK.lock().unwrap();
        set_no_color(true);
        assert_eq!(theme_kind(), ThemeKind::Simple);
        set_no_color(false);
    }

    #[test]
    fn dialoguer_theme_does_not_panic_in_either_branch() {
        let _guard = TEST_LOCK.lock().unwrap();
        set_no_color(true);
        let _simple = dialoguer_theme();
        set_no_color(false);
        let _maybe_colorful = dialoguer_theme();
    }
}