click 0.6.3

A command-line REPL for Kubernetes that integrates into existing cli workflows
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
// Copyright 2017 Databricks, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::command::format_duration;
use crate::command::time_since;
use crate::env::Env;
/// Stuff to handle outputting a table of resources, including
/// applying filters and sorting
use crate::output::ClickWriter;

use chrono::{DateTime, Duration, Utc};
use clap::ArgMatches;
use comfy_table::{Cell, CellAlignment, Color};
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use regex::Regex;

use std::borrow::Cow;
use std::cmp::{Ordering, PartialEq, PartialOrd};
use std::io::Write;

// TODO: Add config to use acsii style
//pub const ASCII_TABLE_STYLE: &str = "   - --       -    ";
pub const UTF8_TABLE_STYLE: &str = "   ─ ══       ─    ";

#[derive(Debug)]
enum CellSpecTxt<'a> {
    DateTime(DateTime<Utc>),
    Duration(Duration),
    Index,
    Int(i64),
    None,
    Quantity(Quantity),
    Str(Cow<'a, str>),
}

pub enum ColorType {
    Info,
    Success,
    Warn,
    Danger,
}

// An enum to hold either an actual color, or a color type like "success"
pub enum TableColor {
    Color(Color),
    ColorType(ColorType),
}

impl TableColor {
    fn to_color(&self, env: &Env) -> Color {
        match self {
            TableColor::Color(color) => *color,
            TableColor::ColorType(color_type) => match color_type {
                ColorType::Info => env.styles.info_color(),
                ColorType::Success => env.styles.success_color(),
                ColorType::Warn => env.styles.warning_color(),
                ColorType::Danger => env.styles.danger_color(),
            },
        }
    }
}

impl From<Color> for TableColor {
    fn from(color: Color) -> Self {
        TableColor::Color(color)
    }
}

impl From<ColorType> for TableColor {
    fn from(color_type: ColorType) -> Self {
        TableColor::ColorType(color_type)
    }
}

/// Holds a specification for a table cell
pub struct CellSpec<'a> {
    txt: CellSpecTxt<'a>,
    pub fg: Option<TableColor>,
    pub bg: Option<TableColor>,
    pub align: Option<CellAlignment>,
}

impl<'a> CellSpec<'a> {
    pub fn new_index() -> CellSpec<'a> {
        CellSpec {
            txt: CellSpecTxt::Index,
            fg: None,
            bg: None,
            align: None,
        }
    }

    pub fn new_int(num: i64) -> CellSpec<'a> {
        CellSpec {
            txt: CellSpecTxt::Int(num),
            fg: None,
            bg: None,
            align: None,
        }
    }

    pub fn with_colors(
        txt: Cow<'a, str>,
        fg: Option<TableColor>,
        bg: Option<TableColor>,
    ) -> CellSpec<'a> {
        CellSpec {
            txt: CellSpecTxt::Str(txt),
            fg,
            bg,
            align: None,
        }
    }

    pub fn _with_align(txt: Cow<'a, str>, align: CellAlignment) -> CellSpec<'a> {
        CellSpec {
            txt: CellSpecTxt::Str(txt),
            fg: None,
            bg: None,
            align: Some(align),
        }
    }

    pub fn to_cell(&self, index: usize, env: &Env) -> Cell {
        let cell = match &self.txt {
            CellSpecTxt::DateTime(datetime) => Cell::new(format_duration(time_since(*datetime))),
            CellSpecTxt::Duration(duration) => Cell::new(format_duration(*duration)),
            CellSpecTxt::Index => {
                Cell::new(format!("{index}").as_str()).set_alignment(CellAlignment::Right)
            }
            CellSpecTxt::Int(num) => {
                Cell::new(format!("{num}").as_str()).set_alignment(CellAlignment::Right)
            }
            CellSpecTxt::None => Cell::new("Unknown/None"),
            CellSpecTxt::Quantity(quant) => Cell::new(&quant.0),
            CellSpecTxt::Str(s) => Cell::new(s),
        };

        let cell = if let Some(a) = self.align {
            cell.set_alignment(a)
        } else {
            cell
        };

        let cell = if let Some(fg) = &self.fg {
            cell.fg(fg.to_color(env))
        } else {
            cell
        };

        if let Some(bg) = &self.bg {
            cell.bg(bg.to_color(env))
        } else {
            cell
        }
    }

    pub fn matches(&self, regex: &Regex) -> bool {
        match &self.txt {
            CellSpecTxt::Quantity(quant) => regex.is_match(&quant.0),
            CellSpecTxt::Index => false,
            CellSpecTxt::Str(s) => regex.is_match(s),
            _ => regex.is_match(&self.to_string()),
        }
    }
}

impl<'a> ToString for CellSpec<'a> {
    fn to_string(&self) -> String {
        match &self.txt {
            CellSpecTxt::DateTime(datetime) => format_duration(time_since(*datetime)),
            CellSpecTxt::Duration(duration) => format_duration(*duration),
            CellSpecTxt::Index => "[index]".to_string(),
            CellSpecTxt::Int(num) => format!("{num}"),
            CellSpecTxt::None => "Unknown/None".to_string(),
            CellSpecTxt::Quantity(quant) => quant.0.clone(),
            CellSpecTxt::Str(s) => s.to_string(),
        }
    }
}

impl<'a> From<&'a str> for CellSpec<'a> {
    fn from(s: &'a str) -> Self {
        CellSpec {
            txt: CellSpecTxt::Str(Cow::Borrowed(s)),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a> From<Cow<'a, str>> for CellSpec<'a> {
    fn from(c: Cow<'a, str>) -> Self {
        CellSpec {
            txt: CellSpecTxt::Str(c),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a> From<String> for CellSpec<'a> {
    fn from(s: String) -> Self {
        CellSpec {
            txt: CellSpecTxt::Str(Cow::Owned(s)),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a> From<i64> for CellSpec<'a> {
    fn from(num: i64) -> Self {
        CellSpec::new_int(num)
    }
}

impl<'a> From<i32> for CellSpec<'a> {
    fn from(num: i32) -> Self {
        CellSpec::new_int(num as i64)
    }
}

impl<'a> From<usize> for CellSpec<'a> {
    fn from(num: usize) -> Self {
        CellSpec::new_int(num as i64)
    }
}

impl<'a> From<Quantity> for CellSpec<'a> {
    fn from(quant: Quantity) -> Self {
        CellSpec {
            txt: CellSpecTxt::Quantity(quant),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a> From<Duration> for CellSpec<'a> {
    fn from(duration: Duration) -> Self {
        CellSpec {
            txt: CellSpecTxt::Duration(duration),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a> From<DateTime<Utc>> for CellSpec<'a> {
    fn from(dt: DateTime<Utc>) -> Self {
        CellSpec {
            txt: CellSpecTxt::DateTime(dt),
            fg: None,
            bg: None,
            align: None,
        }
    }
}

impl<'a, T> From<Option<T>> for CellSpec<'a>
where
    T: Into<CellSpec<'a>>,
{
    fn from(opt: Option<T>) -> Self {
        match opt {
            Some(v) => v.into(),
            None => CellSpec {
                txt: CellSpecTxt::None,
                fg: None,
                bg: None,
                align: None,
            },
        }
    }
}
impl<'a> PartialEq for CellSpec<'a> {
    fn eq(&self, other: &Self) -> bool {
        match (&self.txt, &other.txt) {
            (CellSpecTxt::DateTime(dt1), CellSpecTxt::DateTime(dt2)) => dt1.eq(dt2),
            (CellSpecTxt::Duration(dur1), CellSpecTxt::Duration(dur2)) => dur1 == dur2,
            (CellSpecTxt::Index, CellSpecTxt::Index) => true,
            (CellSpecTxt::Int(num1), CellSpecTxt::Int(num2)) => num1 == num2,
            (CellSpecTxt::None, CellSpecTxt::None) => true,
            (CellSpecTxt::Quantity(quant1), CellSpecTxt::Quantity(quant2)) => quant1 == quant2,
            (CellSpecTxt::Str(st), CellSpecTxt::Str(ot)) => st == ot,
            _ => false,
        }
    }
}
impl<'a> Eq for CellSpec<'a> {}

impl<'a> PartialOrd for CellSpec<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (&self.txt, &other.txt) {
            (CellSpecTxt::DateTime(dt1), CellSpecTxt::DateTime(dt2)) => dt1.partial_cmp(dt2),
            (CellSpecTxt::Duration(dur1), CellSpecTxt::Duration(dur2)) => dur1.partial_cmp(dur2),
            (CellSpecTxt::Index, CellSpecTxt::Index) => Some(Ordering::Equal),
            (CellSpecTxt::Int(num1), CellSpecTxt::Int(num2)) => num1.partial_cmp(num2),
            (CellSpecTxt::None, CellSpecTxt::None) => Some(Ordering::Equal),
            (CellSpecTxt::Quantity(quant1), CellSpecTxt::Quantity(quant2)) => {
                raw_quantity(quant1).partial_cmp(&raw_quantity(quant2))
            }
            (CellSpecTxt::Str(st), CellSpecTxt::Str(ot)) => st.partial_cmp(ot),
            (CellSpecTxt::None, _) => Some(Ordering::Greater), // none is the least
            (_, CellSpecTxt::None) => Some(Ordering::Less),    // none is the least
            _ => None,
        }
    }
}

impl<'a> Ord for CellSpec<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.partial_cmp(other) {
            Some(o) => o,
            None => Ordering::Equal,
        }
    }
}

// convert a Quantity to a raw number. We assume the quantity has been serialized according to
// the rules in the docs such that:
//  Before serializing, Quantity will be put in “canonical form”. This means that Exponent/suffix
//  will be adjusted up or down (with a corresponding increase or decrease in Mantissa) such that:
//  a. No precision is lost b. No fractional digits will be emitted c. The exponent (or suffix) is
//  as large as possible. The sign will be omitted unless the number is negative.
// Specifically we assume no fractional quantity.
// Anything with an invalid format is converted to 0.0
pub fn raw_quantity(quantity: &Quantity) -> f64 {
    let mut chars = quantity.0.chars().peekable();
    let has_neg = chars.peek().unwrap_or(&'+').eq(&'-');
    if has_neg {
        chars.next();
    }

    // find location of first non-digit
    let mut split = match chars.position(|c| !c.is_ascii_digit()) {
        Some(pos) => pos,
        None => {
            // no non digit, just parse as a raw number, set split to end of string
            let mut len = quantity.0.len();
            if has_neg {
                len -= 1; // since we'll add one below :)
            }
            len
        }
    };

    if has_neg {
        split += 1; // shift over for the -
    }

    let digits = if has_neg {
        &quantity.0[1..split]
    } else {
        &quantity.0[..split]
    };

    let amt = digits.parse::<i64>().unwrap();
    let suffix = &quantity.0[split..];

    let base10: i64 = 10;

    if suffix.len() > 1 && (suffix.starts_with('e') || suffix.starts_with('E')) {
        // our suffix has more than one char and starts with e/E, so it should be a decimal exponent
        match (suffix[1..]).parse::<u32>() {
            Ok(exp) => {
                let famt = (amt * base10.pow(exp)) as f64;
                if has_neg {
                    return -famt;
                } else {
                    return famt;
                }
            }
            Err(_) => {
                println!("Invalid suffix for quantity: {suffix}");
                return 0.0;
            }
        }
    }

    let bytes = match suffix {
        "" => amt,
        "m" => {
            // this is the only branch that could actually produce a fraction, so we handle it
            // specially
            let famt = amt as f64;
            let famt = famt / (base10.pow(3) as f64);
            if has_neg {
                return -famt;
            } else {
                return famt;
            }
        }
        "Ki" => (amt * 2) << 9,
        "Mi" => (amt * 2) << 19,
        "Gi" => (amt * 2) << 29,
        "Ti" => (amt * 2) << 39,
        "Pi" => (amt * 2) << 49,
        "Ei" => (amt * 2) << 59,
        "k" => amt * base10.pow(3),
        "M" => amt * base10.pow(6),
        "G" => amt * base10.pow(9),
        "T" => amt * base10.pow(12),
        "P" => amt * base10.pow(15),
        "E" => amt * base10.pow(18),
        _ => {
            println!("Invalid suffix for quantity {suffix}");
            0
        }
    };

    if has_neg {
        -bytes as f64
    } else {
        bytes as f64
    }
}

pub fn get_regex(matches: &ArgMatches) -> Result<Option<Regex>, String> {
    match matches.get_one::<String>("regex").map(|s| s.as_str()) {
        Some(pattern) => {
            if let Ok(regex) = Regex::new(pattern) {
                Ok(Some(regex))
            } else {
                Err(format!("Invalid regex: {pattern}"))
            }
        }
        None => Ok(None),
    }
}

pub fn print_filled_table(table: &mut comfy_table::Table, writer: &mut ClickWriter) {
    table.load_preset(UTF8_TABLE_STYLE);
    table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);
    clickwriteln!(writer, "{table}");
}

#[allow(clippy::ptr_arg)]
pub fn print_table<T: Into<comfy_table::Row>>(
    titles: T,
    specs: Vec<Vec<CellSpec<'_>>>,
    env: &Env,
    writer: &mut ClickWriter,
) -> comfy_table::Table {
    let mut table = comfy_table::Table::new();
    table.load_preset(UTF8_TABLE_STYLE);
    table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);
    table.set_header(titles);
    for (index, t_spec) in specs.iter().enumerate() {
        let row_vec: Vec<Cell> = t_spec.iter().map(|spec| spec.to_cell(index, env)).collect();
        table.add_row(row_vec);
    }
    clickwriteln!(writer, "{table}");
    table
}

#[cfg(test)]
mod tests {
    use crate::table::raw_quantity;
    use k8s_openapi::apimachinery::pkg::api::resource::Quantity;

    #[test]
    fn test_raw_quantity() {
        assert_eq!(raw_quantity(&Quantity("1500m".to_string())), 1.5);
        assert_eq!(raw_quantity(&Quantity("-1500m".to_string())), -1.5);
        assert_eq!(raw_quantity(&Quantity("1Ki".to_string())), 1024.0);
        assert_eq!(raw_quantity(&Quantity("2Gi".to_string())), 2147483648.0);
        assert_eq!(raw_quantity(&Quantity("12e6".to_string())), 12000000.0);
        assert_eq!(raw_quantity(&Quantity("12E6".to_string())), 12000000.0);
        assert_eq!(raw_quantity(&Quantity("-22E6".to_string())), -22000000.0);
        assert_eq!(raw_quantity(&Quantity("3G".to_string())), 3000000000.0);
        assert_eq!(raw_quantity(&Quantity("34".to_string())), 34.0);
        assert_eq!(raw_quantity(&Quantity("-3456".to_string())), -3456.0);
    }
}