]> git.r.bdr.sh - rbdr/blog/blobdiff - src/command/update.rs
Add part of the implementation for add
[rbdr/blog] / src / command / update.rs
index 0a0211cbae485729691120850c88dca78c01a4f9..ed6b066556042f9325d60167d523a4fd49bcf81c 100644 (file)
@@ -1,5 +1,10 @@
-use std::io::Result;
+use std::fs::create_dir_all;
+use std::io::{Result, Error, ErrorKind};
+use std::path::PathBuf;
 use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp};
+use crate::configuration::Configuration;
+use crate::constants::METADATA_FILENAME;
+use crate::metadata::Metadata;
 
 pub struct Update;
 
@@ -7,6 +12,18 @@ impl Update {
     pub fn new() -> Self {
         Update
     }
+
+    fn copy_post(&self, post_location: &PathBuf) {
+        
+    }
+
+    fn write_metadata(&self, metadata: Metadata, metadata_location: &PathBuf) {
+        
+    }
+
+    fn archive(&self, post_location: &PathBuf) {
+        
+    }
 }
 
 impl super::Command for Update {
@@ -14,8 +31,23 @@ impl super::Command for Update {
         vec![Box::new(SyncDown::new())]
     }
 
-    fn execute(&self, input: Option<&String>) -> Result<()> {
-        println!("Update: {:?}!", input);
+    fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
+        let input = input.expect("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"));
+        }
+
+        create_dir_all(&configuration.posts_directory)?;
+
+        let metadata_file_path = configuration.posts_directory
+            .join("0")
+            .join(METADATA_FILENAME);
+        let metadata = Metadata::read_or_create(&metadata_file_path);
+
+        self.copy_post(&post_location);
+        self.write_metadata(metadata, &metadata_file_path);
+        self.archive(&post_location);
         return Ok(())
     }