use std::process::Stdio;
use tokio::process::Command;
use super::Result;
pub async fn get(kubectl_command: &str) -> Result<K8sVersion> {
let mut command = Command::new(kubectl_command);
command
.stdin(Stdio::null())
.stderr(Stdio::inherit())
.stdout(Stdio::piped());
command.arg("version");
command.arg("-o").arg("json");
command.kill_on_drop(true);
let reader = command.output().await?;
let json: serde_json::Value = serde_json::from_slice(&reader.stdout)?;
Ok(K8sVersion {
major: json["serverVersion"]["major"].to_string().replace('\"', ""),
minor: json["serverVersion"]["minor"].to_string().replace('\"', ""),
platform: json["serverVersion"]["platform"]
.to_string()
.replace('\"', ""),
git_version: json["serverVersion"]["gitVersion"]
.to_string()
.replace('\"', ""),
})
}
#[derive(Debug)]
pub struct K8sVersion {
major: String,
minor: String,
platform: String,
git_version: String,
}
impl K8sVersion {
pub fn major(&self) -> String {
self.major.to_string()
}
pub fn minor(&self) -> String {
self.minor.to_string()
}
pub fn platform(&self) -> String {
self.platform.to_string()
}
pub fn version(&self) -> String {
self.git_version.to_string()
}
}