Skip to content

Commit

Permalink
db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Dec 16, 2023
1 parent 466b0fd commit 2c85c99
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 21 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions src/cli/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub enum Action {
directory: Option<Vec<PathBuf>>,
file: Option<Vec<PathBuf>>,
exclude: Option<Vec<String>>,
config: PathBuf,
},
}
61 changes: 57 additions & 4 deletions src/cli/actions/new.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
6 changes: 4 additions & 2 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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!(
Expand Down
8 changes: 8 additions & 0 deletions src/cli/dispatch/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ pub fn dispatch(matches: &ArgMatches) -> Result<Action> {
.get_one("name")
.map(|s: &String| s.to_string())
.ok_or_else(|| anyhow::anyhow!("Name required"))?,

config: matches
.get_one::<String>("config")
.map(PathBuf::from)
.ok_or_else(|| anyhow::anyhow!("Config required"))?,

directory: Some(
matches
.get_many::<PathBuf>("directory")
.unwrap_or_default()
.map(|v| v.to_path_buf())
.collect::<Vec<_>>(),
),

file: Some(
matches
.get_many::<PathBuf>("file")
.unwrap_or_default()
.map(|v| v.to_path_buf())
.collect::<Vec<_>>(),
),

exclude: Some(
matches
.get_many::<String>("exclude")
Expand Down

0 comments on commit 2c85c99

Please sign in to comment.