service_install/tui/
removal.rs

1use dialoguer::Confirm;
2
3use crate::install::{RemoveError, RemoveSteps};
4use crate::Tense;
5
6use dialoguer;
7
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    #[error("The removal was canceled by the user")]
11    Canceled,
12    #[error("User aborted removal after one or more errors happened, errors: {0:?}")]
13    AbortedAfterError(Vec<RemoveError>),
14    #[error("Removal done however one or more errors happened, errors: {0:?}")]
15    CompletedWithErrors(Vec<RemoveError>),
16    #[error("Could not get input from the user")]
17    UserInputFailed(
18        #[from]
19        #[source]
20        dialoguer::Error,
21    ),
22}
23
24/// Start an interactive removal wizard using the provided [remove steps](RemoveSteps). This will ask
25/// the user to confirm each of the step. If anything goes wrong the user will
26/// be prompted if they wish to continue or abort.
27///
28/// # Errors
29/// This returns an error if the user canceled the removal, something went wrong
30/// getting user input or anything during the removal failed.
31///
32/// In that last case either [`AbortedAfterError`](Error::AbortedAfterError) or
33/// [`CompletedWithErrors`](Error::CompletedWithErrors) is returned depending on
34/// if the user aborted the removal of continued
35pub fn start(steps: RemoveSteps) -> Result<(), Error> {
36    let mut errors = Vec::new();
37    for mut step in steps {
38        if !Confirm::new()
39            .with_prompt(step.describe(Tense::Questioning))
40            .interact()?
41        {
42            return Err(Error::Canceled);
43        }
44
45        if let Err(e) = step.perform() {
46            errors.push(e);
47            if !Confirm::new()
48                .with_prompt("Error happened during removal, do you want to try and continue?")
49                .interact()?
50            {
51                return Err(Error::AbortedAfterError(errors));
52            }
53        }
54    }
55
56    if !errors.is_empty() {
57        return Err(Error::CompletedWithErrors(errors));
58    }
59
60    Ok(())
61}