taggo/src/main.rs

69 lines
1.3 KiB
Rust

use std::{path, process::exit};
use clap::Parser;
use cli::CliArgs;
use rusqlite::Connection;
const DB_PATH: &str = ".tags";
mod cli;
mod tags;
fn main() {
let args = CliArgs::parse();
match args.commands {
cli::Commands::Init => {
if !has_database() {
let conn = Connection::open(DB_PATH).unwrap();
init_db(&conn);
} else {
panic!("Database is already initialized in this folder");
}
exit(0);
}
cli::Commands::Tag(args) => tags::handle_tag(args),
cli::Commands::Tags(args) => tags::handle_tags(args),
cli::Commands::Files(args) => tags::handle_files(args),
}
}
fn init_db(conn: &Connection) {
conn.execute(
r#"CREATE TABLE IF NOT EXISTS tag(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255) NOT NULL UNIQUE
);"#,
(),
)
.unwrap();
conn.execute(
r#"CREATE TABLE IF NOT EXISTS file(
id INTEGER PRIMARY KEY AUTOINCREMENT,
path VARCHAR(255) NOT NULL UNIQUE
);"#,
(),
)
.unwrap();
conn.execute(
r#"CREATE TABLE IF NOT EXISTS file_tag(
tag_id INT REFERENCES tag(id),
file_id INT REFERENCES file(id)
);"#,
(),
)
.unwrap();
}
pub fn try_database() {
if !has_database() {
panic!("No database found! Use taggo init to create one first.");
}
}
pub fn has_database() -> bool {
path::Path::new(DB_PATH).exists()
}