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
use std::{
    error::Error,
    io::{self, Write},
    path::PathBuf,
    result::Result as StdResult,
};

use seaplane::{
    error::SeaplaneError,
    rexports::{container_image_ref::ImageReferenceError, seaplane_oid::error::Error as OidError},
};

use crate::{
    log::{log_level, LogLevel},
    printer::{eprinter, Color},
};

pub type Result<T> = StdResult<T, CliError>;

/// A trait for adding context to an error that will be printed along with the error. Contexts are
/// useful for adding things such as hints (i.e. try --help), or additional information such as the
/// path name on a PermissionDenied error, etc.
///
/// **NOTE:** all contexts print *without* a trailing newline. This allows a context to print to
/// the same line in different formats (colors, etc.). If a trailing newline is required, you
/// should add it manually.
pub trait Context {
    /// A simple context
    fn context<S: Into<String>>(self, msg: S) -> Self;

    /// A context that is evaluated lazily when called. This is useful if building the context is
    /// expensive or allocates
    fn with_context<F, S>(self, f: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>;

    /// A simple context that will color the output
    ///
    /// **NOTE:** The color is reset at the end of this context even if there is no trailing
    /// newline. This allows you to chain multiple contexts on the same line where only part of the
    /// context is colored.
    fn color_context<S: Into<String>>(self, color: Color, msg: S) -> Self;

    /// A context that will color the output and that is evaluated lazily when called. This is
    /// useful if building the context is expensive or allocates
    ///
    /// **NOTE:** The color is reset at the end of this context even if there is no trailing
    /// newline. This allows you to chain multiple contexts on the same line where only part of the
    /// context is colored.
    fn with_color_context<F, S>(self, f: F) -> Self
    where
        F: FnOnce() -> (Color, S),
        S: Into<String>;
}

impl<T> Context for StdResult<T, CliError> {
    fn context<S: Into<String>>(self, msg: S) -> Self {
        match self {
            Ok(t) => Ok(t),
            Err(cli_err) => Err(cli_err.context(msg)),
        }
    }
    fn color_context<S: Into<String>>(self, color: Color, msg: S) -> Self {
        match self {
            Ok(t) => Ok(t),
            Err(cli_err) => Err(cli_err.color_context(color, msg)),
        }
    }
    fn with_context<F, S>(self, f: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        match self {
            Ok(t) => Ok(t),
            Err(cli_err) => Err(cli_err.context(f())),
        }
    }

    fn with_color_context<F, S>(self, f: F) -> Self
    where
        F: FnOnce() -> (Color, S),
        S: Into<String>,
    {
        match self {
            Ok(t) => Ok(t),
            Err(cli_err) => {
                let (color, msg) = f();
                Err(cli_err.color_context(color, msg))
            }
        }
    }
}

#[derive(Debug)]
pub struct ColorString {
    msg: String,
    color: Option<Color>,
}

#[derive(Debug)]
pub struct CliError {
    kind: CliErrorKind,
    context: Vec<ColorString>,
    status: Option<i32>, // TODO: default to 1
}

impl CliError {
    pub fn bail(msg: &'static str) -> Self {
        Self { kind: CliErrorKind::UnknownWithContext(msg), ..Default::default() }
    }
}

impl Context for CliError {
    fn color_context<S: Into<String>>(mut self, color: Color, msg: S) -> Self {
        self.context
            .push(ColorString { msg: msg.into(), color: Some(color) });
        self
    }

    fn context<S: Into<String>>(mut self, msg: S) -> Self {
        self.context
            .push(ColorString { msg: msg.into(), color: None });
        self
    }

    fn with_context<F, S>(mut self, f: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        self.context
            .push(ColorString { msg: f().into(), color: None });
        self
    }

    fn with_color_context<F, S>(mut self, f: F) -> Self
    where
        F: FnOnce() -> (Color, S),
        S: Into<String>,
    {
        let (color, msg) = f();
        self.context
            .push(ColorString { msg: msg.into(), color: Some(color) });
        self
    }
}

impl Default for CliError {
    fn default() -> Self { Self { kind: CliErrorKind::Unknown, context: Vec::new(), status: None } }
}

// We have to impl Display so we can use the ? operator...but we don't actually want to use it's
// pipeline to do any kind of displaying because it doesn't support any sort of coloring. So we
// handle it manually.
impl std::fmt::Display for CliError {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        panic!("std::fmt::Display is not actually implemented for CliError by design, use CliError::print instead")
    }
}

// Just so we can us the ? operator
impl Error for CliError {}

macro_rules! impl_err {
    (@boxed, $errty:ty, $variant:ident) => {
        impl From<$errty> for CliError {
            fn from(e: $errty) -> Self {
                CliError {
                    kind: CliErrorKind::$variant(std::boxed::Box::new(e)),
                    ..Default::default()
                }
            }
        }
    };
    ($errty:ty, $variant:ident) => {
        impl From<$errty> for CliError {
            fn from(e: $errty) -> Self {
                CliError { kind: CliErrorKind::$variant(e), ..Default::default() }
            }
        }
    };
}

// These are placeholders until we get around to writing distinct errors for the cases we care
// about
impl_err!(base64::DecodeError, Base64Decode);
impl_err!(serde_json::Error, SerdeJson);
impl_err!(@boxed, toml::de::Error, TomlDe);
impl_err!(@boxed, toml::ser::Error, TomlSer);
impl_err!(seaplane::error::SeaplaneError, Seaplane);
impl_err!(ImageReferenceError, ImageReference);
impl_err!(OidError, Oid);
impl_err!(std::string::FromUtf8Error, InvalidUtf8);
impl_err!(hex::FromHexError, HexDecode);
impl_err!(std::num::ParseIntError, ParseInt);
impl_err!(strum::ParseError, StrumParse);
impl_err!(clap::Error, Clap);

impl From<io::Error> for CliError {
    fn from(e: io::Error) -> Self {
        match e.kind() {
            io::ErrorKind::NotFound => {
                CliError { kind: CliErrorKind::MissingPath, ..Default::default() }
            }
            io::ErrorKind::PermissionDenied => {
                CliError { kind: CliErrorKind::PermissionDenied, ..Default::default() }
            }
            _ => CliError { kind: CliErrorKind::Io(e, None), ..Default::default() },
        }
    }
}

impl From<tempfile::PersistError> for CliError {
    fn from(e: tempfile::PersistError) -> Self {
        Self {
            kind: CliErrorKind::Io(e.error, Some(e.file.path().to_path_buf())),
            ..Default::default()
        }
    }
}

impl From<tempfile::PathPersistError> for CliError {
    fn from(e: tempfile::PathPersistError) -> Self {
        Self { kind: CliErrorKind::Io(e.error, Some(e.path.to_path_buf())), ..Default::default() }
    }
}

impl From<CliErrorKind> for CliError {
    fn from(kind: CliErrorKind) -> Self { CliError { kind, ..Default::default() } }
}

#[derive(Debug)]
pub enum CliErrorKind {
    AmbiguousItem(String),
    Base64Decode(base64::DecodeError),
    Clap(clap::Error),
    CliArgNotUsed(&'static str),
    ConflictingArguments(String, String),
    DuplicateName(String),
    EndpointInvalidFlight(String),
    ExistingValue(&'static str),
    FlightsInUse(Vec<String>),
    HexDecode(hex::FromHexError),
    ImageReference(ImageReferenceError),
    InlineFlightHasSpace,
    InlineFlightInvalidName(String),
    InlineFlightMissingImage,
    InlineFlightMissingValue(String),
    InlineFlightUnknownItem(String),
    InvalidCliValue(Option<&'static str>, String),
    InvalidUtf8(std::string::FromUtf8Error),
    Io(io::Error, Option<PathBuf>),
    MissingApiKey,
    MissingPath,
    MultipleAtStdin,
    NoMatchingItem(String),
    Oid(OidError),
    OneOff(String),
    ParseInt(std::num::ParseIntError),
    PermissionDenied,
    Seaplane(SeaplaneError),
    SerdeJson(serde_json::Error),
    StrumParse(strum::ParseError),
    TomlDe(Box<toml::de::Error>),
    TomlSer(Box<toml::ser::Error>),
    Unknown,
    UnknownWithContext(&'static str),
}

impl CliErrorKind {
    fn print(&self) {
        use CliErrorKind::*;

        match self {
            OneOff(msg) => {
                cli_eprintln!("{msg}");
            }
            FlightsInUse(flights) => {
                cli_eprintln!("the following Flights are referenced by a Formation Plan and cannot be deleted");
                for f in flights {
                    cli_eprintln!(@Yellow, "\t{f}");
                }
                cli_eprint!("\n(hint: override this check and force delete with '");
                cli_eprint!(@Yellow, "--force");
                cli_eprintln!("' which will also remove the Flight from the Formation Plan)");
            }
            EndpointInvalidFlight(flight) => {
                cli_eprint!("Flight '");
                cli_eprint!(@Red, "{flight}");
                cli_eprintln!(
                    "' is referenced in an endpoint but does not exist in the local Plans"
                );
            }
            ConflictingArguments(a, b) => {
                cli_eprint!("cannot use '");
                cli_eprint!(@Yellow, "{a}");
                cli_eprint!("' with '");
                cli_eprint!(@Yellow, "{b}");
                cli_eprintln!(
                    "'\n(hint: one or both arguments may have been implied from other flags)"
                );
            }
            Base64Decode(e) => {
                cli_eprintln!("base64 decode: {e}");
            }
            DuplicateName(name) => {
                cli_eprint!("an item with the name '");
                cli_eprint!(@Yellow, "{name}");
                cli_eprintln!("' already exists");
            }
            NoMatchingItem(item) => {
                cli_eprint!("the NAME or ID '");
                cli_eprint!(@Green, "{item}");
                cli_eprintln!("' didn't match anything");
            }
            AmbiguousItem(item) => {
                cli_eprint!("the NAME or ID '");
                cli_eprint!(@Yellow, "{item}");
                cli_eprintln!("' is ambiguous and matches more than one item");
            }
            MissingPath => {
                cli_eprintln!("missing file or directory");
            }
            PermissionDenied => {
                cli_eprintln!("permission denied when accessing file or directory");
            }
            HexDecode(e) => {
                cli_eprintln!("hex decode: {e}")
            }
            ImageReference(e) => {
                cli_eprintln!("seaplane: {e}")
            }
            InvalidUtf8(e) => {
                cli_eprintln!("invalid UTF-8: {e}")
            }
            StrumParse(e) => {
                cli_eprintln!("string parse error: {e}")
            }
            Io(e, Some(path)) => {
                cli_eprintln!("io: {e}");
                cli_eprint!("\tpath: ");
                cli_eprintln!(@Yellow, "{path:?}");
            }
            Io(e, None) => {
                cli_eprintln!("io: {e}");
            }
            SerdeJson(e) => {
                cli_eprintln!("json: {e}")
            }
            TomlDe(e) => {
                cli_eprintln!("toml: {e}")
            }
            TomlSer(e) => {
                cli_eprintln!("toml: {e}")
            }
            ParseInt(e) => {
                cli_eprintln!("parse integer: {e}")
            }
            UnknownWithContext(e) => {
                cli_eprintln!("unknown: {e}")
            }
            InvalidCliValue(a, v) => {
                cli_eprint!("the CLI value '");
                if let Some(val) = a {
                    cli_eprint!("--{val}=");
                    cli_eprint!(@Red, "{v}");
                } else {
                    cli_eprint!(@Red, "{v}");
                }
                cli_eprintln!("' isn't valid");
            }
            CliArgNotUsed(a) => {
                cli_eprint!("the CLI argument '");
                cli_eprint!("{a}");
                cli_eprintln!("' wasn't used but is required in this context");
            }
            Unknown => {
                cli_eprintln!("unknown")
            }
            MissingApiKey => {
                cli_eprintln!("no API key was found or provided")
            }
            MultipleAtStdin => {
                cli_eprint!("more than one '");
                cli_print!(@Yellow, "-");
                cli_println!("' values were provided and only one is allowed");
            }
            Seaplane(e) => match e {
                SeaplaneError::ApiResponse(ae) => {
                    cli_eprintln!("{ae}");
                }
                _ => {
                    cli_eprintln!("Seaplane API: {e}")
                }
            },
            ExistingValue(value) => {
                cli_eprintln!("{value} already exists");
            }
            InlineFlightUnknownItem(item) => {
                cli_eprintln!(
                    "{item} is not a valid INLINE-FLIGHT-SPEC item (valid keys are: name, image)"
                );
            }
            InlineFlightInvalidName(name) => {
                cli_eprintln!("'{name}' is not a valid Flight name");
            }
            InlineFlightHasSpace => {
                cli_eprintln!("INLINE-FLIGHT-SPEC contains a space ' ' which isn't allowed.");
            }
            InlineFlightMissingImage => {
                cli_eprintln!(
                    "INLINE-FLIGHT-SPEC missing image=... key and value which is required"
                );
            }
            InlineFlightMissingValue(key) => {
                cli_eprintln!("INLINE-FLIGHT-SPEC missing a value for the key {key}");
            }
            Clap(e) => {
                cli_eprintln!("{e}")
            }
            Oid(e) => {
                cli_eprintln!("{e}")
            }
        }
    }

    pub fn into_err(self) -> CliError { CliError { kind: self, ..Default::default() } }

    #[cfg(test)]
    pub fn is_parse_int(&self) -> bool { matches!(self, Self::ParseInt(_)) }
    #[cfg(test)]
    pub fn is_strum_parse(&self) -> bool { matches!(self, Self::StrumParse(_)) }
}

// Impl PartialEq manually so we can just match on kind, and not the associated data
impl PartialEq<Self> for CliErrorKind {
    fn eq(&self, rhs: &Self) -> bool {
        use CliErrorKind::*;

        match self {
            OneOff(_) => matches!(rhs, OneOff(_)),
            EndpointInvalidFlight(_) => matches!(rhs, EndpointInvalidFlight(_)),
            AmbiguousItem(_) => matches!(rhs, AmbiguousItem(_)),
            Io(_, _) => matches!(rhs, Io(_, _)),
            DuplicateName(_) => matches!(rhs, DuplicateName(_)),
            MissingApiKey => matches!(rhs, MissingApiKey),
            MissingPath => matches!(rhs, MissingPath),
            NoMatchingItem(_) => matches!(rhs, NoMatchingItem(_)),
            PermissionDenied => matches!(rhs, PermissionDenied),
            MultipleAtStdin => matches!(rhs, MultipleAtStdin),
            Seaplane(_) => matches!(rhs, Seaplane(_)),
            SerdeJson(_) => matches!(rhs, SerdeJson(_)),
            TomlSer(_) => matches!(rhs, TomlSer(_)),
            TomlDe(_) => matches!(rhs, TomlDe(_)),
            Unknown => matches!(rhs, Unknown),
            UnknownWithContext(_) => matches!(rhs, UnknownWithContext(_)),
            ExistingValue(_) => matches!(rhs, ExistingValue(_)),
            ImageReference(_) => matches!(rhs, ImageReference(_)),
            CliArgNotUsed(_) => matches!(rhs, CliArgNotUsed(_)),
            InvalidCliValue(_, _) => matches!(rhs, InvalidCliValue(_, _)),
            StrumParse(_) => matches!(rhs, StrumParse(_)),
            Base64Decode(_) => matches!(rhs, Base64Decode(_)),
            InvalidUtf8(_) => matches!(rhs, InvalidUtf8(_)),
            HexDecode(_) => matches!(rhs, HexDecode(_)),
            ConflictingArguments(_, _) => matches!(rhs, ConflictingArguments(_, _)),
            InlineFlightUnknownItem(_) => matches!(rhs, InlineFlightUnknownItem(_)),
            InlineFlightInvalidName(_) => matches!(rhs, InlineFlightInvalidName(_)),
            InlineFlightHasSpace => matches!(rhs, InlineFlightHasSpace),
            InlineFlightMissingImage => matches!(rhs, InlineFlightMissingImage),
            InlineFlightMissingValue(_) => matches!(rhs, InlineFlightMissingValue(_)),
            ParseInt(_) => matches!(rhs, ParseInt(_)),
            FlightsInUse(_) => matches!(rhs, FlightsInUse(_)),
            Clap(_) => matches!(rhs, Clap(_)),
            Oid(_) => matches!(rhs, Oid(_)),
        }
    }
}

impl CliError {
    /// Essentially destructure the cli_*! macros which actually also reduces the branches
    pub fn print(&self) {
        if log_level() <= &LogLevel::Error {
            // Scope for acquiring Mutex on global printer
            {
                let mut ptr = eprinter();
                ptr.set_color(Color::Red);
                let _ = write!(ptr, "error: ");
                ptr.reset();
            }

            // This function will try to reacquire the mutex
            self.kind.print();

            // Reacquire mutex lock
            let mut ptr = eprinter();
            for ColorString { color, msg } in &self.context {
                if let Some(c) = color {
                    ptr.set_color(*c);
                }
                let _ = write!(ptr, "{msg}");
                ptr.reset();
            }
        }
    }

    pub fn exit(&self) -> ! {
        self.print();
        // TODO: solidify what should happen if an error with self.fatal = false is called here...
        std::process::exit(self.status.unwrap_or(1))
    }

    pub fn kind(&self) -> &CliErrorKind { &self.kind }
}