Skip to content

Commit b7299d6

Browse files
committed
introduce subcommands and lock repository/keys
1 parent 7d55763 commit b7299d6

File tree

1 file changed

+88
-14
lines changed

1 file changed

+88
-14
lines changed

src/commands/lock.rs

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,90 @@ use abscissa_core::{Command, Runnable, Shutdown};
88
use anyhow::Result;
99
use chrono::{DateTime, Duration, Local};
1010

11-
use rustic_core::LockOptions;
11+
use rustic_core::{FileType, LockOptions};
1212

13-
/// `prune` subcommand
14-
#[allow(clippy::struct_excessive_bools)]
15-
#[derive(clap::Parser, Command, Debug, Clone)]
13+
/// `lock` subcommand
14+
#[derive(clap::Parser, Command, Debug)]
1615
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(FileType::Key, self.duration.0)?;
89+
Ok(())
90+
}
91+
}
92+
93+
#[derive(clap::Parser, Command, Debug, Clone)]
94+
pub(crate) struct SnapSubCmd {
1795
/// Extend locks even if the files are already locked long enough
1896
#[clap(long)]
1997
always_extend_lock: bool,
@@ -44,7 +122,7 @@ impl FromStr for LockDuration {
44122
}
45123
}
46124

47-
impl Runnable for LockCmd {
125+
impl Runnable for SnapSubCmd {
48126
fn run(&self) {
49127
if let Err(err) = self.inner_run() {
50128
status_err!("{}", err);
@@ -53,7 +131,7 @@ impl Runnable for LockCmd {
53131
}
54132
}
55133

56-
impl LockCmd {
134+
impl SnapSubCmd {
57135
fn inner_run(&self) -> Result<()> {
58136
let config = RUSTIC_APP.config();
59137
let repo = open_repository(&config.repository)?;
@@ -64,15 +142,11 @@ impl LockCmd {
64142
repo.get_snapshots(&self.ids)?
65143
};
66144

67-
if config.global.dry_run {
68-
println!("lock is not supported in dry-run mode");
69-
} else {
70-
let lock_opts = LockOptions::default()
71-
.always_extend_lock(self.always_extend_lock)
72-
.until(self.duration.0);
145+
let lock_opts = LockOptions::default()
146+
.always_extend_lock(self.always_extend_lock)
147+
.until(self.duration.0);
73148

74-
repo.lock(&lock_opts, &snapshots)?;
75-
}
149+
repo.lock_snaphots(&lock_opts, &snapshots)?;
76150

77151
Ok(())
78152
}

0 commit comments

Comments
 (0)