]>
Commit | Line | Data |
---|---|---|
606f82c7 | 1 | use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, write}; |
a9c6be41 RBR |
2 | use std::io::{Result, Error, ErrorKind}; |
3 | use std::path::PathBuf; | |
5f81d796 | 4 | use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp}; |
a9c6be41 RBR |
5 | use crate::configuration::Configuration; |
6 | use crate::constants::METADATA_FILENAME; | |
7 | use crate::metadata::Metadata; | |
f6a545b0 | 8 | use serde_json; |
d620665f RBR |
9 | |
10 | pub struct Update; | |
11 | ||
12 | impl Update { | |
13 | pub fn new() -> Self { | |
14 | Update | |
15 | } | |
a9c6be41 | 16 | |
606f82c7 RBR |
17 | fn copy_post(&self, source: &PathBuf, target: &PathBuf) -> Result<()> { |
18 | let post_name = source.file_name() | |
19 | .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Could not get post filename."))?; | |
20 | ||
21 | let target_post = target.join(post_name); | |
22 | copy(source, target_post)?; | |
23 | Ok(()) | |
a9c6be41 RBR |
24 | } |
25 | ||
606f82c7 | 26 | fn write_metadata(&self, metadata: &Metadata, metadata_location: &PathBuf) -> Result<()> { |
f6a545b0 | 27 | let serialized_metadata = serde_json::to_string(&metadata)?; |
606f82c7 RBR |
28 | write(metadata_location, serialized_metadata)?; |
29 | Ok(()) | |
a9c6be41 RBR |
30 | } |
31 | ||
606f82c7 RBR |
32 | fn archive(&self, source: &PathBuf, target: &PathBuf) -> Result<()> { |
33 | let entries = read_dir(source)?; | |
34 | for entry in entries { | |
35 | let entry = entry?; | |
36 | let entry_type = entry.file_type()?; | |
37 | let entry_name = entry.file_name(); | |
38 | let entry_source = entry.path(); | |
39 | let entry_target = target.join(entry_name); | |
40 | ||
41 | if entry_type.is_dir() { | |
42 | self.archive(&entry_source, &entry_target)?; | |
43 | } else { | |
44 | copy(&entry_source, &entry_target)?; | |
45 | } | |
46 | } | |
47 | ||
48 | Ok(()) | |
a9c6be41 | 49 | } |
d620665f RBR |
50 | } |
51 | ||
52 | impl super::Command for Update { | |
53 | fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> { | |
5f81d796 | 54 | vec![Box::new(SyncDown::new())] |
d620665f RBR |
55 | } |
56 | ||
a9c6be41 | 57 | fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { |
50f53dc4 RBR |
58 | let input = input |
59 | .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "You must provide a path to a post"))?; | |
a9c6be41 RBR |
60 | let post_location = PathBuf::from(input); |
61 | if !post_location.exists() { | |
62 | return Err(Error::new(ErrorKind::NotFound, "The path provided does not exist")); | |
63 | } | |
64 | ||
606f82c7 RBR |
65 | // Step 1. Write into the ephemeral posts |
66 | ||
a9c6be41 RBR |
67 | create_dir_all(&configuration.posts_directory)?; |
68 | ||
606f82c7 RBR |
69 | let first_post_path = configuration.posts_directory.join("0"); |
70 | let metadata_file_path = first_post_path.join(METADATA_FILENAME); | |
a9c6be41 RBR |
71 | let metadata = Metadata::read_or_create(&metadata_file_path); |
72 | ||
606f82c7 RBR |
73 | let _ = remove_dir_all(&first_post_path); |
74 | create_dir_all(&first_post_path)?; | |
75 | ||
76 | self.copy_post(&post_location, &first_post_path)?; | |
77 | self.write_metadata(&metadata, &metadata_file_path)?; | |
78 | ||
79 | // Step 2. Write into the archive | |
80 | ||
81 | create_dir_all(&configuration.archive_directory)?; | |
82 | ||
83 | let post_archive_path = configuration.archive_directory.join(metadata.id); | |
84 | let _ = remove_dir_all(&post_archive_path); | |
85 | create_dir_all(&post_archive_path)?; | |
86 | ||
87 | self.archive(&first_post_path, &post_archive_path)?; | |
d620665f RBR |
88 | return Ok(()) |
89 | } | |
90 | ||
91 | fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> { | |
5f81d796 RBR |
92 | vec![ |
93 | Box::new(Generate::new()), | |
94 | Box::new(SyncUp::new()) | |
95 | ] | |
d620665f RBR |
96 | } |
97 | ||
98 | fn command(&self) -> &'static str { | |
99 | "update" | |
100 | } | |
101 | ||
102 | fn help(&self) -> &'static str { | |
103 | "<path_to_post>\t\tUpdates latest blog post" | |
104 | } | |
105 | } | |
106 |