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.

54 lines
1.3 KiB

use serde_json::{from_slice, Value};
use shlex::split;
use std::process::{Command, Output};
const PANEL_BINARY: &str = "/u_panel";
pub struct Panel;
impl Panel {
fn run(args: &[&str]) -> Output {
Command::new(PANEL_BINARY)
.arg("--json")
.args(args)
.output()
.unwrap()
}
pub fn output_argv(args: &[&str]) -> Value {
let result = Self::run(args);
assert!(result.status.success());
from_slice(&result.stdout).unwrap()
}
pub fn output<S: Into<String>>(args: S) -> Value {
let splitted = split(args.into().as_ref()).unwrap();
Self::output_argv(
splitted
.iter()
.map(|s| s.as_ref())
.collect::<Vec<&str>>()
.as_ref(),
)
}
fn status_is_ok(data: &Value) {
assert_eq!(
data["status"], "ok",
"Panel failed with erroneous status: {}",
data["data"]
);
}
pub fn check_status<S: Into<String>>(args: S) {
let result = Self::output(args);
Self::status_is_ok(&result);
}
pub fn check_output<S: Into<String>>(args: S) -> Vec<Value> {
let result = Self::output(args);
Self::status_is_ok(&result);
result["data"].as_array().unwrap().clone()
}
}