Skip to content

Commit 258b21a

Browse files
committed
feat: support command with args
1 parent fad4dae commit 258b21a

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

agent/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ serde = { version = "1.0.202", features = ["derive"] }
2222
serde_json = "1.0"
2323
etcetera = "0.8"
2424

25+
shellish_parse = "2"
26+
2527
[build-dependencies]
2628
#vergen = { version = "9.0.0-beta.2" ,features = ["build"]}

agent/examples/publish_command.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::path::PathBuf;
23
use rumqttc::v5::{AsyncClient, MqttOptions, Incoming};
34
use rumqttc::v5::mqttbytes::QoS;
@@ -6,8 +7,13 @@ use mproxy::message::{RequestMessage, ResponseMessage};
67

78
#[tokio::main]
89
async fn main() -> anyhow::Result<()> {
10+
let args:Vec<String> = env::args().collect();
11+
let command = if args.len() > 1 {
12+
args.iter().skip(1).map(|x|x.to_string()).collect::<Vec<_>>().join(" ")
13+
} else {
14+
"ls -ls".to_string()
15+
};
916

10-
let command = "ls";
1117
let config_path:PathBuf = "./config.yml".parse().unwrap();
1218

1319
let is_atty = atty::is(atty::Stream::Stdout);
@@ -33,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
3339
let resp: ResponseMessage = serde_json::from_slice(data.payload.as_ref()).unwrap();
3440

3541
match resp {
36-
ResponseMessage::Ok {data, seq,..} => println!("recv:{data}, seq: {seq}"),
42+
ResponseMessage::Ok {data, seq, pid, ..} => println!("[{pid}][{seq}] {data}"),
3743
ResponseMessage::Err {message,..} => eprintln!("{message}"),
3844
}
3945
}

agent/src/handler.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,19 @@ impl Handler {
2626
info!("begin to handle cmd: {:?}",&cmd);
2727
match cmd {
2828
RequestMessage::Cmd { command,request_id } => {
29+
let command_parsed = shellish_parse::parse(&command,false);
30+
let command_parsed = match command_parsed {
31+
Ok(result) => result,
32+
Err(e) => {
33+
let _ = self.connection.publish_response(&self.publish_topic, ResponseMessage::Err {request_id: request_id.clone(), message:format!("{}", e)}).await;
34+
continue;
35+
}
36+
};
37+
2938
let mut seq:u32 = 1;
30-
let mut command = Command::new(&command);
39+
let mut command = Command::new(&command_parsed[0]);
40+
command.args(&command_parsed[1..]);
41+
3142
command.stdout(Stdio::piped()).stderr(Stdio::piped());
3243
match command.spawn() {
3344
Ok(mut child) => {
@@ -69,8 +80,15 @@ mod test {
6980

7081

7182
#[test]
72-
pub fn parse() {
83+
fn parse() {
84+
let v = shellish_parse::parse("ls -ls #tes",false).unwrap();
85+
println!("{v:?}");
86+
}
87+
88+
#[test]
89+
fn command_run() {
7390
let mut cmd = Command::new("ls");
91+
cmd.arg("-ls");
7492
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
7593
let mut child = cmd
7694
.spawn().unwrap();

0 commit comments

Comments
 (0)