1
1
use anyhow:: Context ;
2
2
use clap:: Parser ;
3
3
use josh_sync:: JoshConfig ;
4
+ use josh_sync:: josh:: { JoshProxy , try_install_josh} ;
5
+ use std:: path:: { Path , PathBuf } ;
4
6
5
7
const DEFAULT_CONFIG_PATH : & str = "josh-sync.toml" ;
6
8
@@ -14,6 +16,12 @@ struct Args {
14
16
enum Command {
15
17
/// Initialize a config file for this repository.
16
18
Init ,
19
+ /// Pull changes from the main `rustc` repository.
20
+ /// This creates new commits that should be then merged into this subtree repository.
21
+ Pull {
22
+ #[ clap( long, default_value( DEFAULT_CONFIG_PATH ) ) ]
23
+ config : PathBuf ,
24
+ } ,
17
25
}
18
26
19
27
fn main ( ) -> anyhow:: Result < ( ) > {
@@ -27,8 +35,40 @@ fn main() -> anyhow::Result<()> {
27
35
} ;
28
36
let config = toml:: to_string_pretty ( & config) . context ( "cannot serialize config" ) ?;
29
37
std:: fs:: write ( DEFAULT_CONFIG_PATH , config) . context ( "cannot write config" ) ?;
38
+ println ! ( "Created config file at {DEFAULT_CONFIG_PATH}" ) ;
39
+ }
40
+ Command :: Pull { config } => {
41
+ let config = load_config ( & config)
42
+ . context ( "cannot load config. Run the `init` command to initialize it." ) ?;
43
+ let josh = get_josh_proxy ( ) ?;
30
44
}
31
45
}
32
46
33
47
Ok ( ( ) )
34
48
}
49
+
50
+ fn get_josh_proxy ( ) -> anyhow:: Result < JoshProxy > {
51
+ match JoshProxy :: lookup ( ) {
52
+ Some ( proxy) => Ok ( proxy) ,
53
+ None => {
54
+ println ! ( "josh-proxy not found. Do you want to install it? [y/n]" ) ;
55
+ let mut line = String :: new ( ) ;
56
+ std:: io:: stdin ( ) . read_line ( & mut line) ?;
57
+ if line. trim ( ) . to_lowercase ( ) == "y" {
58
+ match try_install_josh ( ) {
59
+ Some ( proxy) => Ok ( proxy) ,
60
+ None => Err ( anyhow:: anyhow!( "Could not install josh-proxy" ) ) ,
61
+ }
62
+ } else {
63
+ Err ( anyhow:: anyhow!( "josh-proxy could not be found" ) )
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ fn load_config ( path : & Path ) -> anyhow:: Result < JoshConfig > {
70
+ let data = std:: fs:: read_to_string ( path)
71
+ . with_context ( || format ! ( "cannot load config file from {}" , path. display( ) ) ) ?;
72
+ let config: JoshConfig = toml:: from_str ( & data) . context ( "cannot load config as TOML" ) ?;
73
+ Ok ( config)
74
+ }
0 commit comments