]> git.r.bdr.sh - rbdr/blog/blobdiff - src/command/update.rs
Deal with all lints
[rbdr/blog] / src / command / update.rs
index d25109365a189a4a8bdaeba571f099dd38903276..54005f82dcc47b0313e238ba74dab0ac0ccdc4ce 100644 (file)
@@ -1,4 +1,11 @@
-use std::io::Result;
+use super::{generate::Generate, sync_down::SyncDown, sync_up::SyncUp};
+use crate::configuration::Configuration;
+use crate::constants::METADATA_FILENAME;
+use crate::metadata::Metadata;
+use serde_json;
+use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, write};
+use std::io::{Error, ErrorKind, Result};
+use std::path::{Path, PathBuf};
 
 pub struct Update;
 
 
 pub struct Update;
 
@@ -6,20 +13,93 @@ impl Update {
     pub fn new() -> Self {
         Update
     }
     pub fn new() -> Self {
         Update
     }
+
+    fn copy_post(source: &PathBuf, target: &Path) -> Result<()> {
+        let post_name = source
+            .file_name()
+            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Could not get post filename."))?;
+
+        let target_post = target.join(post_name);
+        copy(source, target_post)?;
+        Ok(())
+    }
+
+    fn write_metadata(metadata: &Metadata, metadata_location: &PathBuf) -> Result<()> {
+        let serialized_metadata = serde_json::to_string(&metadata)?;
+        write(metadata_location, serialized_metadata)?;
+        Ok(())
+    }
+
+    fn archive(source: &PathBuf, target: &Path) -> Result<()> {
+        let entries = read_dir(source)?;
+        for entry in entries {
+            let entry = entry?;
+            let entry_type = entry.file_type()?;
+            let entry_name = entry.file_name();
+            let entry_source = entry.path();
+            let entry_target = target.join(entry_name);
+
+            if entry_type.is_dir() {
+                Update::archive(&entry_source, &entry_target)?;
+            } else {
+                copy(&entry_source, &entry_target)?;
+            }
+        }
+
+        Ok(())
+    }
 }
 
 impl super::Command for Update {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
 }
 
 impl super::Command for Update {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![Box::new(SyncDown::new())]
     }
 
     }
 
-    fn execute(&self, input: Option<&String>) -> Result<()> {
-        println!("Update: {:?}!", input);
-        return Ok(())
+    fn execute(
+        &self,
+        input: Option<&String>,
+        configuration: &Configuration,
+        _: &str,
+    ) -> Result<()> {
+        let input = input.ok_or_else(|| {
+            Error::new(ErrorKind::InvalidInput, "You must provide a path to a post")
+        })?;
+        let post_location = PathBuf::from(input);
+        if !post_location.exists() {
+            return Err(Error::new(
+                ErrorKind::NotFound,
+                "The path provided does not exist",
+            ));
+        }
+
+        // Step 1. Write into the ephemeral posts
+
+        create_dir_all(&configuration.posts_directory)?;
+
+        let first_post_path = configuration.posts_directory.join("0");
+        let metadata_file_path = first_post_path.join(METADATA_FILENAME);
+        let metadata = Metadata::read_or_create(&metadata_file_path);
+
+        let _ = remove_dir_all(&first_post_path);
+        create_dir_all(&first_post_path)?;
+
+        Update::copy_post(&post_location, &first_post_path)?;
+        Update::write_metadata(&metadata, &metadata_file_path)?;
+
+        // Step 2. Write into the archive
+
+        create_dir_all(&configuration.archive_directory)?;
+
+        let post_archive_path = configuration.archive_directory.join(metadata.id);
+        let _ = remove_dir_all(&post_archive_path);
+        create_dir_all(&post_archive_path)?;
+
+        Update::archive(&first_post_path, &post_archive_path)?;
+        Ok(())
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![Box::new(Generate::new()), Box::new(SyncUp::new())]
     }
 
     fn command(&self) -> &'static str {
     }
 
     fn command(&self) -> &'static str {
@@ -30,4 +110,3 @@ impl super::Command for Update {
         "<path_to_post>\t\tUpdates latest blog post"
     }
 }
         "<path_to_post>\t\tUpdates latest blog post"
     }
 }
-