use crate::{AnsibleConfig, AnsibleError, ConfigFormat, PluginType};
use crate::command_config::CommandConfig;
use crate::async_support::{AsyncResult, AsyncExecute, IntoAsync, FromAsync};
use std::ffi::OsStr;
use std::fmt::Display;
use tokio::process::Command;
#[derive(Debug, Clone)]
pub struct AsyncAnsibleConfig {
command: String,
cfg: CommandConfig,
config_file: Option<String>,
plugin_type: Option<String>,
}
impl Default for AsyncAnsibleConfig {
fn default() -> Self {
Self {
command: "ansible-config".to_string(),
cfg: CommandConfig::default(),
config_file: None,
plugin_type: None,
}
}
}
impl AsyncAnsibleConfig {
pub fn new() -> Self {
Self::default()
}
pub fn set_system_envs(&mut self) -> &mut Self {
self.cfg.set_system_envs();
self
}
pub fn filter_envs<T, S>(&mut self, iter: T) -> &mut Self
where
T: IntoIterator<Item = S>,
S: AsRef<OsStr> + Display,
{
self.cfg.filter_envs(iter);
self
}
pub fn add_env(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
self.cfg.add_env(key, value);
self
}
pub fn arg<S: AsRef<OsStr> + Display>(&mut self, arg: S) -> &mut Self {
self.cfg.arg(arg);
self
}
pub fn set_config_file(&mut self, config_file: impl Into<String>) -> &mut Self {
self.config_file = Some(config_file.into());
self
}
pub fn set_format(&mut self, format: ConfigFormat) -> &mut Self {
self.arg("--format").arg(format.to_string());
self
}
pub fn set_plugin_type(&mut self, plugin_type: PluginType) -> &mut Self {
self.plugin_type = Some(plugin_type.to_string());
self
}
pub async fn list(&self) -> AsyncResult<String> {
let mut args = vec!["list"];
if let Some(ref plugin_type) = self.plugin_type {
args.push("--type");
args.push(plugin_type);
}
self.execute_config_command(&args).await
}
pub async fn dump(&self) -> AsyncResult<String> {
self.execute_config_command(&["dump"]).await
}
pub async fn view(&self) -> AsyncResult<String> {
self.execute_config_command(&["view"]).await
}
pub async fn init(&self) -> AsyncResult<String> {
self.execute_config_command(&["init"]).await
}
pub async fn validate(&self) -> AsyncResult<String> {
self.execute_config_command(&["validate"]).await
}
async fn execute_config_command(&self, args: &[&str]) -> AsyncResult<String> {
let mut cmd = Command::new(&self.command);
cmd.envs(&self.cfg.envs);
cmd.args(&self.cfg.args);
cmd.args(args);
if let Some(ref config_file) = self.config_file {
cmd.arg("--config").arg(config_file);
}
let output = cmd.output().await?;
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
return Err(AnsibleError::command_failed(
"Ansible config command failed",
output.status.code(),
Some(stdout),
Some(stderr),
));
}
let result = [output.stdout, "\n".as_bytes().to_vec(), output.stderr].concat();
let s = String::from_utf8_lossy(&result);
Ok(s.to_string())
}
}
impl AsyncExecute for AsyncAnsibleConfig {
async fn execute_async(&self) -> AsyncResult<String> {
self.list().await
}
}
impl IntoAsync<AsyncAnsibleConfig> for AnsibleConfig {
fn into_async(self) -> AsyncAnsibleConfig {
AsyncAnsibleConfig {
command: self.command,
cfg: self.cfg,
config_file: self.config_file,
plugin_type: self.plugin_type,
}
}
}
impl FromAsync<AsyncAnsibleConfig> for AnsibleConfig {
fn from_async(async_config: AsyncAnsibleConfig) -> Self {
AnsibleConfig {
command: async_config.command,
cfg: async_config.cfg,
config_file: async_config.config_file,
plugin_type: async_config.plugin_type,
}
}
}
impl std::fmt::Display for AsyncAnsibleConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.command)
}
}