-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): Add basic "docker ps" endpoint. (#5)
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use anyhow::anyhow; | ||
use async_graphql::{Object, SimpleObject}; | ||
use serde::Deserialize; | ||
use tokio::process::Command; | ||
|
||
pub struct Docker; | ||
|
||
const DOCKER_FORMAT_ARGS: [&str; 2] = ["--format", "{{json .}}"]; | ||
|
||
/// Information | ||
#[Object] | ||
impl Docker { | ||
async fn ps(&self) -> anyhow::Result<Vec<DockerPsResponse>> { | ||
let output = Command::new("docker") | ||
.arg("ps") | ||
.args(DOCKER_FORMAT_ARGS) | ||
.output() | ||
.await?; | ||
if !&output.status.success() { | ||
return match std::str::from_utf8(&output.stderr) { | ||
Ok(s) => Err(anyhow!("{}", s)), | ||
Err(e) => Err(e.into()), | ||
}; | ||
} | ||
let output = std::str::from_utf8(&output.stdout)?; | ||
let mut processes: Vec<DockerPsResponse> = Vec::new(); | ||
|
||
for line in output.lines() { | ||
let parsed = serde_json::from_str::<DockerPsResponse>(line)?; | ||
processes.push(parsed); | ||
} | ||
Ok(processes) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, SimpleObject, Debug)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct DockerPsResponse { | ||
command: String, | ||
created_at: String, | ||
image: String, | ||
names: String, | ||
running_for: String, | ||
state: String, | ||
status: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters