X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/f6a545b00a4046879b7cc25c06c37bb6b6880b43..b17907faf8d9693cef94a6048d802bd4ced9102f:/src/command/update.rs diff --git a/src/command/update.rs b/src/command/update.rs index 67cc462..54005f8 100644 --- a/src/command/update.rs +++ b/src/command/update.rs @@ -1,11 +1,11 @@ -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 super::{generate::Generate, sync_down::SyncDown, sync_up::SyncUp}; use crate::configuration::Configuration; use crate::constants::METADATA_FILENAME; use crate::metadata::Metadata; use serde_json; +use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, write}; +use std::io::{Error, ErrorKind, Result}; +use std::path::{Path, PathBuf}; pub struct Update; @@ -14,8 +14,9 @@ impl Update { Update } - fn copy_post(&self, source: &PathBuf, target: &PathBuf) -> Result<()> { - let post_name = source.file_name() + fn copy_post(source: &PathBuf, target: &Path) -> 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); @@ -23,13 +24,13 @@ impl Update { Ok(()) } - fn write_metadata(&self, metadata: &Metadata, metadata_location: &PathBuf) -> Result<()> { + fn write_metadata(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<()> { + fn archive(source: &PathBuf, target: &Path) -> Result<()> { let entries = read_dir(source)?; for entry in entries { let entry = entry?; @@ -39,7 +40,7 @@ impl Update { let entry_target = target.join(entry_name); if entry_type.is_dir() { - self.archive(&entry_source, &entry_target)?; + Update::archive(&entry_source, &entry_target)?; } else { copy(&entry_source, &entry_target)?; } @@ -54,11 +55,21 @@ impl super::Command for Update { vec![Box::new(SyncDown::new())] } - fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - let input = input.expect("You must provide a path to a post"); + fn execute( + &self, + input: Option<&String>, + configuration: &Configuration, + _: &str, + ) -> 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")); + return Err(Error::new( + ErrorKind::NotFound, + "The path provided does not exist", + )); } // Step 1. Write into the ephemeral posts @@ -72,8 +83,8 @@ impl super::Command for Update { 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)?; + Update::copy_post(&post_location, &first_post_path)?; + Update::write_metadata(&metadata, &metadata_file_path)?; // Step 2. Write into the archive @@ -83,15 +94,12 @@ impl super::Command for Update { let _ = remove_dir_all(&post_archive_path); create_dir_all(&post_archive_path)?; - self.archive(&first_post_path, &post_archive_path)?; - return Ok(()) + Update::archive(&first_post_path, &post_archive_path)?; + Ok(()) } fn after_dependencies(&self) -> Vec> { - vec![ - Box::new(Generate::new()), - Box::new(SyncUp::new()) - ] + vec![Box::new(Generate::new()), Box::new(SyncUp::new())] } fn command(&self) -> &'static str { @@ -102,4 +110,3 @@ impl super::Command for Update { "\t\tUpdates latest blog post" } } -