seaplane_cli/cli/cmds/metadata/
get.rs

1use clap::{ArgMatches, Command};
2
3use crate::{
4    api::MetadataReq,
5    cli::{cmds::metadata::common, CliCommand},
6    context::{Ctx, MetadataCtx},
7    error::Result,
8    printer::{Output, OutputFormat},
9};
10
11static LONG_ABOUT: &str = "Retrieve a metadata key-value pair
12
13Keys and values will be displayed in base64 encoded format by default because they may contain
14arbitrary binary data. Use --decode to output the decoded values instead.";
15
16#[derive(Copy, Clone, Debug)]
17pub struct SeaplaneMetadataGet;
18
19impl SeaplaneMetadataGet {
20    pub fn command() -> Command {
21        // TODO: add a way to elide long keys or values with ... after a certain char count
22        Command::new("get")
23            .visible_alias("show")
24            .about("Retrieve a metadata key-value pair")
25            .long_about(LONG_ABOUT)
26            .arg(common::single_key())
27            .arg(common::base64())
28            .args(common::display_args())
29            .group(common::keys_or_values())
30            .mut_arg("no-header", |a| a.hide(true))
31            .mut_arg("only-keys", |a| a.hide(true))
32            .mut_arg("only-values", |a| a.hide(true))
33    }
34}
35
36impl CliCommand for SeaplaneMetadataGet {
37    fn run(&self, ctx: &mut Ctx) -> Result<()> {
38        let kvs = {
39            let mut req = MetadataReq::new(ctx)?;
40            let mdctx = ctx.md_ctx.get_mut_or_init();
41            for kv in mdctx.kvs.iter_mut() {
42                req.set_key(kv.key.to_string())?;
43                kv.set_value(
44                    // The key is already in Base64 so no need to convert
45                    req.get_value()?.to_string(),
46                );
47            }
48
49            mdctx.kvs.clone()
50        };
51        match ctx.args.out_format {
52            OutputFormat::Json => kvs.print_json(ctx)?,
53            OutputFormat::Table => kvs.print_table(ctx)?,
54        }
55
56        Ok(())
57    }
58
59    fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
60        ctx.md_ctx
61            .init(MetadataCtx::from_md_common(&common::SeaplaneMetadataCommonArgMatches(matches))?);
62        ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
63        let mut mdctx = ctx.md_ctx.get_mut_or_init();
64        mdctx.decode = matches.get_flag("decode");
65        mdctx.no_header = true;
66        mdctx.no_keys = true;
67        mdctx.no_values = false;
68        Ok(())
69    }
70}