@@ -50,15 +50,19 @@ mod rpc_wrapper;
5050mod sampler;
5151mod ui;
5252
53+ use tracing:: { info} ;
54+ use tracing_subscriber;
55+ use anyhow:: { Result , anyhow} ;
5356use clap:: { ArgGroup , Parser , Subcommand , * } ;
57+
5458#[ derive( Parser , Debug ) ]
5559#[ clap( author, version, about, long_about = None ) ]
56-
5760struct Args {
5861 /// Subcommands to run
5962 #[ clap( subcommand) ]
6063 command : Commands ,
6164}
65+
6266#[ derive( Debug , Subcommand ) ]
6367pub enum Commands {
6468 /// Start the local light client
@@ -97,6 +101,7 @@ pub enum Commands {
97101 Config ( ConfigSubcommands ) ,
98102 Slot ,
99103}
104+
100105#[ derive( Debug , Subcommand ) ]
101106pub enum ConfigSubcommands {
102107 Set {
@@ -108,10 +113,19 @@ pub enum ConfigSubcommands {
108113 } ,
109114 Get ,
110115}
116+
117+ pub fn get_config_file ( ) -> Result < ConfigSchema > {
118+ let home_path = std:: env:: var ( "HOME" ) ?;
119+ let path = home_path + "/.config/tinydancer/config.json" ;
120+ let config_str = std:: fs:: read_to_string ( path) ?;
121+ Ok ( serde_json:: from_str :: < ConfigSchema > ( & config_str) ?)
122+ }
123+
111124// ~/.config/
112125#[ tokio:: main]
113- async fn main ( ) {
126+ async fn main ( ) -> Result < ( ) > {
114127 let args = Args :: parse ( ) ;
128+ tracing_subscriber:: fmt:: init ( ) ;
115129
116130 match args. command {
117131 Commands :: Logs { log_path } => {
@@ -121,84 +135,46 @@ async fn main() {
121135 . output ( )
122136 . expect ( "log command failed" ) ;
123137 }
138+
124139 Commands :: Start {
125140 enable_ui_service,
126141 sample_qty,
127142 archive_path,
128143 shred_archive_duration,
129144 tui_monitor,
130145 } => {
131- let mut config_file = {
132- let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
133-
134- // println!("path {:?}", path);
135- let text =
136- std:: fs:: read_to_string ( home_path + "/.config/tinydancer/config.json" ) . unwrap ( ) ;
137-
138- serde_json:: from_str :: < ConfigSchema > ( & text)
146+ let config_file = get_config_file ( ) . map_err ( |_| anyhow ! ( "tinydancer config not set" ) ) ?;
147+ let config = TinyDancerConfig {
148+ enable_ui_service,
149+ rpc_endpoint : get_cluster ( config_file. cluster ) ,
150+ sample_qty,
151+ tui_monitor,
152+ log_path : config_file. log_path ,
153+ archive_config : {
154+ archive_path. map ( |path| ArchiveConfig {
155+ shred_archive_duration,
156+ archive_path : path,
157+ } )
158+ } ,
139159 } ;
140- match config_file {
141- Ok ( config_file) => {
142- let config = TinyDancerConfig {
143- enable_ui_service,
144- rpc_endpoint : get_cluster ( config_file. cluster ) ,
145- sample_qty,
146- tui_monitor,
147- log_path : config_file. log_path ,
148- archive_config : {
149- if let Some ( path) = archive_path {
150- Some ( ArchiveConfig {
151- shred_archive_duration,
152- archive_path : path,
153- } )
154- } else {
155- None
156- }
157- } ,
158- } ;
159- let client = TinyDancer :: new ( config) . await ;
160- client. join ( ) . await ;
161- }
162- Err ( e) => {
163- // println!("error: {:?}", e);
164- std:: process:: Command :: new ( "echo" )
165- . arg ( "\" Please set a config first using tindancer config set\" " )
166- . spawn ( )
167- . expect ( "Config not set" ) ;
168- }
169- }
160+ let client = TinyDancer :: new ( config) . await ;
161+ client. join ( ) . await ;
170162 }
171- Commands :: Slot => {
172- let config_file = {
173- let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
174163
175- // println!("path {:?}", path);
176- let text =
177- std:: fs:: read_to_string ( home_path + "/.config/tinydancer/config.json" ) . unwrap ( ) ;
164+ Commands :: Slot => {
165+ let config_file = get_config_file ( ) . map_err ( |_| anyhow ! ( "tinydancer config not set" ) ) ?;
166+ let slot_res = send_rpc_call ! (
167+ get_endpoint( config_file. cluster) ,
168+ serde_json:: json!( { "jsonrpc" : "2.0" , "id" : 1 , "method" : "getSlot" } ) . to_string( )
169+ ) ;
170+ let slot = serde_json:: from_str :: < GetSlotResponse > ( slot_res. as_str ( ) ) ;
178171
179- serde_json:: from_str :: < ConfigSchema > ( & text)
180- } ;
181- match config_file {
182- Ok ( config_file) => {
183- let slot_res = send_rpc_call ! (
184- get_endpoint( config_file. cluster) ,
185- serde_json:: json!( { "jsonrpc" : "2.0" , "id" : 1 , "method" : "getSlot" } ) . to_string( )
186- ) ;
187- let slot = serde_json:: from_str :: < GetSlotResponse > ( slot_res. as_str ( ) ) ;
188- match slot {
189- Ok ( slot) => {
190- println ! ( "Slot: {}" , slot. result. to_string( ) . green( ) , ) ;
191- }
192- Err ( e) => {
193- println ! ( "Failed to get slot,due to error: {}" , e. to_string( ) . red( ) ) ;
194- }
195- }
172+ match slot {
173+ Ok ( slot) => {
174+ println ! ( "Slot: {}" , slot. result. to_string( ) . green( ) , ) ;
196175 }
197- Err ( _) => {
198- std:: process:: Command :: new ( "echo" )
199- . arg ( "\" Please set a config first using tindancer config set\" " )
200- . spawn ( )
201- . expect ( "Config not set" ) ;
176+ Err ( e) => {
177+ println ! ( "Failed to get slot,due to error: {}" , e. to_string( ) . red( ) ) ;
202178 }
203179 }
204180 }
@@ -224,117 +200,80 @@ async fn main() {
224200 // println!("{:?}", fs::create_dir_all("~/.config/tinydancer"));
225201
226202 let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
227- let is_existing = home_path. clone ( ) + "/.config/tinydancer" ;
228- let path = Path :: new ( & is_existing) ;
203+ let tinydancer_dir = home_path + "/.config/tinydancer" ;
204+
205+ let path = Path :: new ( & tinydancer_dir) ;
229206 if !path. exists ( ) {
230207 std:: process:: Command :: new ( "mkdir" )
231- . arg ( home_path . clone ( ) + "/.config/tinydancer" )
208+ . arg ( & tinydancer_dir )
232209 . stdout ( std:: process:: Stdio :: null ( ) )
233210 . spawn ( )
234211 . expect ( "couldnt make dir" ) ;
235212 }
236- let is_existing = home_path. clone ( ) + "/.config/tinydancer/config.json" ;
237- let path = Path :: new ( & is_existing) ;
213+ sleep ( Duration :: from_secs ( 1 ) ) ;
214+
215+ let config_path = tinydancer_dir + "./config.json" ;
216+ let path = Path :: new ( & config_path) ;
238217 if !path. exists ( ) {
239218 std:: process:: Command :: new ( "touch" )
240- . arg ( home_path . clone ( ) + "/.config/tinydancer/config.json" )
219+ . arg ( & config_path )
241220 . stdout ( std:: process:: Stdio :: null ( ) )
242221 . spawn ( )
243222 . expect ( "couldnt make file" ) ;
244223 }
245224 sleep ( Duration :: from_secs ( 1 ) ) ;
246- loop {
247- let mut config_file = {
248- let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
249225
250- // println!("path {:?}", path);
251- let text =
252- std:: fs:: read_to_string ( home_path + "/.config/tinydancer/config.json" )
253- . unwrap ( ) ;
254-
255- serde_json:: from_str :: < ConfigSchema > ( & text)
256- } ;
257-
258- match config_file {
259- Ok ( mut config_file) => {
260- config_file. log_path = log_path. clone ( ) ;
261- config_file. cluster = cluster. clone ( ) ;
262- std:: fs:: write (
263- home_path. clone ( ) + "/.config/tinydancer/config.json" ,
264- serde_json:: to_string_pretty ( & config_file) . unwrap ( ) ,
265- )
266- . unwrap ( ) ;
267- break ;
268- }
269- Err ( _) => {
270- std:: fs:: write (
271- home_path. clone ( ) + "/.config/tinydancer/config.json" ,
272- serde_json:: to_string_pretty ( & serde_json:: json!( {
273- "cluster" : "Localnet" ,
274- "logPath" : "client.log"
275- } ) )
276- . unwrap ( ) ,
277- )
278- . unwrap ( ) ;
279- break ;
280- }
226+ let config_file = get_config_file ( ) ;
227+ match config_file {
228+ Ok ( mut config_file) => {
229+ // overwrite
230+ config_file. log_path = log_path;
231+ config_file. cluster = cluster;
232+ std:: fs:: write (
233+ config_path,
234+ serde_json:: to_string_pretty ( & config_file) ?,
235+ ) ?;
236+ }
237+ Err ( _) => {
238+ // initialize
239+ std:: fs:: write (
240+ config_path,
241+ serde_json:: to_string_pretty ( & serde_json:: json!( {
242+ "cluster" : "Localnet" ,
243+ "logPath" : "client.log"
244+ } ) ) ?,
245+ ) ?;
281246 }
282247 }
283248 }
284249 } ,
285250 Commands :: Verify { slot } => {
286- let mut spinner = Spinner :: new (
251+ let _spinner = Spinner :: new (
287252 spinners:: Dots ,
288253 format ! ( "Verifying Shreds for Slot {}" , slot) ,
289254 Color :: Green ,
290255 ) ;
291- let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
292- let is_existing = home_path. clone ( ) + "/.config/tinydancer/config.json" ;
293- let path = Path :: new ( & is_existing) ;
294- if path. exists ( ) {
295- let mut config_file = {
296- let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
297-
298- let text =
299- std:: fs:: read_to_string ( home_path + "/.config/tinydancer/config.json" )
300- . unwrap ( ) ;
301256
302- serde_json:: from_str :: < ConfigSchema > ( & text)
303- } ;
304- // println!("path {:?}", config_file);
305- match config_file {
306- Ok ( config_file) => {
307- let is_verified =
308- pull_and_verify_shreds ( slot, get_endpoint ( config_file. cluster ) ) . await ;
257+ let config_file = get_config_file ( ) . map_err ( |_| anyhow ! ( "tinydancer config not set" ) ) ?;
258+ let is_verified = pull_and_verify_shreds ( slot, get_endpoint ( config_file. cluster ) ) . await ;
309259
310- if is_verified {
311- println ! (
312- "\n Slot {} is {} ✓" ,
313- slot. to_string( ) . yellow( ) ,
314- "Valid" . to_string( ) . green( )
315- ) ;
316- } else {
317- println ! (
318- "\n Slot {} is not {} ❌" ,
319- slot. to_string( ) . yellow( ) ,
320- "Valid" . to_string( ) . red( )
321- ) ;
322- }
323- }
324- Err ( e) => {
325- tiny_logger:: logs:: error!( "{}" , e) ;
326- println ! ( "e {:?}" , e) ;
327- }
328- } ;
260+ if is_verified {
261+ println ! (
262+ "\n Slot {} is {} ✓" ,
263+ slot. to_string( ) . yellow( ) ,
264+ "Valid" . to_string( ) . green( )
265+ ) ;
329266 } else {
330267 println ! (
331- "{} {} " ,
332- "Initialise a config first using:" . to_string( ) . yellow( ) ,
333- "tinydancer set config " . to_string( ) . green ( )
268+ "\n Slot {} is not {} ❌ " ,
269+ slot . to_string( ) . yellow( ) ,
270+ "Valid " . to_string( ) . red ( )
334271 ) ;
335272 }
336273 }
337274 }
275+
276+ Ok ( ( ) )
338277}
339278
340279pub fn get_cluster ( cluster : String ) -> Cluster {
0 commit comments