diff --git a/Cargo.lock b/Cargo.lock index 29929b5..35c914a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,9 +257,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.150" +version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" [[package]] name = "libredox" @@ -345,9 +345,9 @@ dependencies = [ [[package]] name = "openssl-src" -version = "300.2.0+3.2.0" +version = "300.2.1+3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1ebed1d188c4cd64c2bcd73d6c1fe1092f3d98c111831923cc1b706c3859fca" +checksum = "3fe476c29791a5ca0d1273c697e96085bbabbbea2ef7afd5617e78a4b40332d3" dependencies = [ "cc", ] @@ -469,9 +469,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "2.0.39" +version = "2.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" dependencies = [ "proc-macro2", "quote", @@ -480,18 +480,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.50" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.50" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df" dependencies = [ "proc-macro2", "quote", @@ -671,18 +671,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.30" +version = "0.7.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "306dca4455518f1f31635ec308b6b3e4eb1b11758cefafc782827d0aa7acb5c7" +checksum = "1c4061bedbb353041c12f413700357bec76df2c7e2ca8e4df8bac24c6bf68e3d" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.30" +version = "0.7.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be912bf68235a88fbefd1b73415cb218405958d1655b2ece9035a19920bdf6ba" +checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 2836162..15ab816 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,5 @@ anyhow = "1" clap = { version = "4", features = ["string", "env"] } config = { version = "0.13.4", default-features = false, features = ["yaml"] } dirs = "5" -openssl = { version = "0.10", features = ["vendored"] } +openssl = { version = "0.10", optional = true, features = ["vendored"] } rusqlite = { version = "0.30.0", features = ["bundled"] } diff --git a/src/cli/actions/mod.rs b/src/cli/actions/mod.rs index 7a0d0ac..24a288d 100644 --- a/src/cli/actions/mod.rs +++ b/src/cli/actions/mod.rs @@ -9,5 +9,6 @@ pub enum Action { directory: Option>, file: Option>, exclude: Option>, + config: PathBuf, }, } diff --git a/src/cli/actions/new.rs b/src/cli/actions/new.rs index 7afb701..1a7470d 100644 --- a/src/cli/actions/new.rs +++ b/src/cli/actions/new.rs @@ -1,18 +1,71 @@ use crate::cli::actions::Action; use anyhow::Result; +use rusqlite::Connection; +use std::fs; +use std::path::PathBuf; /// Handle the create action pub fn handle(action: Action) -> Result<()> { match action { Action::New { name, - directory: _, - file: _, - exclude: _, + config, + directory, + file, + exclude, } => { - println!("Creating new project: {}", name); + let db_path = config.join(format!("{}.db", name)); + + create_db_tables(db_path)?; + + if let Some(directory) = directory { + for dir in directory { + println!("Directory: {}", fs::canonicalize(dir)?.display()); + } + } + + if let Some(file) = file { + for file in file { + println!("File: {}", fs::canonicalize(file)?.display()); + } + } + + if let Some(exclude) = exclude { + for exclude in exclude { + println!("Exclude: {}", exclude); + } + } } } Ok(()) } + +fn create_db_tables(db_path: PathBuf) -> Result<()> { + let conn = Connection::open(db_path)?; + + // create the tables + conn.execute( + "CREATE TABLE IF NOT EXISTS Directory ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + parent_id INTEGER, + FOREIGN KEY (parent_id) REFERENCES Directory (id) +)", + [], + )?; + + conn.execute( + "CREATE TABLE IF NOT EXISTS File ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + size INTEGER NOT NULL, + directory_id INTEGER, + hash TEXT NOT NULL, + FOREIGN KEY (directory_id) REFERENCES Directory (id) +)", + [], + )?; + + Ok(()) +} diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs index 5c612c4..76689ea 100644 --- a/src/cli/commands/mod.rs +++ b/src/cli/commands/mod.rs @@ -25,7 +25,8 @@ pub fn new(config_path: PathBuf) -> Command { .short('c') .long("config") .help("Path to the configuration files") - .default_value(config_path.into_os_string()), + .default_value(config_path.into_os_string()) + .global(true), ) .subcommand(new::command()) .subcommand(edit::command()) @@ -34,10 +35,11 @@ pub fn new(config_path: PathBuf) -> Command { #[cfg(test)] mod tests { use super::*; + use std::path::PathBuf; #[test] fn test_new() { - let command = new(Path::new(".")); + let command = new(PathBuf::from(".")); assert_eq!(command.get_name(), "backup"); assert_eq!( diff --git a/src/cli/dispatch/new.rs b/src/cli/dispatch/new.rs index 4e6d5be..242b039 100644 --- a/src/cli/dispatch/new.rs +++ b/src/cli/dispatch/new.rs @@ -9,6 +9,12 @@ pub fn dispatch(matches: &ArgMatches) -> Result { .get_one("name") .map(|s: &String| s.to_string()) .ok_or_else(|| anyhow::anyhow!("Name required"))?, + + config: matches + .get_one::("config") + .map(PathBuf::from) + .ok_or_else(|| anyhow::anyhow!("Config required"))?, + directory: Some( matches .get_many::("directory") @@ -16,6 +22,7 @@ pub fn dispatch(matches: &ArgMatches) -> Result { .map(|v| v.to_path_buf()) .collect::>(), ), + file: Some( matches .get_many::("file") @@ -23,6 +30,7 @@ pub fn dispatch(matches: &ArgMatches) -> Result { .map(|v| v.to_path_buf()) .collect::>(), ), + exclude: Some( matches .get_many::("exclude")