Skip to content

Commit d8e121b

Browse files
committed
Make some codes more clear using cmd_lib
1 parent e485698 commit d8e121b

File tree

12 files changed

+215
-280
lines changed

12 files changed

+215
-280
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ time = "0.3.37"
154154
uuid = { version = "1.11.0", features = ["v4"] }
155155
which = "7.0.0"
156156
smallvec = "1.13.2"
157+
cmd_lib = "1.9.5"
157158

158159
[patch.crates-io]
159160
tokio-vsock = { git = "https://github.com/kvinwang/tokio-vsock", branch = "shared-self-accept" }

tappd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ default-net.workspace = true
3535
rocket-vsock-listener.workspace = true
3636
sd-notify.workspace = true
3737
reqwest.workspace = true
38+
cmd_lib.workspace = true

tappd/src/guest_api_service.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::{path::Path, process::Command};
1+
use std::{fmt::Debug, path::Path};
22

33
use anyhow::{Context, Result};
44
use bollard::{container::ListContainersOptions, Docker};
5+
use cmd_lib::run_cmd as cmd;
56
use fs_err as fs;
67
use guest_api::{
78
guest_api_server::{GuestApiRpc, GuestApiServer},
@@ -12,6 +13,7 @@ use host_api::Notification;
1213
use ra_rpc::{CallContext, RpcCall};
1314
use serde::Deserialize;
1415
use tappd_rpc::worker_server::WorkerRpc as _;
16+
use tracing::error;
1517

1618
use crate::{rpc_service::ExternalRpcHandler, AppState};
1719

@@ -57,9 +59,9 @@ impl GuestApiRpc for GuestApiHandler {
5759
async fn shutdown(self) -> Result<()> {
5860
tokio::spawn(async move {
5961
notify_host("shutdown.progress", "stopping app").await.ok();
60-
run_command("systemctl stop app-compose").ok();
62+
perr(cmd!(systemctl stop app-compose));
6163
notify_host("shutdown.progress", "powering off").await.ok();
62-
run_command("systemctl poweroff").ok();
64+
perr(cmd!(systemctl poweroff));
6365
});
6466
Ok(())
6567
}
@@ -217,10 +219,8 @@ pub async fn notify_host(event: &str, payload: &str) -> Result<()> {
217219
Ok(())
218220
}
219221

220-
fn run_command(command: &str) -> Result<()> {
221-
let output = Command::new("sh").arg("-c").arg(command).output()?;
222-
if !output.status.success() {
223-
return Err(anyhow::anyhow!("Command failed: {}", output.status));
222+
fn perr<T, E: Debug>(result: Result<T, E>) {
223+
if let Err(e) = &result {
224+
error!("{e:?}");
224225
}
225-
Ok(())
226226
}

tdxctl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ra-tls.workspace = true
3232
tproxy-rpc.workspace = true
3333
tdx-attest.workspace = true
3434
host-api = { workspace = true, features = ["client"] }
35+
cmd_lib.workspace = true
3536

3637
[dev-dependencies]
3738
rand.workspace = true

0 commit comments

Comments
 (0)