1 use std::fs::create_dir_all;
2 use std::io::{Result, Error, ErrorKind};
3 use std::path::PathBuf;
4 use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp};
5 use crate::configuration::Configuration;
6 use crate::constants::METADATA_FILENAME;
7 use crate::metadata::Metadata;
12 pub fn new() -> Self {
16 fn copy_post(&self, post_location: &PathBuf) {
20 fn write_metadata(&self, metadata: Metadata, metadata_location: &PathBuf) {
24 fn archive(&self, post_location: &PathBuf) {
29 impl super::Command for Update {
30 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
31 vec![Box::new(SyncDown::new())]
34 fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
35 let input = input.expect("You must provide a path to a post");
36 let post_location = PathBuf::from(input);
37 if !post_location.exists() {
38 return Err(Error::new(ErrorKind::NotFound, "The path provided does not exist"));
41 create_dir_all(&configuration.posts_directory)?;
43 let metadata_file_path = configuration.posts_directory
45 .join(METADATA_FILENAME);
46 let metadata = Metadata::read_or_create(&metadata_file_path);
48 self.copy_post(&post_location);
49 self.write_metadata(metadata, &metadata_file_path);
50 self.archive(&post_location);
54 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
56 Box::new(Generate::new()),
57 Box::new(SyncUp::new())
61 fn command(&self) -> &'static str {
65 fn help(&self) -> &'static str {
66 "<path_to_post>\t\tUpdates latest blog post"