seaplane_cli/cli/
errors.rs

1// Common errors and their messages/hints
2
3use crate::{
4    error::{CliError, CliErrorKind, Context, Result},
5    printer::Color,
6};
7
8pub fn wrap_cli_context(err: CliError, has_exact: bool, has_all: bool) -> Result<()> {
9    use CliErrorKind::*;
10
11    let e = match err.kind() {
12        NoMatchingItem(_) => {
13            if has_exact {
14                err.context("(hint: remove '")
15                    .color_context(Color::Yellow, "--exact")
16                    .context("' to allow partial matches)\n")
17                    .context("(hint: try '")
18                    .color_context(Color::Yellow, "seaplane formation fetch-remote")
19                    .context("' to update remote Formation definitions)\n")
20            } else if has_all {
21                err.context("(hint: try adding '")
22                    .color_context(Color::Yellow, "--all")
23                    .context("' to allow partial matches)\n")
24                    .context("(hint: try '")
25                    .color_context(Color::Yellow, "seaplane formation fetch-remote")
26                    .context("' to sync local definitions)\n")
27            } else {
28                err
29            }
30        }
31        AmbiguousItem(_) => {
32            if has_all {
33                err.context("(hint: add '")
34                    .color_context(Color::Yellow, "--all")
35                    .context("' operation on every match)\n")
36            } else {
37                err
38            }
39        }
40        _ => err,
41    };
42
43    Err(e)
44}
45
46pub fn no_matching_item(item: String, has_exact: bool, has_all: bool) -> Result<()> {
47    wrap_cli_context(CliErrorKind::NoMatchingItem(item).into_err(), has_exact, has_all)
48}
49
50pub fn ambiguous_item(item: String, has_all: bool) -> Result<()> {
51    wrap_cli_context(CliErrorKind::AmbiguousItem(item).into_err(), false, has_all)
52}