]> git.r.bdr.sh - rbdr/blog/blob - src/command/update.rs
ed6b066556042f9325d60167d523a4fd49bcf81c
[rbdr/blog] / src / command / update.rs
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;
8
9 pub struct Update;
10
11 impl Update {
12 pub fn new() -> Self {
13 Update
14 }
15
16 fn copy_post(&self, post_location: &PathBuf) {
17
18 }
19
20 fn write_metadata(&self, metadata: Metadata, metadata_location: &PathBuf) {
21
22 }
23
24 fn archive(&self, post_location: &PathBuf) {
25
26 }
27 }
28
29 impl super::Command for Update {
30 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
31 vec![Box::new(SyncDown::new())]
32 }
33
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"));
39 }
40
41 create_dir_all(&configuration.posts_directory)?;
42
43 let metadata_file_path = configuration.posts_directory
44 .join("0")
45 .join(METADATA_FILENAME);
46 let metadata = Metadata::read_or_create(&metadata_file_path);
47
48 self.copy_post(&post_location);
49 self.write_metadata(metadata, &metadata_file_path);
50 self.archive(&post_location);
51 return Ok(())
52 }
53
54 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
55 vec![
56 Box::new(Generate::new()),
57 Box::new(SyncUp::new())
58 ]
59 }
60
61 fn command(&self) -> &'static str {
62 "update"
63 }
64
65 fn help(&self) -> &'static str {
66 "<path_to_post>\t\tUpdates latest blog post"
67 }
68 }
69