]> git.r.bdr.sh - rbdr/blog/blame - src/command/add.rs
Use sequence format for deb
[rbdr/blog] / src / command / add.rs
CommitLineData
0e276d03 1use std::fs::{create_dir_all, remove_dir_all, rename};
606f82c7 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 25 create_dir_all(&configuration.posts_directory)?;
0e276d03 26 for i in (0..configuration.max_posts - 1).rev() {
606f82c7
RBR
27 let source = configuration.posts_directory.join(i.to_string());
28 let target = configuration.posts_directory.join((i + 1).to_string());
29
0e276d03
RBR
30 if target.exists() {
31 remove_dir_all(&target)?;
32 }
606f82c7
RBR
33 if source.exists() {
34 rename(&source, &target)?;
35 }
a9c6be41 36 }
606f82c7 37 Ok(())
d620665f
RBR
38 }
39
40 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
5f81d796
RBR
41 vec![
42 Box::new(Update::new()),
43 Box::new(Generate::new()),
44 Box::new(SyncUp::new())
45 ]
d620665f
RBR
46 }
47
48 fn command(&self) -> &'static str {
49 "add"
50 }
51
52 fn help(&self) -> &'static str {
2f579cf4 53 "<path_to_post>\t\t\tCreates new blog post"
d620665f
RBR
54 }
55}