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
use std::{
    fs,
    io::{self, Read, Write},
    path::{Path, PathBuf},
};

use seaplane::api::compute::v1::Flight as FlightModel;
use serde::{Deserialize, Serialize};
use tabwriter::TabWriter;

use crate::{
    context::{Ctx, FlightCtx},
    error::{CliError, CliErrorKind, Context, Result},
    fs::{FromDisk, ToDisk},
    ops::Id,
    printer::{Color, Output},
};

/// A wrapper round a Flight model
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Flight {
    pub id: Id,
    #[serde(flatten)]
    pub model: FlightModel,
}

impl Flight {
    pub fn new(model: FlightModel) -> Self { Self { id: Id::new(), model } }

    pub fn from_json(s: &str) -> Result<Flight> { serde_json::from_str(s).map_err(CliError::from) }

    pub fn starts_with(&self, s: &str) -> bool {
        self.id.to_string().starts_with(s) || self.model.name().starts_with(s)
    }

    /// Applies the non-default differences from `ctx`
    pub fn update_from(&mut self, ctx: &FlightCtx, keep_src_name: bool) -> Result<()> {
        let mut dest_builder = FlightModel::builder();

        // Name
        if keep_src_name {
            dest_builder = dest_builder.name(self.model.name());
        } else {
            dest_builder = dest_builder.name(&ctx.name_id);
        }

        if let Some(image) = ctx.image.clone() {
            dest_builder = dest_builder.image_reference(image);
        } else {
            dest_builder = dest_builder.image_reference(self.model.image().clone());
        }

        if ctx.minimum != 1 {
            dest_builder = dest_builder.minimum(ctx.minimum);
        } else {
            dest_builder = dest_builder.minimum(self.model.minimum());
        }

        if let Some(max) = ctx.maximum {
            dest_builder = dest_builder.maximum(max);
        } else if ctx.reset_maximum {
            dest_builder.clear_maximum();
        } else if let Some(max) = self.model.maximum() {
            dest_builder = dest_builder.maximum(max);
        }

        // Architecture
        for arch in ctx.architecture.iter().chain(self.model.architecture()) {
            dest_builder = dest_builder.add_architecture(*arch);
        }

        // API Permission
        #[cfg(feature = "unstable")]
        {
            let orig_api_perms = self.model.api_permission();
            let cli_api_perms = ctx.api_permission;
            match (orig_api_perms, cli_api_perms) {
                (true, false) => dest_builder = dest_builder.api_permission(false),
                (false, true) => dest_builder = dest_builder.api_permission(true),
                _ => (),
            }
        }

        self.model = dest_builder.build().expect("Failed to build Flight");
        Ok(())
    }

    /// Creates a Flight from either a JSON string in STDIN (@-) or a path pointed to by @path.
    fn from_at_str(flight: &str) -> Result<Self> {
        // First try to create for a @- (STDIN)
        if flight == "@-" {
            let mut buf = String::new();
            let stdin = io::stdin();
            let mut stdin_lock = stdin.lock();
            stdin_lock.read_to_string(&mut buf)?;

            // TODO: we need to check for and handle duplicates
            let new_flight = Flight::from_json(&buf)?;
            return Ok(new_flight);
        // next try to create if using @path
        } else if let Some(path) = flight.strip_prefix('@') {
            let new_flight = Flight::from_json(
                &fs::read_to_string(path)
                    .map_err(CliError::from)
                    .context("\n\tpath: ")
                    .with_color_context(|| (Color::Yellow, path))?,
            )?;
            return Ok(new_flight);
        }

        Err(CliErrorKind::InvalidCliValue(None, flight.into()).into_err())
    }
}

#[derive(Debug, Deserialize, Serialize, Default, Clone)]
#[serde(transparent)]
pub struct Flights {
    #[serde(skip)]
    loaded_from: Option<PathBuf>,
    inner: Vec<Flight>,
}

impl FromDisk for Flights {
    fn set_loaded_from<P: AsRef<Path>>(&mut self, p: P) {
        self.loaded_from = Some(p.as_ref().into());
    }

    fn loaded_from(&self) -> Option<&Path> { self.loaded_from.as_deref() }
}

impl ToDisk for Flights {}

impl Flights {
    /// Takes strings in the form of @- or @path and creates then adds them to the DB. Only one @-
    /// may be used or an Error is returned.
    ///
    /// Returns a list of any created IDs
    pub fn add_from_at_strs<S>(&mut self, flights: Vec<S>) -> Result<Vec<String>>
    where
        S: AsRef<str>,
    {
        if flights.iter().filter(|f| f.as_ref() == "@-").count() > 1 {
            return Err(CliErrorKind::MultipleAtStdin.into_err());
        }
        let mut ret = Vec::new();
        for flight in flights {
            let new_flight = Flight::from_at_str(flight.as_ref())?;
            ret.push(new_flight.model.name().to_owned());
            self.inner.push(new_flight);
        }

        Ok(ret)
    }

    /// Removes any Flight definitions from the matching indices and returns a Vec of all removed
    /// Flights
    pub fn remove_indices(&mut self, indices: &[usize]) -> Vec<Flight> {
        // TODO: There is probably a much more performant way to remove a bunch of times from a Vec
        // but we're talking such a small number of items this should never matter.

        indices
            .iter()
            .enumerate()
            .map(|(i, idx)| self.inner.remove(idx - i))
            .collect()
    }

    /// Returns all indices of where the flight's Name or ID (as hex) begins with the `needle`
    pub fn indices_of_left_matches(&self, needle: &str) -> Vec<usize> {
        // TODO: Are there any names that also coincide with valid hex?
        self.inner
            .iter()
            .enumerate()
            .filter(|(_idx, flight)| flight.starts_with(needle))
            .map(|(idx, _flight)| idx)
            .collect()
    }

    /// Returns all indices of where the flight's Name or ID (as hex) is an exact match of `needle`
    pub fn indices_of_matches(&self, needle: &str) -> Vec<usize> {
        // TODO: Are there any names that also coincide with valid hex?
        self.inner
            .iter()
            .enumerate()
            .filter(|(_idx, flight)| {
                flight.id.to_string() == needle || flight.model.name() == needle
            })
            .map(|(idx, _flight)| idx)
            .collect()
    }

    pub fn iter(&self) -> impl Iterator<Item = &Flight> { self.inner.iter() }

    pub fn clone_flight(&mut self, src: &str, exact: bool) -> Result<Flight> {
        let src_flight = self.remove_flight(src, exact)?;
        let model = src_flight.model.clone();

        // Re add the source flight
        self.inner.push(src_flight);

        Ok(Flight::new(model))
    }

    /// Either updates a matching local flight, or creates a new one. Returns NEW flight names and
    /// IDs
    pub fn update_or_create_flight(&mut self, model: &FlightModel) -> Vec<(String, Id)> {
        let mut found = false;
        let mut ret = Vec::new();
        for flight in self
            .inner
            .iter_mut()
            .filter(|f| f.model.name() == model.name() && f.model.image_str() == model.image_str())
        {
            found = true;
            flight.model.set_minimum(model.minimum());
            flight.model.set_maximum(model.maximum());

            for arch in model.architecture() {
                flight.model.add_architecture(*arch);
            }

            #[cfg(feature = "unstable")]
            {
                flight.model.set_api_permission(model.api_permission());
            }
        }

        if !found {
            let f = Flight::new(model.clone());
            ret.push((f.model.name().to_owned(), f.id));
            self.inner.push(f);
        }

        ret
    }

    pub fn update_flight(&mut self, src: &str, exact: bool, ctx: &FlightCtx) -> Result<()> {
        let mut src_flight = self.remove_flight(src, exact)?;
        src_flight.update_from(ctx, ctx.generated_name)?;

        // Re add the source flight
        self.inner.push(src_flight);

        Ok(())
    }

    pub fn add_flight(&mut self, flight: Flight) { self.inner.push(flight); }

    pub fn remove_flight(&mut self, src: &str, exact: bool) -> Result<Flight> {
        // Ensure only one current Flight matches what the user gave
        // TODO: We should check if these ones we remove are referenced remote or not
        let indices =
            if exact { self.indices_of_matches(src) } else { self.indices_of_left_matches(src) };
        match indices.len() {
            0 => return Err(CliErrorKind::NoMatchingItem(src.into()).into_err()),
            1 => (),
            _ => return Err(CliErrorKind::AmbiguousItem(src.into()).into_err()),
        }

        Ok(self.remove_indices(&indices).pop().unwrap())
    }

    pub fn find_name(&self, name: &str) -> Option<&Flight> {
        self.inner.iter().find(|f| f.model.name() == name)
    }

    pub fn find_name_or_partial_id(&self, needle: &str) -> Option<&Flight> {
        self.inner
            .iter()
            .find(|f| f.model.name() == needle || f.id.to_string().starts_with(needle))
    }
}

impl Output for Flights {
    fn print_json(&self, _ctx: &Ctx) -> Result<()> {
        cli_println!("{}", serde_json::to_string(self)?);

        Ok(())
    }

    fn print_table(&self, ctx: &Ctx) -> Result<()> {
        let buf = Vec::new();
        let mut tw = TabWriter::new(buf);
        // TODO: Add local/remote formation references
        writeln!(tw, "LOCAL ID\tNAME\tIMAGE\tMIN\tMAX\tARCH\tAPI PERMS")?;
        for flight in self.iter() {
            let arch = flight
                .model
                .architecture()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(",");

            #[cfg_attr(not(feature = "unstable"), allow(unused_mut))]
            let mut api_perms = false;
            // Due to our use of cfg and Rust's "unused-assignment" lint
            let _ = api_perms;
            #[cfg(feature = "unstable")]
            {
                api_perms = flight.model.api_permission();
            }
            writeln!(
                tw,
                "{}\t{}\t{}\t{}\t{}\t{}\t{}",
                &flight.id.to_string()[..8], // TODO: make sure length is not ambiguous
                flight.model.name(),
                flight.model.image_str().trim_start_matches(&ctx.registry),
                flight.model.minimum(),
                flight
                    .model
                    .maximum()
                    .map(|n| format!("{n}"))
                    .unwrap_or_else(|| "INF".into()),
                if arch.is_empty() { "auto" } else { &*arch },
                api_perms,
            )?;
        }
        tw.flush()?;

        cli_println!(
            "{}",
            String::from_utf8_lossy(
                &tw.into_inner()
                    .map_err(|_| CliError::bail("IO flush error"))?
            )
        );

        Ok(())
    }
}