Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands)!: show-config now outputs toml #1095

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ simplelog = "0.12"

# commands
bytesize = "1"
cached = "0.49.3"
clap = { version = "4", features = ["derive", "env", "wrap_help"] }
clap_complete = "4"
convert_case = "0.6.0"
Expand All @@ -88,6 +89,7 @@ itertools = "0.12"
merge = "0.1"
once_cell = "1.19"
self_update = { version = "0.39", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] }
toml = "0.8"

[dev-dependencies]
abscissa_core = { version = "0.7.0", default-features = false, features = ["testing"] }
Expand Down
4 changes: 2 additions & 2 deletions src/commands/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::{bail, Context, Result};
use log::{debug, info, warn};
use merge::Merge;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use rustic_core::{
BackupOptions, ConfigOptions, KeyOptions, LocalSourceFilterOptions, LocalSourceSaveOptions,
ParentOptions, PathList, SnapshotOptions,
};

/// `backup` subcommand
#[derive(Clone, Command, Default, Debug, clap::Parser, Deserialize, Merge)]
#[derive(Clone, Command, Default, Debug, clap::Parser, Serialize, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
// Note: using cli_sources, sources and source within this struct is a hack to support serde(deny_unknown_fields)
// for deserializing the backup options from TOML
Expand Down
4 changes: 2 additions & 2 deletions src/commands/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::{bail, Result};
use log::{error, info};
use merge::Merge;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use rustic_core::{CopySnapshot, Id, KeyOptions};

Expand All @@ -31,7 +31,7 @@ pub(crate) struct CopyCmd {
}

/// Target repository options
#[derive(Default, Clone, Debug, Deserialize, Merge)]
#[derive(Default, Clone, Debug, Serialize, Deserialize, Merge)]
pub struct Targets {
/// Target repositories
#[merge(strategy = merge::vec::overwrite_empty)]
Expand Down
4 changes: 2 additions & 2 deletions src/commands/forget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use abscissa_core::{Command, FrameworkError, Runnable};
use anyhow::Result;

use merge::Merge;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

use crate::{commands::prune::PruneCmd, filtering::SnapshotFilter};
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Override<RusticConfig> for ForgetCmd {

/// Forget options
#[serde_as]
#[derive(Clone, Default, Debug, clap::Parser, Deserialize, Merge)]
#[derive(Clone, Default, Debug, clap::Parser, Serialize, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case")]
pub struct ForgetOptions {
/// Group snapshots by any combination of host,label,paths,tags (default: "host,label,paths")
Expand Down
20 changes: 16 additions & 4 deletions src/commands/show_config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
//! `show-config` subcommand

use crate::{Application, RUSTIC_APP};
use crate::{status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable};
use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use toml::to_string_pretty;

/// `show-config` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct ShowConfigCmd {}

impl Runnable for ShowConfigCmd {
fn run(&self) {
let config = RUSTIC_APP.config();
println!("{config:#?}");
if let Err(err) = self.inner_run() {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}

impl ShowConfigCmd {
fn inner_run(&self) -> Result<()> {
let config = to_string_pretty(RUSTIC_APP.config().as_ref())?;
println!("{config}");
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/commands/webdav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use abscissa_core::{config::Override, Command, FrameworkError, Runnable, Shutdow
use anyhow::{anyhow, Result};
use dav_server::{warp::dav_handler, DavHandler};
use merge::Merge;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use rustic_core::vfs::{FilePolicy, IdenticalSnapshot, Latest, Vfs};

#[derive(Clone, Command, Default, Debug, clap::Parser, Deserialize, Merge)]
#[derive(Clone, Command, Default, Debug, clap::Parser, Serialize, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct WebDavCmd {
/// Address to bind the webdav server to. [default: "localhost:8000"]
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{
///
/// # Example
// TODO: add example
#[derive(Clone, Default, Debug, Parser, Deserialize, Merge)]
#[derive(Clone, Default, Debug, Parser, Deserialize, Serialize, Merge)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct RusticConfig {
/// Global options
Expand Down Expand Up @@ -69,7 +69,7 @@ pub struct RusticConfig {
pub webdav: WebDavCmd,
}

#[derive(Clone, Default, Debug, Parser, Deserialize, Merge)]
#[derive(Clone, Default, Debug, Parser, Serialize, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case")]
pub struct AllRepositoryOptions {
/// Backend options
Expand Down
40 changes: 27 additions & 13 deletions src/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use log::warn;
use rustic_core::{repofile::SnapshotFile, StringList};
use std::{error::Error, str::FromStr};

use cached::proc_macro::cached;
use rhai::{serde::to_dynamic, Dynamic, Engine, FnPtr, AST};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

/// A function to filter snapshots
Expand All @@ -24,6 +25,17 @@ impl FromStr for SnapshotFn {
}
}

#[cached(key = "String", convert = r#"{ s.to_string() }"#, size = 1)]
fn string_to_fn(s: &str) -> Option<SnapshotFn> {
match SnapshotFn::from_str(s) {
Ok(filter_fn) => Some(filter_fn),
Err(err) => {
warn!("Error evaluating filter-fn {s}: {err}",);
None
}
}
}

impl SnapshotFn {
/// Call the function with a [`SnapshotFile`]
///
Expand All @@ -43,7 +55,7 @@ impl SnapshotFn {
}

#[serde_as]
#[derive(Clone, Default, Debug, Deserialize, merge::Merge, clap::Parser)]
#[derive(Clone, Default, Debug, Serialize, Deserialize, merge::Merge, clap::Parser)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct SnapshotFilter {
/// Hostname to filter (can be specified multiple times)
Expand Down Expand Up @@ -71,7 +83,7 @@ pub struct SnapshotFilter {
/// Function to filter snapshots
#[clap(long, global = true, value_name = "FUNC")]
#[serde_as(as = "Option<DisplayFromStr>")]
filter_fn: Option<SnapshotFn>,
filter_fn: Option<String>,
}

impl SnapshotFilter {
Expand All @@ -87,17 +99,19 @@ impl SnapshotFilter {
#[must_use]
pub fn matches(&self, snapshot: &SnapshotFile) -> bool {
if let Some(filter_fn) = &self.filter_fn {
match filter_fn.call::<bool>(snapshot) {
Ok(result) => {
if !result {
return false;
if let Some(func) = string_to_fn(filter_fn) {
match func.call::<bool>(snapshot) {
Ok(result) => {
if !result {
return false;
}
}
Err(err) => {
warn!(
"Error evaluating filter-fn for snapshot {}: {err}",
snapshot.id
);
}
}
Err(err) => {
warn!(
"Error evaluating filter-fn for snapshot {}: {err}",
snapshot.id
);
}
}
}
Expand Down
80 changes: 80 additions & 0 deletions tests/show-config-fixtures/empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[global]
use-profile = []
dry-run = false
no-progress = false

[global.env]

[repository]
no-cache = false
warm-up = false

[repository.options]

[repository.options-hot]

[repository.options-cold]

[snapshot-filter]
filter-host = []
filter-label = []
filter-paths = []
filter-tags = []

[backup]
stdin-filename = ""
with-atime = false
ignore-devid = false
no-scan = false
json = false
quiet = false
init = false
skip-identical-parent = false
force = false
ignore-ctime = false
ignore-inode = false
glob = []
iglob = []
glob-file = []
iglob-file = []
git-ignore = false
no-require-git = false
custom-ignorefile = []
exclude-if-present = []
one-file-system = false
tag = []
delete-never = false
sources = []
source = ""

[copy]
targets = []

[forget]
prune = false
filter-host = []
filter-label = []
filter-paths = []
filter-tags = []
keep-tags = []
keep-ids = []
keep-last = 0
keep-hourly = 0
keep-daily = 0
keep-weekly = 0
keep-monthly = 0
keep-quarter-yearly = 0
keep-half-yearly = 0
keep-yearly = 0
keep-within = "0s"
keep-within-hourly = "0s"
keep-within-daily = "0s"
keep-within-weekly = "0s"
keep-within-monthly = "0s"
keep-within-quarter-yearly = "0s"
keep-within-half-yearly = "0s"
keep-within-yearly = "0s"

[webdav]
symlinks = false

Loading
Loading