From 54c6503e093b15a8224d51d4b5b20c9d9c2264ee Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Wed, 4 Sep 2024 09:49:39 +0000 Subject: [PATCH] basic add --- src/main.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 73f9fc2..1c0eba5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use clap::{Parser, Subcommand}; use std::fs; -use std::io; +use std::io::{self, Write}; use std::path::Path; @@ -9,7 +9,6 @@ const STORAGE_FOLDER: &str = "storage"; const STORAGE_PATH: &str = "storage/db.mps"; // TODO: concat from STORAGE_FOLDER - #[derive(Parser)] #[command(name = "mps", version = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] struct Cli { @@ -59,6 +58,21 @@ fn init() -> io::Result<()> { Ok(()) } +fn add() -> io::Result<()> { + let mut file = match fs::OpenOptions::new() + .write(true) + .open(STORAGE_PATH) + { + Ok(file) => file, + Err(_) => { + return Err(io::Error::new(io::ErrorKind::Other, "can`t open database")); // TODO: better error handling + } + }; + + writeln!(file, "new item")?; + Ok(()) +} + fn main() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { @@ -67,7 +81,7 @@ fn main() -> io::Result<()> { println!("Initialization complete"); } Some(Commands::Add{..}) => { - println!("about to add new item"); + add()?; } None => { if !is_inited() {