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
use std::{
    fs::File,
    io::{self, Read},
};

use seaplane::api::{metadata::v1::Key, shared::v1::Directory};

use crate::{
    cli::cmds::metadata::{SeaplaneMetadataCommonArgMatches, SeaplaneMetadataSetArgMatches},
    error::{CliError, Context, Result},
    ops::metadata::{KeyValue, KeyValues},
    printer::Color,
};

/// Represents the "Source of Truth" i.e. it combines all the CLI options, ENV vars, and config
/// values into a single structure that can be used later to build models for the API or local
/// structs for serializing
// TODO: we may not want to derive this we implement circular references
#[derive(Debug, Default, Clone)]
pub struct MetadataCtx {
    pub kvs: KeyValues,
    pub directory: Option<Directory>,
    /// Is the key or value already URL safe base64 encoded
    pub base64: bool,
    /// Print with decoding
    pub decode: bool,
    /// Print with safe decoding
    pub decode_safe: bool,
    /// Print with no decoding
    pub no_decode: bool,
    /// A base64 encoded key
    pub from: Option<Key>,
    /// Skip the KEY or VALUE header in --format=table
    pub no_header: bool,
    /// Don't print keys
    pub no_keys: bool,
    /// Don't print values
    pub no_values: bool,
    /// Max width of keys
    pub keys_width_limit: usize,
    /// Max width of values
    pub values_width_limit: usize,
}

impl MetadataCtx {
    /// Builds a MetadataCtx from ArgMatches
    pub fn from_md_common(matches: &SeaplaneMetadataCommonArgMatches) -> Result<MetadataCtx> {
        let matches = matches.0;
        let base64 = matches.contains_id("base64");
        let raw_keys: Vec<_> = matches.get_many::<String>("key").unwrap().collect();

        let mut kvs = KeyValues::default();
        for key in raw_keys {
            if base64 {
                // Check that what the user passed really is valid base64
                let _ = base64::decode_config(key, base64::URL_SAFE_NO_PAD)?;
                kvs.push(KeyValue::from_key(key));
            } else {
                kvs.push(KeyValue::from_key_unencoded(key));
            }
        }

        Ok(MetadataCtx {
            kvs,
            base64: true, // At this point all keys and values should be encoded as base64
            ..MetadataCtx::default()
        })
    }

    /// Builds a MetadataCtx from ArgMatches
    pub fn from_md_set(matches: &SeaplaneMetadataSetArgMatches) -> Result<MetadataCtx> {
        let matches = matches.0;
        let base64 = matches.contains_id("base64");
        let raw_key = matches.get_one::<String>("key").unwrap();
        let raw_value = matches.get_one::<String>("value").unwrap();
        let value = if let Some(val) = raw_value.strip_prefix('@') {
            if val == "-" {
                let mut buf: Vec<u8> = Vec::new();
                let stdin = io::stdin();
                let mut stdin_lock = stdin.lock();
                stdin_lock.read_to_end(&mut buf)?;
                buf
            } else {
                let mut f = File::open(val)
                    .map_err(CliError::from)
                    .context("\n\tpath: ")
                    .with_color_context(|| (Color::Yellow, val))?;

                // TODO: @perf we could pre-allocate the vec based on the file size
                let mut buf = Vec::new();

                f.read_to_end(&mut buf)?;
                buf
            }
        } else {
            raw_value.as_bytes().to_vec()
        };

        let kv = if base64 {
            // make sure it's valid base64
            let _ = base64::decode_config(raw_key, base64::URL_SAFE_NO_PAD)?;
            let _ = base64::decode_config(&value, base64::URL_SAFE_NO_PAD)?;
            // The user used `--base64` and it is valid base64 so there is no reason the from_utf8
            // should fail
            KeyValue::new(raw_key, &String::from_utf8(value)?)
        } else {
            KeyValue::new(
                base64::encode_config(raw_key, base64::URL_SAFE_NO_PAD),
                base64::encode_config(value, base64::URL_SAFE_NO_PAD),
            )
        };

        let mut kvs = KeyValues::default();
        kvs.push(kv);

        Ok(MetadataCtx { kvs, base64: true, ..MetadataCtx::default() })
    }
}