22
33use crate :: config:: { self , BranchName , Config , PrNumber , PullRequest } ;
44use 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
107use anyhow:: { anyhow, bail} ;
118use colored:: Colorize as _;
@@ -14,14 +11,6 @@ use crate::github::{self, Branch, Remote, RemoteBranch};
1411use crate :: utils:: { format_pr, format_url, with_uuid} ;
1512use 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
2615pub 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
0 commit comments