Create files in .local on first run

This commit is contained in:
xqtc 2024-10-10 22:42:31 +02:00
parent 2010c906b1
commit 519f3d2165
3 changed files with 49 additions and 2 deletions

View file

@ -2,7 +2,7 @@ use chrono::{NaiveDateTime, Utc};
use color_eyre::Section;
use inquire;
use serde::{self, Deserialize, Serialize};
use std::collections::HashMap;
use std::{collections::HashMap, process::exit};
use strum::{EnumIter, IntoEnumIterator};
use uuid::Uuid;
@ -54,8 +54,15 @@ pub fn add_ingestion() {
} else {
std::fs::File::create(INGESTIONS_FILE.to_string()).unwrap();
ingesstions_bytes_loaded_des = HashMap::new();
let ingesstions_bytes_loaded_ser =
bincode::serialize(&ingesstions_bytes_loaded_des).unwrap();
std::fs::write(INGESTIONS_FILE.to_string(), ingesstions_bytes_loaded_ser).unwrap();
}
let substances = crate::substances::substances_to_vec();
if substances.is_empty() {
eprintln!("Add a substance before you log an ingestions");
exit(1)
}
let substance = inquire::Select::new("What did yout ingest?", substances)
.prompt()
.unwrap();
@ -139,3 +146,9 @@ pub fn list_ingestions() -> Result<(), std::io::Error> {
Ok(())
}
pub fn create_ingestions_file() -> Result<(), std::io::Error> {
let hash: HashMap<Uuid, Ingestion> = HashMap::new();
let hash_ser = bincode::serialize(&hash).unwrap();
std::fs::write(INGESTIONS_FILE.to_string(), hash_ser)
}

View file

@ -4,7 +4,7 @@ use std::{collections::HashMap, path::Path};
use bincode::serialize;
use chrono::Utc;
use clap::{Parser, Subcommand};
use config::LOCAL_PATH;
use config::{INGESTIONS_FILE, LOCAL_PATH, SUBSTANCES_FILE};
use git2;
use inquire;
use serde::{self, Deserialize, Serialize};
@ -64,6 +64,34 @@ fn main() {
}
}
}
if !substances::path_exists(SUBSTANCES_FILE.to_string()) {
match substances::create_substances_file() {
Ok(_) => {
println!(
"Created substances file at {:?}",
SUBSTANCES_FILE.to_string()
)
}
Err(_) => {
eprintln!("Could not create substances file");
panic!()
}
};
}
if !substances::path_exists(INGESTIONS_FILE.to_string()) {
match ingestions::create_ingestions_file() {
Ok(_) => {
println!(
"Created ingestions file at {:?}",
INGESTIONS_FILE.to_string()
)
}
Err(_) => {
eprintln!("Could not create substances file");
panic!()
}
};
}
let cli = Cli::parse();
match &cli.command {

View file

@ -106,3 +106,9 @@ pub fn substances_to_vec() -> Vec<String> {
}
sub_vec
}
pub fn create_substances_file() -> Result<(), std::io::Error> {
let hash: HashMap<Uuid, Substance> = HashMap::new();
let hash_ser = bincode::serialize(&hash).unwrap();
std::fs::write(SUBSTANCES_FILE.to_string(), hash_ser)
}