2
2
3
3
use crate :: config:: { self , BranchName , Config , PrNumber , PullRequest } ;
4
4
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;
9
6
10
7
use anyhow:: { anyhow, bail} ;
11
8
use colored:: Colorize as _;
@@ -14,14 +11,6 @@ use crate::github::{self, Branch, Remote, RemoteBranch};
14
11
use crate :: utils:: { format_pr, format_url, with_uuid} ;
15
12
use crate :: { commands, confirm_prompt, git} ;
16
13
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
-
25
14
/// Run patchy, if `yes` then there will be no prompt
26
15
pub async fn run ( yes : bool , use_gh_cli : bool ) -> Result < ( ) > {
27
16
let root = config:: ROOT . as_str ( ) ;
@@ -72,35 +61,7 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
72
61
) ;
73
62
}
74
63
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 ( ) ?;
104
65
105
66
let info = RemoteBranch {
106
67
remote : Remote {
@@ -240,20 +201,7 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
240
201
) ;
241
202
}
242
203
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) ?;
257
205
258
206
for patch in config. patches {
259
207
let file_name = git:: ROOT
0 commit comments