Skip to content

Commit

Permalink
Make examples nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jul 14, 2023
1 parent 93dc579 commit 4295a12
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 137 deletions.
14 changes: 14 additions & 0 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 @@ -51,6 +51,7 @@ thiserror = { workspace = true }
# macros
derive_more = { workspace = true }
derivative = { workspace = true }
derive_setters = { workspace = true }

#logging
log = { workspace = true }
Expand Down Expand Up @@ -160,6 +161,7 @@ anyhow = "1"
# macros
derive_more = "0.99"
derivative = "2"
derive_setters = "0.1"

#logging
log = "0.4"
Expand Down
1 change: 1 addition & 0 deletions crates/rustic_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ thiserror = { workspace = true }
# macros
derive_more = { workspace = true }
derivative = { workspace = true }
derive_setters = { workspace = true }

#logging
log = { workspace = true }
Expand Down
28 changes: 10 additions & 18 deletions crates/rustic_core/examples/backup.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
//! `backup` example
use rustic_core::{BackupOpts, PathList, Repository, RepositoryOptions, SnapshotFile};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};

let repo = Repository::new(&repo_opts)
.unwrap()
.open()
.unwrap()
.to_indexed_ids()
.unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?.to_indexed_ids()?;

let backup_opts = BackupOpts::default();
let source = PathList::from_string(".", true).unwrap(); // true: sanitize the given string
let source = PathList::from_string(".", true)?; // true: sanitize the given string
let dry_run = false;

let snap = repo
.backup(&backup_opts, source, SnapshotFile::default(), dry_run)
.unwrap();
let snap = repo.backup(&backup_opts, source, SnapshotFile::default(), dry_run)?;

println!("successfully created snapshot:\n{snap:#?}")
println!("successfully created snapshot:\n{snap:#?}");
Ok(())
}
16 changes: 8 additions & 8 deletions crates/rustic_core/examples/check.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! `check` example
use rustic_core::{CheckOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?;

// Check respository with standard options
let opts = CheckOpts::default();
repo.check(opts).unwrap()
repo.check(opts)?;
Ok(())
}
17 changes: 8 additions & 9 deletions crates/rustic_core/examples/config.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
//! `config` example
use rustic_core::{ConfigOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};

let repo = Repository::new(&repo_opts).unwrap().open().unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?;

// Set Config, e.g. Compression level
let config_opts = ConfigOpts {
set_compression: Some(22),
..Default::default()
};
repo.apply_config(&config_opts).unwrap();
repo.apply_config(&config_opts)?;
Ok(())
}
20 changes: 7 additions & 13 deletions crates/rustic_core/examples/copy.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//! `copy` example
use std::error::Error;

use rustic_core::{CopySnapshot, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let src_repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let src_repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let src_repo = Repository::new(&src_repo_opts)?.open()?.to_indexed()?;

let dst_repo_opts = RepositoryOptions {
repository: Some("/tmp/repo2".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let dst_repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let dst_repo = Repository::new(&dst_repo_opts)?.open()?.to_indexed_ids()?;

// get snapshots which are missing in dst_repo
Expand All @@ -34,6 +29,5 @@ fn main() -> Result<(), Box<dyn Error>> {
.iter()
.filter_map(|CopySnapshot { relevant, sn }| relevant.then_some(sn)),
)?;

Ok(())
}
20 changes: 9 additions & 11 deletions crates/rustic_core/examples/forget.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//! `forget` example
use rustic_core::{KeepOptions, Repository, RepositoryOptions, SnapshotGroupCriterion};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?;

// Check respository with standard options
let group_by = SnapshotGroupCriterion::default();
let mut keep = KeepOptions::default();
keep.keep_daily = 5;
keep.keep_weekly = 10;
let snaps = repo
.get_forget_snapshots(&keep, group_by, |_| true)
.unwrap();
let snaps = repo.get_forget_snapshots(&keep, group_by, |_| true)?;
println!("{snaps:?}");
// to remove the snapshots-to-forget, uncomment this line:
// repo.delete_snapshots(&snaps.into_forget_ids()).unwrap()
// repo.delete_snapshots(&snaps.into_forget_ids())?
Ok(())
}
17 changes: 7 additions & 10 deletions crates/rustic_core/examples/init.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
//! `init` example
use rustic_core::{ConfigOpts, KeyOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Init repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let key_opts = KeyOpts::default();
let config_opts = ConfigOpts::default();
let _repo = Repository::new(&repo_opts)
.unwrap()
.init(&key_opts, &config_opts)
.unwrap();
let _repo = Repository::new(&repo_opts)?.init(&key_opts, &config_opts)?;

// -> use _repo for any operation on an open repository
Ok(())
}
17 changes: 8 additions & 9 deletions crates/rustic_core/examples/key.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
//! `key` example
use rustic_core::{KeyOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};

let repo = Repository::new(&repo_opts).unwrap().open().unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?;

// Add a new key with the given password
let key_opts = KeyOpts::default();
repo.add_key("new_password", &key_opts).unwrap();
repo.add_key("new_password", &key_opts)?;
Ok(())
}
26 changes: 10 additions & 16 deletions crates/rustic_core/examples/ls.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
//! `ls` example
use rustic_core::{Repository, RepositoryOptions, TreeStreamerOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};

let repo = Repository::new(&repo_opts)
.unwrap()
.open()
.unwrap()
.to_indexed()
.unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?.to_indexed()?;

// use latest snapshot without filtering snapshots
let node = repo.node_from_snapshot_path("latest", |_| true).unwrap();
let node = repo.node_from_snapshot_path("latest", |_| true)?;

// recursively list the snapshot contents using no additional filtering
let recursive = true;
let streamer_opts = TreeStreamerOptions::default();
for item in repo.ls(&node, &streamer_opts, recursive).unwrap() {
let (path, _) = item.unwrap();
for item in repo.ls(&node, &streamer_opts, recursive)? {
let (path, _) = item?;
println!("{path:?} ");
}
Ok(())
}
18 changes: 9 additions & 9 deletions crates/rustic_core/examples/prune.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
//! `prune` example
use rustic_core::{PruneOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();
let repo_opts = RepositoryOptions::default()
.repository("tmp/repo")
.password("test");
let repo = Repository::new(&repo_opts)?.open()?;

let prune_opts = PruneOpts::default();
let prune_plan = repo.prune_plan(&prune_opts).unwrap();
let prune_plan = repo.prune_plan(&prune_opts)?;
println!("{:?}", prune_plan.stats);
println!("to repack: {:?}", prune_plan.repack_packs());
// to run the plan uncomment this line:
// prune_plan.do_prune(&repo, &prune_opts).unwrap();
// prune_plan.do_prune(&repo, &prune_opts)?;
Ok(())
}
Loading

0 comments on commit 4295a12

Please sign in to comment.