ch_10: misc fixes

This commit is contained in:
Sylvain Kerkour
2021-05-16 11:43:29 +00:00
parent e00a110e66
commit 06a3a2fc33
9 changed files with 55 additions and 10 deletions
+11 -3
View File
@@ -1,11 +1,19 @@
use common::api;
use super::Client;
use crate::{config, Error};
impl Client {
pub fn list_jobs(&self) -> Result<(), Error> {
pub fn list_jobs(&self) -> Result<Vec<api::Job>, Error> {
let get_jobs_route = format!("{}/api/jobs", config::SERVER_URL);
// self.http_client
Ok(())
let res = self.http_client.get(get_jobs_route).send()?;
let api_res: api::Response<api::JobsList> = res.json()?;
if let Some(err) = api_res.error {
return Err(Error::Internal(err.message));
}
Ok(api_res.data.unwrap().jobs)
}
}
+33 -1
View File
@@ -1,7 +1,39 @@
use crate::{api, Error};
use prettytable::{Cell, Row, Table};
pub fn run(api_client: &api::Client) -> Result<(), Error> {
let _ = api_client.list_agents()?;
let jobs = api_client.list_jobs()?;
let mut table = Table::new();
table.add_row(Row::new(vec![
Cell::new("Job ID"),
Cell::new("Created At"),
Cell::new("Executed At"),
Cell::new("command"),
Cell::new("Args"),
Cell::new("Output"),
Cell::new("Agent ID"),
]));
for job in jobs {
table.add_row(Row::new(vec![
Cell::new(job.id.to_string().as_str()),
Cell::new(job.created_at.to_string().as_str()),
Cell::new(
job.executed_at
.map(|t| t.to_string())
.unwrap_or(String::new())
.as_str(),
),
Cell::new(job.command.as_str()),
Cell::new(job.args.join(" ").as_str()),
Cell::new(job.output.unwrap_or("".to_string()).as_str()),
Cell::new(job.agent_id.to_string().as_str()),
]));
}
table.printstd();
Ok(())
}
+1 -1
View File
@@ -2,7 +2,7 @@ use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum Error {
#[error("Internal error")]
#[error("Internal error: {0}")]
Internal(String),
#[error("{0}")]
NotFound(String),
+1
View File
@@ -52,6 +52,7 @@ pub struct Job {
pub command: String,
pub args: Vec<String>,
pub output: Option<String>,
pub agent_id: Uuid,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
+2 -2
View File
@@ -9,8 +9,8 @@ edition = "2018"
[dependencies]
common = { path = "../common" }
uuid = { version = "0.8", features = ["serde", "v4"] }
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "0.8", features = ["v4"] }
chrono = { version = "0.4" }
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "uuid", "json", "postgres", "migrate", "chrono", "time" ] }
warp = { version = "0.3", default-features = false }
tokio = { version = "1", features = ["full"] }
+1 -1
View File
@@ -15,4 +15,4 @@ CREATE TABLE jobs (
agent_id UUID NOT NULL REFERENCES agents(id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX index_jobs_on_agent_id ON jobs (agent_id);
CREATE INDEX index_jobs_on_agent_id ON jobs (agent_id);
+1
View File
@@ -9,6 +9,7 @@ pub async fn create_job(
input: api::CreateJob,
) -> Result<impl warp::Reply, warp::Rejection> {
let job = state.service.create_job(input).await?;
let job: api::Job = job.into();
let res = api::Response::ok(job);
let res_json = warp::reply::json(&res);
+1
View File
@@ -24,6 +24,7 @@ impl Into<api::Job> for Job {
command: self.command,
args: self.args.0,
output: self.output,
agent_id: self.agent_id,
}
}
}
+4 -2
View File
@@ -36,7 +36,7 @@ impl Service {
self.repo.update_job(&self.db, &job).await
}
pub async fn create_job(&self, input: CreateJob) -> Result<(), Error> {
pub async fn create_job(&self, input: CreateJob) -> Result<Job, Error> {
let command = input.command.trim();
let mut command_with_args: Vec<String> = command
.split_whitespace()
@@ -60,6 +60,8 @@ impl Service {
agent_id: input.agent_id,
};
self.repo.create_job(&self.db, &new_job).await
self.repo.create_job(&self.db, &new_job).await?;
Ok(new_job)
}
}