Skip to content

Commit c571279

Browse files
aawsomesimonsan
andauthored
feat: Add --filter-jq option (#1372)
Adds filtering using `jq` syntax. You can do something like `--filter-jq '.summary.files_new > 51'` - works similar to what you'd expect from `jq`. Also puts the `rhai` and `jq` filtering into features which are enabled by default. --------- Signed-off-by: simonsan <[email protected]> Co-authored-by: simonsan <[email protected]>
1 parent 5b02cd2 commit c571279

8 files changed

+225
-33
lines changed

Cargo.lock

+68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rustic - fast, encrypted, deduplicated backups powered by Rust
1818
"""
1919

2020
[features]
21-
default = ["tui", "webdav"]
21+
default = ["tui", "webdav", "rhai", "jq"]
2222
release = ["default", "self-update"]
2323

2424
# Allocators
@@ -37,6 +37,10 @@ webdav = [
3737
]
3838
mount = ["dep:fuse_mt"]
3939

40+
# Filtering
41+
rhai = ["dep:rhai"]
42+
jq = ["dep:jaq-core", "dep:jaq-std", "dep:jaq-json"]
43+
4044
[[bin]]
4145
name = "rustic"
4246
path = "src/bin/rustic.rs"
@@ -87,7 +91,6 @@ serde_with = { version = "3", features = ["base64"] }
8791
aho-corasick = "1"
8892
chrono = { version = "0.4", default-features = false, features = ["clock", "serde"] }
8993
comfy-table = "7"
90-
rhai = { version = "1", features = ["sync", "serde", "no_optimize", "no_module", "no_custom_syntax", "only_i64"] }
9194
scopeguard = "1"
9295
semver = { version = "1", optional = true }
9396
simplelog = "0.12"
@@ -117,6 +120,12 @@ self_update = { version = "=0.39.0", default-features = false, optional = true,
117120
tar = "0.4.43"
118121
toml = "0.8"
119122

123+
# filtering
124+
jaq-core = { version = "2", optional = true }
125+
jaq-json = { version = "1", features = ["serde_json"], optional = true }
126+
jaq-std = { version = "2", optional = true }
127+
rhai = { version = "1", features = ["sync", "serde", "no_optimize", "no_module", "no_custom_syntax", "only_i64"], optional = true }
128+
120129
[dev-dependencies]
121130
abscissa_core = { version = "0.8.1", default-features = false, features = ["testing"] }
122131
assert_cmd = "2.0.16"

config/README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,22 @@ See [Global Hooks](#global-hooks-globalhooks).
159159

160160
### Snapshot-Filter Options `[snapshot-filter]`
161161

162-
| Attribute | Description | Default Value | Example Value | CLI Option |
163-
| ------------------ | ---------------------------------------------------------------------- | ------------- | ------------------------ | -------------------- |
164-
| filter-hosts | Array of hosts to filter snapshots. | Not set | ["myhost", "host2"] | --filter-host |
165-
| filter-labels | Array of labels to filter snapshots. | Not set | ["mylabal"] | --filter-label |
166-
| filter-paths | Array of pathlists to filter snapshots. | Not set | ["/home,/root"] | --filter-paths |
167-
| filter-paths-exact | Array or string of paths to filter snapshots. Exact match. | Not set | ["path1,path2", "path3"] | --filter-paths-exact |
168-
| filter-tags | Array of taglists to filter snapshots. | Not set | ["tag1,tag2"] | --filter-tags |
169-
| filter-tags-exact | Array or string of tags to filter snapshots. Exact match. | Not set | ["tag1,tag2", "tag3"] | --filter-tags-exact |
170-
| filter-before | Filter snapshots before the given date/time | Not set | "2024-01-01" | --filter-before |
171-
| filter-after | Filter snapshots after the given date/time | Not set | "2023-01-01 11:15:23" | --filter-after |
172-
| filter-size | Filter snapshots for a total size in the size range. | Not set | "1MB..1GB" | --filter-size |
173-
| | If a single value is given, this is taken as lower bound. | | "500 k" | |
174-
| filter-size-added | Filter snapshots for a size added to the repository in the size range. | Not set | "1MB..1GB" | --filter-size-added |
175-
| | If a single value is given, this is taken as lower bound. | | "500 k" | |
176-
| filter-fn | Custom filter function for snapshots. | Not set | | --filter-fn |
162+
| Attribute | Description | Default Value | Example Value | CLI Option |
163+
| ------------------ | ---------------------------------------------------------------------- | ------------- | -------------------------- | -------------------- |
164+
| filter-hosts | Array of hosts to filter snapshots. | Not set | ["myhost", "host2"] | --filter-host |
165+
| filter-labels | Array of labels to filter snapshots. | Not set | ["mylabal"] | --filter-label |
166+
| filter-paths | Array of pathlists to filter snapshots. | Not set | ["/home,/root"] | --filter-paths |
167+
| filter-paths-exact | Array or string of paths to filter snapshots. Exact match. | Not set | ["path1,path2", "path3"] | --filter-paths-exact |
168+
| filter-tags | Array of taglists to filter snapshots. | Not set | ["tag1,tag2"] | --filter-tags |
169+
| filter-tags-exact | Array or string of tags to filter snapshots. Exact match. | Not set | ["tag1,tag2", "tag3"] | --filter-tags-exact |
170+
| filter-before | Filter snapshots before the given date/time | Not set | "2024-01-01" | --filter-before |
171+
| filter-after | Filter snapshots after the given date/time | Not set | "2023-01-01 11:15:23" | --filter-after |
172+
| filter-size | Filter snapshots for a total size in the size range. | Not set | "1MB..1GB" | --filter-size |
173+
| | If a single value is given, this is taken as lower bound. | | "500 k" | |
174+
| filter-size-added | Filter snapshots for a size added to the repository in the size range. | Not set | "1MB..1GB" | --filter-size-added |
175+
| | If a single value is given, this is taken as lower bound. | | "500 k" | |
176+
| filter-fn | Custom filter function for snapshots. | Not set | | --filter-fn |
177+
| filter-jq | Custom filter jq function for snapshots. Should return bool | Not set | ".summary.files_added > 1" | --filter-jq |
177178

178179
### Backup Options `[backup]`
179180

config/full.toml

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ filter-before = "2024-02-05 12:15" # Default: not set
9595
filter-size = "200MiB" # Default: not set
9696
filter-size-added = "1 MB..10MB" # Default: not set
9797
filter-fn = '|sn| {sn.host == "host1" || sn.description.contains("test")}' # Default: no filter function
98+
filter-jq = '.description | contains ("test")' # Default: no jq filter function
9899

99100
# Backup options: These options are used for all sources when calling the backup command.
100101
# They can be overwritten by source-specific options (see below) or command line options.
@@ -174,6 +175,7 @@ filter-before = "2024-02-05 12:15" # Default: not set
174175
filter-size = "200MiB" # Default: not set
175176
filter-size-added = "1 MB..10MB" # Default: not set
176177
filter-fn = '|sn| {sn.host == "host1" || sn.description.contains("test")}' # Default: no filter function
178+
filter-jq = '.description | contains ("test")' # Default: no jq filter function
177179
# The retention options follow. All of these are not set by default.
178180
keep-tags = ["tag1", "tag2,tag3"] # Default: not set
179181
keep-ids = [

src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Error types
22
33
use abscissa_core::error::{BoxError, Context};
4+
#[cfg(feature = "rhai")]
45
use rhai::EvalAltResult;
56
use std::{
67
fmt::{self, Display},
@@ -18,6 +19,7 @@ pub(crate) enum ErrorKind {
1819
}
1920

2021
/// Kinds of [`rhai`] errors
22+
#[cfg(feature = "rhai")]
2123
#[derive(Debug, Error)]
2224
pub(crate) enum RhaiErrorKinds {
2325
#[error(transparent)]

0 commit comments

Comments
 (0)