Replace Clap by Structopt which seems to be nicer

This commit is contained in:
raute 2020-06-27 21:51:12 +02:00
parent 2179a13aa6
commit 547442b2b8
3 changed files with 139 additions and 23 deletions

119
Cargo.lock generated
View File

@ -40,8 +40,8 @@ name = "bekape"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"regex",
"structopt",
]
[[package]]
@ -76,6 +76,15 @@ dependencies = [
"vec_map",
]
[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.14"
@ -122,6 +131,50 @@ dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro-error"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc175e9777c3116627248584e8f8b3e2987405cabe1c0adf7d1dd28f09dc7880"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cc9795ca17eb581285ec44936da7fc2335a3f34f2ddd13118b6f4d515435c50"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn-mid",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.3.9"
@ -146,6 +199,52 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de2f5e239ee807089b62adce73e48c625e0ed80df02c7ab3f068f5db5281065c"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "510413f9de616762a4fbeab62509bf15c729603b72d7cd71280fbca431b1c118"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "syn-mid"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -174,18 +273,36 @@ dependencies = [
"winapi",
]
[[package]]
name = "unicode-segmentation"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -6,5 +6,5 @@ edition = "2018"
[dependencies]
chrono = "0.4"
clap = "2.33"
regex = "1.3"
structopt = "0.3"

View File

@ -1,8 +1,21 @@
use clap::{App, Arg};
use regex::Regex;
use std::fs;
use std::time::SystemTime;
use std::vec::Vec;
use structopt::StructOpt;
/// A simple backup program
#[derive(StructOpt, Debug)]
#[structopt(name = "Bekape")]
struct Opt {
/// The folder to backup
#[structopt(name = "source")]
source: String,
/// The folder to store the backup
#[structopt(name = "destination")]
destination: String,
}
fn browse_recursively(dir: &str) -> (Vec<String>, Vec<String>) {
let mut content = Vec::new();
@ -64,24 +77,10 @@ fn backup_element(path: &str, src_root: &str, bkp_root: &str, prev_bkp_root: &Op
}
fn main() {
let matches = App::new("bekape")
.about("Simple backup program.")
.arg(
Arg::with_name("source")
.help("The folder to backup")
.required(true),
)
.arg(
Arg::with_name("destination")
.help("The folder to store the backup")
.required(true),
)
.get_matches();
let bkp_src = matches.value_of("source").unwrap();
let bkp_dst = matches.value_of("destination").unwrap();
let opt = Opt::from_args();
let dt = chrono::offset::Utc::now().format("%Y-%m-%d_%H-%M-%S");
let backup_folder = format!("{}/backup_{}_wip", bkp_dst, dt);
let backup_folder = format!("{}/backup_{}_wip", opt.destination, dt);
if fs::create_dir(&backup_folder).is_err() {
eprintln!("Could not create backup directory: {}", &backup_folder);
std::process::exit(1);
@ -89,7 +88,7 @@ fn main() {
// find previous backup
let backup_regex = Regex::new(r"backup_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$").unwrap();
let ls = fs::read_dir(bkp_dst);
let ls = fs::read_dir(&opt.destination);
let prev_backup = if let Ok(ls) = ls {
let mut ls = ls
.filter_map(|s| s.ok())
@ -106,9 +105,9 @@ fn main() {
None
};
let result = browse_recursively(&bkp_src);
let result = browse_recursively(&opt.source);
let content = result.0.iter().map(|s| {
let l = bkp_src.len();
let l = opt.source.len();
s[l..].to_string()
});
@ -117,7 +116,7 @@ fn main() {
}
for elem in content {
backup_element(&elem, &bkp_src, &backup_folder, &prev_backup);
backup_element(&elem, &opt.source, &backup_folder, &prev_backup);
}
let new_name = backup_folder.replace("_wip", "");