use compose_yml::v2 as dc;
use std::marker::PhantomData;
use crate::errors::*;
use crate::plugins;
use crate::plugins::{Operation, PluginNew, PluginTransform};
use crate::project::Project;
use crate::util::{err, ConductorPathExt};
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct Plugin {
_placeholder: PhantomData<()>,
}
impl plugins::Plugin for Plugin {
fn name(&self) -> &'static str {
Self::plugin_name()
}
}
impl PluginNew for Plugin {
fn plugin_name() -> &'static str {
"abs_path"
}
fn new(_project: &Project) -> Result<Self> {
Ok(Plugin {
_placeholder: PhantomData,
})
}
}
impl PluginTransform for Plugin {
fn transform(
&self,
op: Operation,
ctx: &plugins::Context<'_>,
file: &mut dc::File,
) -> Result<()> {
if op != Operation::Output {
return Ok(());
}
for service in file.services.values_mut() {
if let Some(ref mut build) = service.build {
let context: &mut _ = build.context.value_mut()?;
if let dc::Context::Dir(ref mut path) = *context {
let new_path = ctx.project.pods_dir().join(&path).to_absolute()?;
*path = new_path;
}
}
for volume in &mut service.volumes {
let volume = volume.value_mut()?;
let new_host = match volume.host {
Some(dc::HostVolume::Path(ref path)) if path.is_relative() => {
let new_path = ctx.project.pods_dir().join(path);
Some(dc::HostVolume::Path(new_path.to_absolute()?))
}
Some(dc::HostVolume::UserRelativePath(ref path))
if path.is_relative() =>
{
let home = dirs::home_dir()
.ok_or_else(|| err("Cannot find HOME directory"))?;
let new_path = home.join(path);
Some(dc::HostVolume::Path(new_path.to_absolute()?))
}
ref other => other.to_owned(),
};
volume.host = new_host;
}
}
Ok(())
}
}