]> git.r.bdr.sh - rbdr/blog/blame - src/command/update.rs
Add part of the implementation for add
[rbdr/blog] / src / command / update.rs
CommitLineData
a9c6be41
RBR
1use std::fs::create_dir_all;
2use std::io::{Result, Error, ErrorKind};
3use std::path::PathBuf;
5f81d796 4use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp};
a9c6be41
RBR
5use crate::configuration::Configuration;
6use crate::constants::METADATA_FILENAME;
7use crate::metadata::Metadata;
d620665f
RBR
8
9pub struct Update;
10
11impl Update {
12 pub fn new() -> Self {
13 Update
14 }
a9c6be41
RBR
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 }
d620665f
RBR
27}
28
29impl super::Command for Update {
30 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
5f81d796 31 vec![Box::new(SyncDown::new())]
d620665f
RBR
32 }
33
a9c6be41
RBR
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);
d620665f
RBR
51 return Ok(())
52 }
53
54 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
5f81d796
RBR
55 vec![
56 Box::new(Generate::new()),
57 Box::new(SyncUp::new())
58 ]
d620665f
RBR
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