seaplane_cli/cli/cmds/locks/
renew.rs1use clap::{ArgMatches, Command};
2use seaplane::api::locks::v1::LockId;
3use serde_json::json;
4
5use crate::{
6 api::LocksReq,
7 cli::cmds::locks::{common, common::SeaplaneLocksCommonArgMatches, CliCommand},
8 context::{Ctx, LocksCtx},
9 error::Result,
10 printer::OutputFormat,
11};
12
13#[derive(Copy, Clone, Debug)]
14pub struct SeaplaneLocksRenew;
15
16impl SeaplaneLocksRenew {
17 pub fn command() -> Command {
18 Command::new("renew")
19 .about("Attempt to renew the lock for N seconds")
20 .arg(common::lock_name())
21 .arg(common::lock_id())
22 .arg(common::ttl())
23 .arg(common::base64())
24 }
25}
26
27impl CliCommand for SeaplaneLocksRenew {
28 fn run(&self, ctx: &mut Ctx) -> Result<()> {
29 let mut req = LocksReq::new(ctx)?;
30 let locksctx = ctx.locks_ctx.get_mut_or_init();
31 let model_name = locksctx.lock_name.as_ref().map(|s| s.to_model());
32
33 req.set_identifiers(model_name, locksctx.lock_id.as_ref().map(|s| s.encoded().to_owned()))?;
34
35 let ttl = locksctx.ttl.unwrap();
36 req.renew(ttl)?;
37
38 if ctx.args.out_format == OutputFormat::Table {
39 cli_println!("Successfully renewed the lock");
40 } else {
41 cli_println!(
42 "{}",
43 json!({"name": ctx.locks_ctx.get_or_init().lock_name.as_ref().unwrap()})
44 )
45 }
46 Ok(())
47 }
48
49 fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
50 ctx.locks_ctx
51 .init(LocksCtx::from_locks_common(&SeaplaneLocksCommonArgMatches(matches))?);
52
53 ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
54 let mut locksctx = ctx.locks_ctx.get_mut().unwrap();
55 locksctx.base64 = matches.get_flag("base64");
56 let raw_lock_id = matches.get_one::<String>("lock-id").unwrap();
57 locksctx.lock_id = Some(LockId::from_encoded(raw_lock_id));
58 locksctx.ttl = matches.get_one::<u32>("ttl").copied();
59
60 Ok(())
61 }
62}