]> git.r.bdr.sh - rbdr/blog/blame - src/command/add.rs
Allow adding / updating
[rbdr/blog] / src / command / add.rs
CommitLineData
606f82c7
RBR
1use std::fs::{create_dir_all, rename};
2use std::io::Result;
5f81d796
RBR
3use super::{
4 generate::Generate,
5 sync_down::SyncDown,
6 sync_up::SyncUp,
7 update::Update
8};
a9c6be41 9use crate::configuration::Configuration;
d620665f
RBR
10
11pub struct Add;
12
13impl Add {
14 pub fn new() -> Self {
15 Add
16 }
17}
18
19impl super::Command for Add {
20 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
5f81d796 21 vec![Box::new(SyncDown::new())]
d620665f
RBR
22 }
23
a9c6be41 24 fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
606f82c7
RBR
25 create_dir_all(&configuration.posts_directory)?;
26 for i in (0..configuration.max_posts).rev() {
27 let source = configuration.posts_directory.join(i.to_string());
28 let target = configuration.posts_directory.join((i + 1).to_string());
29
30 if source.exists() {
31 rename(&source, &target)?;
32 }
a9c6be41 33 }
606f82c7 34 Ok(())
d620665f
RBR
35 }
36
37 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
5f81d796
RBR
38 vec![
39 Box::new(Update::new()),
40 Box::new(Generate::new()),
41 Box::new(SyncUp::new())
42 ]
d620665f
RBR
43 }
44
45 fn command(&self) -> &'static str {
46 "add"
47 }
48
49 fn help(&self) -> &'static str {
2f579cf4 50 "<path_to_post>\t\t\tCreates new blog post"
d620665f
RBR
51 }
52}