11use std:: env;
22use std:: io:: IsTerminal ;
33use std:: path:: PathBuf ;
4+ use std:: process:: exit;
45use anyhow:: bail;
56use clap:: Parser ;
67use rumqttc:: v5:: { AsyncClient , MqttOptions , Incoming } ;
@@ -43,6 +44,7 @@ async fn main() -> anyhow::Result<()> {
4344 let topic = config. get_subscribe_command_topic ( ) ;
4445 client. subscribe ( topic, QoS :: ExactlyOnce ) . await ?;
4546
47+ let client_ = client. clone ( ) ;
4648 tokio:: spawn ( async move {
4749 loop {
4850 let event = eventloop. poll ( ) . await ;
@@ -51,10 +53,17 @@ async fn main() -> anyhow::Result<()> {
5153 match event {
5254 rumqttc:: v5:: Event :: Incoming ( Incoming :: Publish ( data) ) => {
5355 let resp: ResponseMessage = serde_json:: from_slice ( data. payload . as_ref ( ) ) . unwrap ( ) ;
54-
5556 match resp {
56- ResponseMessage :: Ok { data, seq, pid, ..} => println ! ( "[{pid}][{seq}] {data}" ) ,
57- ResponseMessage :: Err { message, ..} => eprintln ! ( "{message}" ) ,
57+ ResponseMessage :: D { data, seq, pid, ..} => println ! ( "[{pid}][{seq}] {data}" ) ,
58+ ResponseMessage :: Err { message, ..} => {
59+ eprintln ! ( "{message}" ) ;
60+ let _ = client_. disconnect ( ) . await ;
61+ exit ( 1 ) ;
62+ } ,
63+ ResponseMessage :: Ok { ..} => {
64+ let _ = client_. disconnect ( ) . await ;
65+ exit ( 0 ) ;
66+ }
5867 }
5968 }
6069 _ => ( )
@@ -64,7 +73,7 @@ async fn main() -> anyhow::Result<()> {
6473 } ;
6574 }
6675 } ) ;
67- let command = RequestMessage :: Cmd { command : command. to_string ( ) , request_id : "test_request_id" . to_string ( ) } ;
76+ let command = RequestMessage :: Cmd { command : command. to_string ( ) , req_id : "test_request_id" . to_string ( ) } ;
6877 let command = serde_json:: to_vec ( & command) . unwrap ( ) ;
6978 client. publish ( config. get_publish_command_topic ( ) , QoS :: ExactlyOnce , false , command) . await ?;
7079 tokio:: signal:: ctrl_c ( ) . await ?;
@@ -85,10 +94,10 @@ mod test{
8594 let config = Cli :: parse_from ( [ APP_NAME , "--config=config" , "ls" , "pwd" ] ) ;
8695 println ! ( "{config:?}" ) ;
8796
88- let config = Cli :: parse_from ( [ APP_NAME , "-c" , "config.yaml" , "ls" , "pwd "] ) ;
97+ let config = Cli :: parse_from ( [ APP_NAME , "-c" , "config.yaml" , "ls -ls " ] ) ;
8998 println ! ( "{config:?}" ) ;
9099
91- let config = Cli :: parse_from ( [ APP_NAME , "-c" , "config.yaml" ] ) ;
100+ let config = Cli :: parse_from ( [ APP_NAME , "-c" , "config.yaml" , "--" , "ls" , "-ls" ] ) ;
92101 println ! ( "{config:?}" ) ;
93102
94103 }
0 commit comments