seaplane_cli/cli/cmds/locks/
acquire.rs1use clap::{ArgMatches, Command};
2
3use crate::{
4 api::LocksReq,
5 cli::cmds::locks::{common, common::SeaplaneLocksCommonArgMatches, CliCommand},
6 context::{Ctx, LocksCtx},
7 error::Result,
8 ops::locks::HeldLock,
9 printer::{Output, OutputFormat},
10};
11
12#[allow(missing_debug_implementations)]
16#[derive(Copy, Clone)]
17pub struct SeaplaneLocksAcquire;
18
19impl SeaplaneLocksAcquire {
20 pub fn command() -> Command {
21 Command::new("acquire")
22 .visible_alias("acq")
23 .about("Attempt to acquire the lock for N seconds")
24 .arg(common::lock_name())
25 .arg(common::ttl())
26 .arg(common::base64())
27 .arg(
28 arg!(--("client-id") - ('L') =["STRING"] required).help(
29 "Client-chosen identifier stored with the lock for informational purposes",
30 ),
31 )
32 }
33}
34
35impl CliCommand for SeaplaneLocksAcquire {
36 fn run(&self, ctx: &mut Ctx) -> Result<()> {
37 let mut req = LocksReq::new(ctx)?;
38 let locksctx = ctx.locks_ctx.get_mut_or_init();
39 let model_name = locksctx.lock_name.as_ref().map(|s| s.to_model());
40
41 req.set_identifiers(model_name, locksctx.lock_id.as_ref().map(|s| s.encoded().to_owned()))?;
42
43 let ttl = locksctx.ttl.as_ref().unwrap();
44 let client_id: &str = locksctx.client_id.as_ref().unwrap();
45 let held_lock_model = req.acquire(*ttl, client_id)?;
46
47 let held_lock = HeldLock {
48 lock_id: held_lock_model.id().encoded().to_owned(),
49 sequencer: held_lock_model.sequencer(),
50 };
51
52 match ctx.args.out_format {
53 OutputFormat::Json => held_lock.print_json(ctx)?,
54 OutputFormat::Table => held_lock.print_table(ctx)?,
55 }
56
57 Ok(())
58 }
59
60 fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
61 ctx.locks_ctx
62 .init(LocksCtx::from_locks_common(&SeaplaneLocksCommonArgMatches(matches))?);
63
64 ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
65 let mut locksctx = ctx.locks_ctx.get_mut().unwrap();
66 locksctx.ttl = matches.get_one::<u32>("ttl").copied();
67 locksctx.base64 = matches.get_flag("base64");
68 locksctx.client_id = Some(matches.get_one::<String>("client-id").unwrap().to_string());
69
70 Ok(())
71 }
72}