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