seaplane_cli/cli/cmds/restrict/
set.rs1use clap::{ArgMatches, Command};
2use serde_json::json;
3
4use crate::{
5 api::RestrictReq,
6 cli::{cmds::restrict::common, specs::REGION_SPEC, CliCommand},
7 context::{Ctx, RestrictCtx},
8 error::Result,
9 ops::EncodedString,
10 printer::OutputFormat,
11};
12
13#[allow(missing_debug_implementations)]
17pub struct SeaplaneRestrictSetArgMatches<'a>(pub &'a ArgMatches);
18
19#[derive(Copy, Clone, Debug)]
20pub struct SeaplaneRestrictSet;
21
22impl SeaplaneRestrictSet {
23 pub fn command() -> Command {
24 Command::new("set")
25 .visible_alias("put")
26 .about("Set a restriction")
27 .arg(common::api())
28 .arg(common::directory())
29 .arg(common::base64())
30 .args(common::display_args())
31 .next_display_order(0)
32 .next_help_heading("RESTRICTION DETAILS")
33 .args(common::restriction_details())
34 .after_help(REGION_SPEC)
35 .mut_arg("no-header", |a| a.hide(true))
36 }
37}
38
39impl CliCommand for SeaplaneRestrictSet {
40 fn run(&self, ctx: &mut Ctx) -> Result<()> {
41 let mut req = RestrictReq::new(ctx)?;
42 let restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
43 let api = restrict_ctx.api.as_ref().unwrap();
44 let mut dir = restrict_ctx.directory.as_ref().unwrap().to_string();
45 let details = restrict_ctx.restriction_details()?;
46
47 req.set_api(api)?;
48 req.set_directory(&dir)?;
49 req.set_restriction(details)?;
50
51 if ctx.args.out_format == OutputFormat::Table {
52 if restrict_ctx.decode {
53 let es = EncodedString::new(dir);
54 dir = String::from_utf8_lossy(&es.decoded()?).to_string()
55 };
56 cli_println!("Set a restriction on directory {} in {} API", dir, api);
57 } else {
58 cli_println!("{}", json!({"set_restriction": {"api": api, "directory": dir} }))
59 }
60 Ok(())
61 }
62
63 fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
64 ctx.restrict_ctx
65 .init(RestrictCtx::from_restrict_set(&SeaplaneRestrictSetArgMatches(matches))?);
66 ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
67 let mut restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
68 restrict_ctx.decode = matches.get_flag("decode");
69 Ok(())
70 }
71}