|
1 | | -use std::fs; |
| 1 | +use std::fs::{self, File}; |
| 2 | +use std::io::Write as _; |
| 3 | +use std::path::PathBuf; |
2 | 4 |
|
3 | 5 | use anyhow::{anyhow, bail}; |
4 | 6 | use colored::Colorize as _; |
5 | 7 |
|
6 | 8 | use crate::commands::pr_fetch::ignore_octothorpe; |
7 | 9 | use crate::commit::Commit; |
8 | 10 | use crate::git::{self, GIT_ROOT, git}; |
| 11 | +use crate::note; |
9 | 12 | use crate::types::{Branch, BranchAndRemote, Configuration, Remote}; |
10 | 13 | use crate::utils::{display_link, with_uuid}; |
11 | 14 | use crate::{APP_NAME, CONFIG_FILE, CONFIG_ROOT, commands, confirm_prompt, fail, success}; |
12 | | -use crate::{backup, note}; |
13 | 15 |
|
14 | 16 | /// Parses user inputs of the form `<head><syntax><commit-hash>` |
15 | 17 | /// |
@@ -82,8 +84,33 @@ pub async fn run(yes: bool) -> anyhow::Result<()> { |
82 | 84 | ) |
83 | 85 | })?; |
84 | 86 |
|
85 | | - let backed_up_files = backup::files(config_files) |
86 | | - .map_err(|err| anyhow!("Failed to create backups for configuration files:\n{err}"))?; |
| 87 | + let backed_up_files = { |
| 88 | + let mut backups = Vec::new(); |
| 89 | + |
| 90 | + for config_file in config_files { |
| 91 | + let config_file = config_file?; |
| 92 | + |
| 93 | + let path = config_file.path(); |
| 94 | + let backup = fs::read_to_string(&path) |
| 95 | + .map_err(|err| anyhow!("{err}")) |
| 96 | + .and_then(|contents| { |
| 97 | + let filename = config_file.file_name(); |
| 98 | + let mut destination_backed_up = |
| 99 | + tempfile::tempfile().map_err(|err| anyhow!("{err}"))?; |
| 100 | + |
| 101 | + write!(destination_backed_up, "{contents}")?; |
| 102 | + |
| 103 | + Ok((filename, destination_backed_up, contents)) |
| 104 | + }) |
| 105 | + .map_err(|err| { |
| 106 | + anyhow!("Failed to create backups for configuration files:\n{err}") |
| 107 | + })?; |
| 108 | + |
| 109 | + backups.push(backup); |
| 110 | + } |
| 111 | + |
| 112 | + backups |
| 113 | + }; |
87 | 114 |
|
88 | 115 | let info = BranchAndRemote { |
89 | 116 | branch: Branch { |
@@ -164,15 +191,15 @@ pub async fn run(yes: bool) -> anyhow::Result<()> { |
164 | 191 | &info.branch.local_branch_name, |
165 | 192 | )?; |
166 | 193 |
|
167 | | - return Err(anyhow!( |
168 | | - "Could not create directory {}\n{err}", |
169 | | - CONFIG_ROOT.as_str() |
170 | | - )); |
| 194 | + bail!("Could not create directory {}\n{err}", CONFIG_ROOT.as_str()); |
171 | 195 | } |
172 | 196 |
|
173 | 197 | for (file_name, _file, contents) in &backed_up_files { |
174 | | - backup::restore(file_name, contents) |
175 | | - .map_err(|err| anyhow!("Could not restore backups:\n{err}"))?; |
| 198 | + let path = GIT_ROOT.join(PathBuf::from(CONFIG_ROOT.as_str()).join(file_name)); |
| 199 | + let mut file = |
| 200 | + File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?; |
| 201 | + |
| 202 | + write!(file, "{contents}")?; |
176 | 203 | } |
177 | 204 |
|
178 | 205 | // apply patches if they exist |
|
0 commit comments