Browse Source

add id

db-refactoring
Coin de Gamma 4 months ago
parent
commit
10bd6772fa
  1. 15
      src/main.rs

15
src/main.rs

@ -25,7 +25,7 @@ enum Commands {
/// Adds new item with unique id to the db /// Adds new item with unique id to the db
Add { Add {
#[arg(value_name="item_id")] #[arg(value_name="item_id")]
input: String id: String
}, },
/// Lists all ids stored in db /// Lists all ids stored in db
@ -47,19 +47,21 @@ fn init() -> io::Result<()> {
fs::File::create(STORAGE_PATH)?; fs::File::create(STORAGE_PATH)?;
println!("Storage db created"); println!("Storage db created");
println!("Initialization complete. It's required to add folder `{}` under git manually.", STORAGE_FOLDER);
Ok(()) Ok(())
} }
fn add() -> io::Result<()> { fn add(id: &String) -> io::Result<()> {
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()
.write(true) .write(true)
.append(true)
.open(STORAGE_PATH)?; .open(STORAGE_PATH)?;
writeln!(file, "new item")?; writeln!(file, "{}", id)?;
Ok(()) Ok(())
} }
// TODO: make this ti return just set on values, will need later to check ids // TODO: make this to return just set on values, will need later to check ids
fn list() -> io:: Result<()> { fn list() -> io:: Result<()> {
let file = fs::File::open(STORAGE_PATH)?; let file = fs::File::open(STORAGE_PATH)?;
let reader = io::BufReader::new(file); let reader = io::BufReader::new(file);
@ -81,10 +83,9 @@ fn main() -> io::Result<()> {
match &cli.command { match &cli.command {
Some(Commands::Init) => { Some(Commands::Init) => {
init()?; init()?;
println!("Initialization complete");
} }
Some(Commands::Add{..}) => { Some(Commands::Add{ id }) => {
add()?; add(id)?;
} }
Some(Commands::List) => { Some(Commands::List) => {
list()?; list()?;

Loading…
Cancel
Save