1+ use colored:: Colorize ;
12use std:: {
2- fs:: { self , File } ,
3+ fs:: { self , File , OpenOptions } ,
34 io:: Write ,
45} ;
56
67static SNIPPETS_SOURCE_FILE_NAME : & str = "interactor_main.rs" ;
8+ static SC_CONFIG_PATH : & str = "../sc-config.toml" ;
9+ static FULL_PROXY_ENTRY : & str = r#"[[proxy]]
10+ path = "interact-rs/src/proxy.rs"
11+ "# ;
12+ static PROXY_PATH : & str = "interact-rs/src/proxy.rs" ;
713
814pub ( crate ) fn create_snippets_folder ( snippets_folder_path : & str ) {
915 // returns error if folder already exists, so we ignore the result
@@ -72,15 +78,14 @@ path = ".."
7278version = "0.50.1"
7379
7480[dependencies.multiversx-sc]
75- version = "0.49.0 "
81+ version = "0.50.1 "
7682
7783[dependencies]
7884clap = {{ version = "4.4.7", features = ["derive"] }}
7985serde = {{ version = "1.0", features = ["derive"] }}
8086toml = "0.8.6"
8187
8288# [workspace]
83-
8489"#
8590 )
8691 . unwrap ( ) ;
@@ -100,31 +105,48 @@ pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: boo
100105 } else {
101106 match File :: options ( ) . create_new ( true ) . write ( true ) . open ( & lib_path) {
102107 Ok ( f) => f,
103- Err ( _) => panic ! ( "{lib_path} file already exists, --overwrite option was not provided" ) ,
108+ Err ( _) => {
109+ println ! (
110+ "{}" ,
111+ format!( "{lib_path} file already exists, --overwrite option was not provided" , )
112+ . yellow( )
113+ ) ;
114+ File :: options ( ) . write ( true ) . open ( & lib_path) . unwrap ( )
115+ } ,
104116 }
105117 }
106118}
107119
108120pub ( crate ) fn create_sc_config_file ( overwrite : bool ) {
109- let sc_config_path = "../sc-config.toml" ;
110- let mut file = if overwrite {
111- File :: create ( sc_config_path ) . unwrap ( )
121+ // check if the file should be overwritten or if it already exists
122+ let mut file = if overwrite || ! file_exists ( SC_CONFIG_PATH ) {
123+ File :: create ( SC_CONFIG_PATH ) . unwrap ( )
112124 } else {
113- match File :: options ( )
114- . create_new ( true )
115- . write ( true )
116- . open ( sc_config_path)
117- {
118- Ok ( f) => f,
119- Err ( _) => return ,
125+ // file already exists
126+ let file = OpenOptions :: new ( )
127+ . read ( true )
128+ . append ( true )
129+ . open ( SC_CONFIG_PATH )
130+ . unwrap ( ) ;
131+
132+ if file_contains_proxy_path ( SC_CONFIG_PATH ) . unwrap_or ( false ) {
133+ return ;
120134 }
135+
136+ file
121137 } ;
122138
123- writeln ! (
124- & mut file,
125- r#"[[proxy]]
126- path = "interact-rs/src/proxy.rs"
127- "#
128- )
129- . unwrap ( ) ;
139+ // write full proxy toml entry to the file
140+ writeln ! ( & mut file, "\n {FULL_PROXY_ENTRY}" ) . unwrap ( ) ;
141+ }
142+
143+ fn file_exists ( path : & str ) -> bool {
144+ fs:: metadata ( path) . is_ok ( )
145+ }
146+
147+ fn file_contains_proxy_path ( file_path : & str ) -> std:: io:: Result < bool > {
148+ let file_content = fs:: read_to_string ( file_path) ?;
149+ let proxy_entry = format ! ( "path = \" {}\" " , PROXY_PATH ) ;
150+
151+ Ok ( file_content. contains ( & proxy_entry) )
130152}
0 commit comments