@@ -95,6 +95,7 @@ pub enum Commands {
9595 /// Edit your client config
9696 #[ clap( subcommand) ]
9797 Config ( ConfigSubcommands ) ,
98+ Slot ,
9899}
99100#[ derive( Debug , Subcommand ) ]
100101pub enum ConfigSubcommands {
@@ -159,7 +160,41 @@ async fn main() {
159160 client. join ( ) . await ;
160161 }
161162 Err ( e) => {
162- println ! ( "error: {:?}" , 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+ }
170+ }
171+ Commands :: Slot => {
172+ let config_file = {
173+ let home_path = std:: env:: var ( "HOME" ) . unwrap ( ) ;
174+
175+ // println!("path {:?}", path);
176+ let text =
177+ std:: fs:: read_to_string ( home_path + "/.config/tinydancer/config.json" ) . unwrap ( ) ;
178+
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+ }
196+ }
197+ Err ( _) => {
163198 std:: process:: Command :: new ( "echo" )
164199 . arg ( "\" Please set a config first using tindancer config set\" " )
165200 . spawn ( )
@@ -325,3 +360,11 @@ pub struct ConfigSchema {
325360 pub log_path : String ,
326361 pub cluster : String ,
327362}
363+
364+ #[ derive( Default , Debug , Clone , PartialEq , Serialize , Deserialize ) ]
365+ #[ serde( rename_all = "camelCase" ) ]
366+ pub struct GetSlotResponse {
367+ pub jsonrpc : String ,
368+ pub result : i64 ,
369+ pub id : i64 ,
370+ }
0 commit comments