| @@ -1,6 +1,5 @@ |
| 1 | use failure::{Error, ResultExt}; | | |
| 2 | use std::{ | 1 | use std::{ |
| 3 | fs::{create_dir, OpenOptions}, | 2 | fs::{self, OpenOptions}, |
| 4 | io::Write, | 3 | io::Write, |
| 5 | path::Path, | 4 | path::Path, |
| 6 | path::PathBuf, | 5 | path::PathBuf, |
| @@ -10,7 +9,18 @@ quick_error! { · 3 unmodified lines |
| 10 | #[derive(Debug)] | 9 | #[derive(Debug)] |
| 11 | pub enum InitError { | 10 | pub enum InitError { |
| 12 | IoOpen(err: std::io::Error, path: PathBuf) { | 11 | IoOpen(err: std::io::Error, path: PathBuf) { |
| 13 | display("Could not open file at {:#?}", path.display()) | 12 | display("Could not open file at '{}'", path.display()) |
| | 13 | cause(err) |
| | 14 | } |
| | 15 | IoWrite(err: std::io::Error, path: PathBuf) { |
| | 16 | display("Could not write file at '{}'", path.display()) |
| | 17 | cause(err) |
| | 18 | } |
| | 19 | DirectoryExists(path: PathBuf) { |
| | 20 | display("Refusing to initialize the existing '{}' directory", path.display()) |
| | 21 | } |
| | 22 | CreateDirectory(err: std::io::Error, path: PathBuf) { |
| | 23 | display("Cold not create directory at '{}'", path.display()) |
| 14 | cause(err) | 24 | cause(err) |
| 15 | } | 25 | } |
| 16 | } | 26 | } |
| @@ -59,7 +69,7 @@ impl<'a> PathCursor<'a> { · 42 unmodified lines |
| 59 | } | 69 | } |
| 60 | | 70 | |
| 61 | impl<'a> NewDir<'a> { | 71 | impl<'a> NewDir<'a> { |
| 62 | fn at(self, component: &str) -> Result<Self, Error> { | 72 | fn at(self, component: &str) -> Result<Self, InitError> { |
| 63 | self.0.push(component); | 73 | self.0.push(component); |
| 64 | create_dir(&self.0)?; | 74 | create_dir(&self.0)?; |
| 65 | Ok(self) | 75 | Ok(self) |
| @@ -81,24 +91,25 @@ impl<'a> Drop for PathCursor<'a> { · 15 unmodified lines |
| 81 | } | 91 | } |
| 82 | } | 92 | } |
| 83 | | 93 | |
| 84 | fn write_file(data: &[u8], path: &Path) -> Result<(), Error> { | 94 | fn write_file(data: &[u8], path: &Path) -> Result<(), InitError> { |
| 85 | let mut file = OpenOptions::new() | 95 | let mut file = OpenOptions::new() |
| 86 | .write(true) | 96 | .write(true) |
| 87 | .create(true) | 97 | .create(true) |
| 88 | .append(false) | 98 | .append(false) |
| 89 | .open(path)?; | 99 | .open(path) |
| | 100 | .map_err(|e| InitError::IoOpen(e, path.to_owned()))?; |
| 90 | file.write_all(data) | 101 | file.write_all(data) |
| 91 | .with_context(|_| format!("Could not initialize file at '{}'", path.display())) | 102 | .map_err(|e| InitError::IoWrite(e, path.to_owned())) |
| 92 | .map_err(Into::into) | 103 | } |
| | 104 | |
| | 105 | fn create_dir(p: &Path) -> Result<(), InitError> { |
| | 106 | fs::create_dir(p).map_err(|e| InitError::CreateDirectory(e, p.to_owned())) |
| 93 | } | 107 | } |
| 94 | | 108 | |
| 95 | pub fn init() -> Result<(), Error> { | 109 | pub fn init() -> Result<(), InitError> { |
| 96 | let mut cursor = PathBuf::from(GIT_DIR_NAME); | 110 | let mut cursor = PathBuf::from(GIT_DIR_NAME); |
| 97 | if cursor.is_dir() { | 111 | if cursor.is_dir() { |
| 98 | bail!( | 112 | return Err(InitError::DirectoryExists(cursor)); |
| 99 | "Refusing to initialize the existing '{}' directory", | | |
| 100 | cursor.display() | | |
| 101 | ) | | |
| 102 | } | 113 | } |
| 103 | create_dir(&cursor)?; | 114 | create_dir(&cursor)?; |
| 104 | | 115 | |