Skip to content

Commit 38caae5

Browse files
committed
refactor: add config::backup module
1 parent 6fcb614 commit 38caae5

File tree

2 files changed

+73
-55
lines changed

2 files changed

+73
-55
lines changed

src/commands/run.rs

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
33
use crate::config::{self, BranchName, Config, PrNumber, PullRequest};
44
use anyhow::Result;
5-
use std::ffi::OsString;
6-
use std::fs::{self, File};
7-
use std::io::Write as _;
8-
use std::path::PathBuf;
5+
use std::fs;
96

107
use anyhow::{anyhow, bail};
118
use colored::Colorize as _;
@@ -14,14 +11,6 @@ use crate::github::{self, Branch, Remote, RemoteBranch};
1411
use crate::utils::{format_pr, format_url, with_uuid};
1512
use crate::{commands, confirm_prompt, git};
1613

17-
/// Backup for a file
18-
struct FileBackup {
19-
/// Name of the file to backup in `.patchy` config directory
20-
filename: OsString,
21-
/// Contents of the backed up file
22-
contents: String,
23-
}
24-
2514
/// Run patchy, if `yes` then there will be no prompt
2615
pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
2716
let root = config::ROOT.as_str();
@@ -72,35 +61,7 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
7261
);
7362
}
7463

75-
// --- Backup all files in the `.patchy` config directory
76-
77-
let config_files = fs::read_dir(&*config::PATH).map_err(|err| {
78-
anyhow!(
79-
"Failed to read files in directory `{}`:\n{err}",
80-
&config::PATH.display()
81-
)
82-
})?;
83-
84-
let mut backed_up_files = Vec::new();
85-
86-
for config_file in config_files.flatten() {
87-
let file_backup = fs::read_to_string(config_file.path())
88-
.map_err(|err| anyhow!("{err}"))
89-
.map(|contents| FileBackup {
90-
filename: config_file.file_name(),
91-
contents,
92-
})
93-
.map_err(|err| {
94-
anyhow!(
95-
"failed to backup patchy config file {} for configuration files:\n{err}",
96-
config_file.file_name().display()
97-
)
98-
})?;
99-
100-
backed_up_files.push(file_backup);
101-
}
102-
103-
// ---
64+
let backed_up_files = config::backup::backup()?;
10465

10566
let info = RemoteBranch {
10667
remote: Remote {
@@ -240,20 +201,7 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
240201
);
241202
}
242203

243-
// Restore all the backup files
244-
245-
for FileBackup {
246-
filename, contents, ..
247-
} in &backed_up_files
248-
{
249-
let path = git::ROOT.join(PathBuf::from(config::ROOT.as_str()).join(filename));
250-
let mut file =
251-
File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?;
252-
253-
write!(file, "{contents}")?;
254-
}
255-
256-
// apply patches if they exist
204+
config::backup::restore(&backed_up_files)?;
257205

258206
for patch in config.patches {
259207
let file_name = git::ROOT

src/config.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,76 @@ macro_rules! impl_deserialize_for {
306306

307307
impl_deserialize_for!(Remote Ref PullRequest Branch BranchName);
308308

309+
pub mod backup {
310+
//! Backup files in patchy's config directory
311+
312+
use crate::git;
313+
use std::io::Write as _;
314+
315+
use super::PATH;
316+
use anyhow::{Result, anyhow};
317+
use std::{
318+
ffi::OsString,
319+
fs::{self, File},
320+
path::PathBuf,
321+
};
322+
323+
/// Backup for a single file
324+
pub struct FileBackup {
325+
/// Name of the file to backup in `.patchy` config directory
326+
filename: OsString,
327+
/// Contents of the backed up file
328+
contents: String,
329+
}
330+
331+
/// Restore the backed up files
332+
pub fn restore(files: &[FileBackup]) -> Result<()> {
333+
for FileBackup {
334+
filename, contents, ..
335+
} in files
336+
{
337+
let path = git::ROOT.join(PathBuf::from(super::ROOT.as_str()).join(filename));
338+
let mut file =
339+
File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?;
340+
341+
write!(file, "{contents}")?;
342+
}
343+
344+
Ok(())
345+
}
346+
347+
/// Backup all files in patchy's config directory
348+
pub fn backup() -> Result<Vec<FileBackup>> {
349+
let config_files = fs::read_dir(&*PATH).map_err(|err| {
350+
anyhow!(
351+
"Failed to read files in directory `{}`:\n{err}",
352+
&PATH.display()
353+
)
354+
})?;
355+
356+
let mut backed_up_files = Vec::new();
357+
358+
for config_file in config_files.flatten() {
359+
let file_backup = fs::read_to_string(config_file.path())
360+
.map_err(|err| anyhow!("{err}"))
361+
.map(|contents| FileBackup {
362+
filename: config_file.file_name(),
363+
contents,
364+
})
365+
.map_err(|err| {
366+
anyhow!(
367+
"failed to backup patchy config file {} for configuration files:\n{err}",
368+
config_file.file_name().display()
369+
)
370+
})?;
371+
372+
backed_up_files.push(file_backup);
373+
}
374+
375+
Ok(backed_up_files)
376+
}
377+
}
378+
309379
#[cfg(test)]
310380
mod tests {
311381
use indexmap::indexset;

0 commit comments

Comments
 (0)