aboutsummaryrefslogtreecommitdiff
path: root/src/command/update.rs
blob: 8a3d6de47e27e41d53d66b6b13344fc6ee2e5832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, write};
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;
use serde_json;

pub struct Update;

impl Update {
    pub fn new() -> Self {
        Update
    }

    fn copy_post(&self, source: &PathBuf, target: &PathBuf) -> Result<()> {
        let post_name = source.file_name()
            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Could not get post filename."))?;

        let target_post = target.join(post_name);
        copy(source, target_post)?;
        Ok(())
    }

    fn write_metadata(&self, metadata: &Metadata, metadata_location: &PathBuf) -> Result<()> {
        let serialized_metadata = serde_json::to_string(&metadata)?;
        write(metadata_location, serialized_metadata)?;
        Ok(())
    }

    fn archive(&self, source: &PathBuf, target: &PathBuf) -> Result<()> {
        let entries = read_dir(source)?;
        for entry in entries {
            let entry = entry?;
            let entry_type = entry.file_type()?;
            let entry_name = entry.file_name();
            let entry_source = entry.path();
            let entry_target = target.join(entry_name);

            if entry_type.is_dir() {
                self.archive(&entry_source, &entry_target)?;
            } else {
                copy(&entry_source, &entry_target)?;
            }
        }

        Ok(())
    }
}

impl super::Command for Update {
    fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![Box::new(SyncDown::new())]
    }

    fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
        let input = input
            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "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"));
        }

        // Step 1. Write into the ephemeral posts

        create_dir_all(&configuration.posts_directory)?;

        let first_post_path = configuration.posts_directory.join("0");
        let metadata_file_path = first_post_path.join(METADATA_FILENAME);
        let metadata = Metadata::read_or_create(&metadata_file_path);

        let _ = remove_dir_all(&first_post_path);
        create_dir_all(&first_post_path)?;

        self.copy_post(&post_location, &first_post_path)?;
        self.write_metadata(&metadata, &metadata_file_path)?;

        // Step 2. Write into the archive

        create_dir_all(&configuration.archive_directory)?;

        let post_archive_path = configuration.archive_directory.join(metadata.id);
        let _ = remove_dir_all(&post_archive_path);
        create_dir_all(&post_archive_path)?;

        self.archive(&first_post_path, &post_archive_path)?;
        return Ok(())
    }

    fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![
            Box::new(Generate::new()),
            Box::new(SyncUp::new())
        ]
    }

    fn command(&self) -> &'static str {
        "update"
    }

    fn help(&self) -> &'static str {
        "<path_to_post>\t\tUpdates latest blog post"
    }
}