1
1
//! `run` subcommand
2
2
3
- use crate :: config:: { BranchName , Config , PullRequest } ;
3
+ use crate :: config:: { self , BranchName , Config , PullRequest } ;
4
4
use std:: ffi:: OsString ;
5
5
use std:: fs:: { self , File } ;
6
6
use std:: io:: Write as _;
@@ -12,9 +12,7 @@ use colored::Colorize as _;
12
12
use crate :: git_high_level;
13
13
use crate :: github_api:: { self , Branch , Remote , RemoteBranch } ;
14
14
use crate :: utils:: { display_link, with_uuid} ;
15
- use crate :: {
16
- CONFIG_FILE , CONFIG_FILE_PATH , CONFIG_PATH , CONFIG_ROOT , commands, confirm_prompt, git,
17
- } ;
15
+ use crate :: { commands, confirm_prompt, git} ;
18
16
19
17
/// Backup for a file
20
18
struct FileBackup {
@@ -26,10 +24,13 @@ struct FileBackup {
26
24
27
25
/// Run patchy, if `yes` then there will be no prompt
28
26
pub async fn run ( yes : bool ) -> anyhow:: Result < ( ) > {
29
- let root = CONFIG_ROOT . as_str ( ) ;
27
+ let root = config :: ROOT . as_str ( ) ;
30
28
31
- let Ok ( config_string) = fs:: read_to_string ( & * CONFIG_FILE_PATH ) else {
32
- log:: error!( "Could not find configuration file at {root}/{CONFIG_FILE}" ) ;
29
+ let Ok ( config_string) = fs:: read_to_string ( & * config:: FILE_PATH ) else {
30
+ log:: error!(
31
+ "Could not find configuration file at {root}/{}" ,
32
+ config:: FILE
33
+ ) ;
33
34
34
35
// We don't want to have *any* sort of prompt when using the -y flag since that
35
36
// would be problematic in scripts
@@ -47,13 +48,16 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
47
48
return Ok ( ( ) ) ;
48
49
} ;
49
50
50
- log:: trace!( "Using configuration file {}" , CONFIG_FILE_PATH . display( ) ) ;
51
+ log:: trace!( "Using configuration file {}" , config :: FILE_PATH . display( ) ) ;
51
52
52
53
let config = toml:: from_str :: < Config > ( & config_string) . map_err ( |err| {
53
- anyhow ! ( "Could not parse `{root}/{CONFIG_FILE}` configuration file:\n {err}" , )
54
+ anyhow ! (
55
+ "Could not parse `{root}/{}` configuration file:\n {err}" ,
56
+ config:: FILE
57
+ )
54
58
} ) ?;
55
59
56
- let crate :: config:: Branch {
60
+ let config:: Branch {
57
61
name : remote_branch,
58
62
commit,
59
63
} = config. remote_branch ;
@@ -70,10 +74,10 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
70
74
71
75
// --- Backup all files in the `.patchy` config directory
72
76
73
- let config_files = fs:: read_dir ( & * CONFIG_PATH ) . map_err ( |err| {
77
+ let config_files = fs:: read_dir ( & * config :: PATH ) . map_err ( |err| {
74
78
anyhow ! (
75
79
"Failed to read files in directory `{}`:\n {err}" ,
76
- & CONFIG_PATH . display( )
80
+ & config :: PATH . display( )
77
81
)
78
82
} ) ?;
79
83
@@ -211,15 +215,18 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
211
215
}
212
216
}
213
217
214
- if let Err ( err) = fs:: create_dir_all ( git:: ROOT . join ( CONFIG_ROOT . as_str ( ) ) ) {
218
+ if let Err ( err) = fs:: create_dir_all ( git:: ROOT . join ( config :: ROOT . as_str ( ) ) ) {
215
219
git:: checkout ( & previous_branch) ?;
216
220
217
221
git:: delete_remote_and_branch (
218
222
& info. remote . local_remote_alias ,
219
223
& info. branch . local_branch_name ,
220
224
) ?;
221
225
222
- bail ! ( "Could not create directory {}\n {err}" , CONFIG_ROOT . as_str( ) ) ;
226
+ bail ! (
227
+ "Could not create directory {}\n {err}" ,
228
+ config:: ROOT . as_str( )
229
+ ) ;
223
230
}
224
231
225
232
// Restore all the backup files
@@ -228,7 +235,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
228
235
filename, contents, ..
229
236
} in & backed_up_files
230
237
{
231
- let path = git:: ROOT . join ( PathBuf :: from ( CONFIG_ROOT . as_str ( ) ) . join ( filename) ) ;
238
+ let path = git:: ROOT . join ( PathBuf :: from ( config :: ROOT . as_str ( ) ) . join ( filename) ) ;
232
239
let mut file =
233
240
File :: create ( & path) . map_err ( |err| anyhow ! ( "failed to restore backup: {err}" ) ) ?;
234
241
@@ -239,7 +246,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
239
246
240
247
for patch in config. patches {
241
248
let file_name = git:: ROOT
242
- . join ( CONFIG_ROOT . as_str ( ) )
249
+ . join ( config :: ROOT . as_str ( ) )
243
250
. join ( format ! ( "{patch}.patch" ) ) ;
244
251
245
252
if !file_name. exists ( ) {
@@ -265,7 +272,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
265
272
) ;
266
273
}
267
274
268
- git:: add ( CONFIG_ROOT . as_str ( ) ) ?;
275
+ git:: add ( config :: ROOT . as_str ( ) ) ?;
269
276
git:: commit ( "restore configuration files" ) ?;
270
277
271
278
let temporary_branch = with_uuid ( "temp-branch" ) ;
0 commit comments