You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.7 KiB

use std::{
collections::HashMap,
fmt
};
4 years ago
use serde::{
Deserialize,
Serialize
};
use uuid::Uuid;
use crate::{contracts::*, UID, exec_job};
4 years ago
4 years ago
pub struct UClient {
pub client_info: ClientInfo,
pub jobs: Vec<JobMeta>, // TODO: to futures
4 years ago
}
impl UClient {
pub fn new(client_info: ClientInfo) -> Self {
Self {
client_info,
jobs: Vec::new()
4 years ago
}
}
}
4 years ago
4 years ago
#[derive(Serialize, Deserialize, Debug, Clone)]
4 years ago
pub struct ClientInfo {
pub info: HashMap<String, String>,
4 years ago
pub id: Uuid,
}
impl ClientInfo {
pub fn gather() -> Self {
let mut info: HashMap<String, String> = HashMap::new();
for job in DEFAULT_JOBS {
let mut job_meta = JobMeta::from_shell(job.1.into());
let job_result = exec_job(&mut job_meta);
let job_data = match job_result.data {
Ok(output) => output.multiline(),
Err(e) => e.description()
};
info.insert(job.0.into(), job_data);
}
4 years ago
ClientInfo {
info,
4 years ago
id: *UID
4 years ago
}
}
pub fn get_field(&self, field: &str) -> Option<&String> {
self.info.get(field)
}
4 years ago
}
impl ToMsg for ClientInfo {}
const DEFAULT_JOBS: &[(&str, &str)] = &[
//("local ip", "ip a"),
("hostname", "hostname"),
("username", "whoami"),
("platform", "uname -a"),
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gather() {
let cli_info = ClientInfo::gather();
let field = cli_info.get_field("username").unwrap();
assert_eq!(
JobOutput::from_multiline(field).unwrap().stdout,
b"plazmoid".to_vec()
)
}
}