|
| 1 | +//! `lock` subcommand |
| 2 | +
|
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +use crate::{commands::open_repository, status_err, Application, RUSTIC_APP}; |
| 6 | +use abscissa_core::{Command, Runnable, Shutdown}; |
| 7 | + |
| 8 | +use anyhow::Result; |
| 9 | +use chrono::{DateTime, Duration, Local}; |
| 10 | + |
| 11 | +use rustic_core::{repofile::KeyId, LockOptions}; |
| 12 | + |
| 13 | +/// `lock` subcommand |
| 14 | +#[derive(clap::Parser, Command, Debug)] |
| 15 | +pub(crate) struct LockCmd { |
| 16 | + /// Subcommand to run |
| 17 | + #[clap(subcommand)] |
| 18 | + cmd: LockSubCmd, |
| 19 | +} |
| 20 | + |
| 21 | +impl Runnable for LockCmd { |
| 22 | + fn run(&self) { |
| 23 | + let config = RUSTIC_APP.config(); |
| 24 | + if config.global.dry_run { |
| 25 | + println!("lock is not supported in dry-run mode"); |
| 26 | + } else { |
| 27 | + self.cmd.run(); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// `lock` subcommand |
| 33 | +#[derive(clap::Subcommand, Debug, Runnable)] |
| 34 | +enum LockSubCmd { |
| 35 | + /// Lock the complete repository |
| 36 | + Repository(RepoSubCmd), |
| 37 | + /// Lock all key files |
| 38 | + Keys(KeysSubCmd), |
| 39 | + /// Lock snapshots and relevant pack files |
| 40 | + Snapshots(SnapSubCmd), |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(clap::Parser, Command, Debug, Clone)] |
| 44 | +pub(crate) struct RepoSubCmd { |
| 45 | + #[clap(long)] |
| 46 | + /// Duration for how long to extend the locks (e.g. "10d"). "forever" is also allowed |
| 47 | + duration: LockDuration, |
| 48 | +} |
| 49 | + |
| 50 | +impl Runnable for RepoSubCmd { |
| 51 | + fn run(&self) { |
| 52 | + if let Err(err) = self.inner_run() { |
| 53 | + status_err!("{}", err); |
| 54 | + RUSTIC_APP.shutdown(Shutdown::Crash); |
| 55 | + }; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl RepoSubCmd { |
| 60 | + fn inner_run(&self) -> Result<()> { |
| 61 | + let config = RUSTIC_APP.config(); |
| 62 | + let repo = open_repository(&config.repository)?; |
| 63 | + repo.lock_repo(self.duration.0)?; |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(clap::Parser, Command, Debug, Clone)] |
| 69 | +pub(crate) struct KeysSubCmd { |
| 70 | + #[clap(long)] |
| 71 | + /// Duration for how long to extend the locks (e.g. "10d"). "forever" is also allowed |
| 72 | + duration: LockDuration, |
| 73 | +} |
| 74 | + |
| 75 | +impl Runnable for KeysSubCmd { |
| 76 | + fn run(&self) { |
| 77 | + if let Err(err) = self.inner_run() { |
| 78 | + status_err!("{}", err); |
| 79 | + RUSTIC_APP.shutdown(Shutdown::Crash); |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl KeysSubCmd { |
| 85 | + fn inner_run(&self) -> Result<()> { |
| 86 | + let config = RUSTIC_APP.config(); |
| 87 | + let repo = open_repository(&config.repository)?; |
| 88 | + repo.lock_repo_files::<KeyId>(self.duration.0)?; |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[derive(clap::Parser, Command, Debug, Clone)] |
| 94 | +pub(crate) struct SnapSubCmd { |
| 95 | + /// Extend locks even if the files are already locked long enough |
| 96 | + #[clap(long)] |
| 97 | + always_extend_lock: bool, |
| 98 | + |
| 99 | + #[clap(long)] |
| 100 | + /// Duration for how long to extend the locks (e.g. "10d"). "forever" is also allowed |
| 101 | + duration: LockDuration, |
| 102 | + |
| 103 | + /// Snapshots to lock. If none is given, use filter options to filter from all snapshots |
| 104 | + #[clap(value_name = "ID")] |
| 105 | + ids: Vec<String>, |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, Clone)] |
| 109 | +struct LockDuration(Option<DateTime<Local>>); |
| 110 | + |
| 111 | +impl FromStr for LockDuration { |
| 112 | + type Err = anyhow::Error; |
| 113 | + fn from_str(s: &str) -> Result<Self> { |
| 114 | + match s { |
| 115 | + "forever" => Ok(Self(None)), |
| 116 | + d => { |
| 117 | + let duration = humantime::Duration::from_str(d)?; |
| 118 | + let duration = Duration::from_std(*duration)?; |
| 119 | + Ok(Self(Some(Local::now() + duration))) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl Runnable for SnapSubCmd { |
| 126 | + fn run(&self) { |
| 127 | + if let Err(err) = self.inner_run() { |
| 128 | + status_err!("{}", err); |
| 129 | + RUSTIC_APP.shutdown(Shutdown::Crash); |
| 130 | + }; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl SnapSubCmd { |
| 135 | + fn inner_run(&self) -> Result<()> { |
| 136 | + let config = RUSTIC_APP.config(); |
| 137 | + let repo = open_repository(&config.repository)?; |
| 138 | + |
| 139 | + let snapshots = if self.ids.is_empty() { |
| 140 | + repo.get_matching_snapshots(|sn| config.snapshot_filter.matches(sn))? |
| 141 | + } else { |
| 142 | + repo.get_snapshots(&self.ids)? |
| 143 | + }; |
| 144 | + |
| 145 | + let lock_opts = LockOptions::default() |
| 146 | + .always_extend_lock(self.always_extend_lock) |
| 147 | + .until(self.duration.0); |
| 148 | + |
| 149 | + repo.lock_snaphots(&lock_opts, &snapshots)?; |
| 150 | + |
| 151 | + Ok(()) |
| 152 | + } |
| 153 | +} |
0 commit comments