seaplane_cli/cli/cmds/restrict/
delete.rs

1use clap::{ArgMatches, Command};
2use serde_json::json;
3
4use crate::{
5    api::RestrictReq,
6    cli::cmds::restrict::{common, common::SeaplaneRestrictCommonArgMatches, CliCommand},
7    context::{Ctx, RestrictCtx},
8    error::Result,
9    ops::EncodedString,
10    printer::OutputFormat,
11};
12
13#[derive(Copy, Clone, Debug)]
14pub struct SeaplaneRestrictDelete;
15
16impl SeaplaneRestrictDelete {
17    pub fn command() -> Command {
18        Command::new("delete")
19            .visible_aliases(["del", "remove", "rm"])
20            .about("Delete a restriction on directory")
21            .arg(common::api())
22            .arg(common::directory())
23            .arg(common::base64())
24            .args(common::display_args())
25            .mut_arg("no-header", |a| a.hide(true))
26    }
27}
28
29impl CliCommand for SeaplaneRestrictDelete {
30    fn run(&self, ctx: &mut Ctx) -> Result<()> {
31        let mut req = RestrictReq::new(ctx)?;
32        let restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
33        let api = restrict_ctx.api.as_ref().unwrap();
34        let mut dir = restrict_ctx.directory.as_ref().unwrap().to_string();
35        req.set_api(api)?;
36        req.set_directory(&dir)?;
37        req.delete_restriction()?;
38
39        if ctx.args.out_format == OutputFormat::Table {
40            if restrict_ctx.decode {
41                let es = EncodedString::new(dir);
42                dir = String::from_utf8_lossy(&es.decoded()?).to_string()
43            };
44            cli_println!("Deleted a restriction on directory {} in {} API", dir, api);
45        } else {
46            cli_println!("{}", json!({"deleted_restriction": {"api": api, "directory": dir} }))
47        }
48        Ok(())
49    }
50
51    fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
52        ctx.restrict_ctx
53            .init(RestrictCtx::from_restrict_common(&SeaplaneRestrictCommonArgMatches(matches))?);
54        ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
55        let mut restrict_ctx = ctx.restrict_ctx.get_mut_or_init();
56        restrict_ctx.decode = matches.get_flag("decode");
57        Ok(())
58    }
59}