seaplane_cli/cli/cmds/restrict/
list.rs

1use clap::{ArgMatches, Command};
2
3use crate::{
4    api::RestrictReq,
5    cli::{cmds::restrict::common, CliCommand},
6    context::{Ctx, RestrictCtx},
7    error::Result,
8    ops::restrict::Restrictions,
9    printer::{Output, OutputFormat},
10};
11
12static LONG_ABOUT: &str = "List restrictions in an API, or across all APIs
13
14Directory will be displayed in base64 encoded format by default because they may contain
15arbitrary binary data. Use --decode to output the decoded values instead.";
16
17/// A newtype wrapper to enforce where the ArgMatches came from which reduces errors in checking if
18/// values of arguments were used or not. i.e. `seaplane formation create` may not have the same
19/// arguments as `seaplane account token` even though both produce an `ArgMatches`.
20#[allow(missing_debug_implementations)]
21pub struct SeaplaneRestrictListArgMatches<'a>(pub &'a ArgMatches);
22
23#[derive(Copy, Clone, Debug)]
24pub struct SeaplaneRestrictList;
25
26impl SeaplaneRestrictList {
27    pub fn command() -> Command {
28        Command::new("list")
29            .visible_alias("ls")
30            .about("List restrictions in an API, or across all APIs")
31            .long_about(LONG_ABOUT)
32            .arg(arg!(api = ["API"]).help("The API to list the restrictions from"))
33            .arg(common::base64())
34            .args(common::display_args())
35    }
36}
37
38impl CliCommand for SeaplaneRestrictList {
39    fn run(&self, ctx: &mut Ctx) -> Result<()> {
40        let restrictions = {
41            let mut req = RestrictReq::new(ctx)?;
42            let restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
43            restrict_ctx.api.as_ref().map(|api| req.set_api(api));
44
45            Restrictions::from_model(req.get_all_pages()?)
46        };
47
48        match ctx.args.out_format {
49            OutputFormat::Json => restrictions.print_json(ctx)?,
50            OutputFormat::Table => restrictions.print_table(ctx)?,
51        }
52        Ok(())
53    }
54
55    fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
56        ctx.restrict_ctx
57            .init(RestrictCtx::from_restrict_list(&SeaplaneRestrictListArgMatches(matches))?);
58        ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
59        let mut restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
60        restrict_ctx.decode = matches.get_flag("decode");
61        restrict_ctx.no_header = matches.get_flag("no-header");
62        Ok(())
63    }
64
65    fn next_subcmd<'a>(
66        &self,
67        _matches: &'a ArgMatches,
68    ) -> Option<(Box<dyn CliCommand>, &'a ArgMatches)> {
69        None
70    }
71}