diff --git a/src/main.rs b/src/main.rs index 4c78f58..72494b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,25 +16,16 @@ struct Config{ const APP_NAME: &str = "tiller"; fn main() -> io::Result<()> { - let config_path = dirs::home_dir() - .expect("failed to find home dir") - .join(format!(".config/{}/config.json",APP_NAME)) - ; - let config_data = read_to_string(config_path)?; - let config:Config = from_str(&config_data).expect("Invalid config format"); + let config = config()?; let Config { editor, til_folder, repo_path } = config; let full_til_path = format!("{}/{}",repo_path,til_folder); - let temp_file = NamedTempFile::new()?; - let temp_path = temp_file.path().to_str().unwrap().to_string(); - - Command::new(editor) - .arg(&temp_path) - .status() - .expect("Failed to open editor"); - - let input_content = read_to_string(temp_path)?; + let input_content = get_content(editor)?; + if input_content.len() < 1 { + println!("You can't save an empty til!"); + std::process::exit(1) + } let new_file_name = get_next_file_name(&full_til_path)?; @@ -50,6 +41,31 @@ fn main() -> io::Result<()> { let mut new_file = File::create(&new_file_path)?; write!(new_file,"{} \n {}", prepend_content, input_content)?; + publish(&new_file_path,&repo_path,&new_file_name) +} + +fn get_next_file_name(folder: &str) -> io::Result { + let paths = fs::read_dir(folder)? + .filter_map(Result::ok) + .filter(|e| e.path().extension().and_then(|s| s.to_str())==Some("md")) + .collect::>(); + let max_num = paths.iter() + .filter_map(|e| e.path().file_stem().and_then(|s| s.to_str()).and_then(|s| s.parse::().ok())) + .max() + .unwrap_or(0); + Ok(format!("{:02}.md",max_num + 1)) +} + +fn update_prepend_content(template: &str, filename: &str) -> io::Result { + let date = Local::now().format("%Y-%m-%dT%H:%M:%S").to_string(); + let filename_wo_ext = filename.replace(".md",""); + let content = template + .replace("$TITLE",&filename_wo_ext) + .replace("$DATE",&date); + Ok(content) +} + +fn publish(new_file_path: &str,repo_path: &str,new_file_name: &str) -> io::Result<()>{ Command::new("git") .args(&["add", &new_file_path]) .current_dir(&repo_path) @@ -67,28 +83,28 @@ fn main() -> io::Result<()> { .current_dir(&repo_path) .status() .expect("Failed to push"); - Ok(()) - } +fn config()->io::Result{ -fn get_next_file_name(folder: &str) -> io::Result { - let paths = fs::read_dir(folder)? - .filter_map(Result::ok) - .filter(|e| e.path().extension().and_then(|s| s.to_str())==Some("md")) - .collect::>(); - let max_num = paths.iter() - .filter_map(|e| e.path().file_stem().and_then(|s| s.to_str()).and_then(|s| s.parse::().ok())) - .max() - .unwrap_or(0); - Ok(format!("{:02}.md",max_num + 1)) + let config_path = dirs::home_dir() + .expect("failed to find home dir") + .join(format!(".config/{}/config.json",APP_NAME)) + ; + let config_data = read_to_string(config_path)?; + let config:Config = from_str(&config_data).expect("Invalid config format"); + + Ok(config) } -fn update_prepend_content(template: &str, filename: &str) -> io::Result { - let date = Local::now().format("%Y-%m-%dT%H:%M:%S").to_string(); - let filename_wo_ext = filename.replace(".md",""); - let content = template - .replace("$TITLE",&filename_wo_ext) - .replace("$DATE",&date); - Ok(content) +fn get_content(editor: String) -> io::Result{ + let temp_file = NamedTempFile::new()?; + let temp_path = temp_file.path().to_str().unwrap().to_string(); + + Command::new(editor) + .arg(&temp_path) + .status() + .expect("Failed to open editor"); + + read_to_string(temp_path) }