adnv 1.5.0

CLI that tracks, checks and auto-updates infrastructure dependency versions across Terraform, Helm, Docker, Git and npm
Documentation
use std::path::PathBuf;

use serde::Deserialize;

use super::{Error, Pipe, Result};

/// copy contents from `path` to either `dest`(if some) or pipe
#[derive(Debug, Deserialize)]
pub struct Action {
    path: PathBuf,
    dest: Option<PathBuf>,
}

impl Action {
    pub async fn execute(&self, pipe: &mut Pipe) -> Result<()> {
        super::validate_contained_path(&self.path, "copy path")?;
        if let Some(dest) = &self.dest {
            super::validate_contained_path(dest, "copy dest")?;
        }

        let input = pipe.input().ok_or(Error::NoInputInPipe)?;
        let output = pipe.output_temp()?;

        let path = self.path.clone();
        let dest_override = self.dest.clone();

        tracing::info!(
            "Copy `{}` to `{}`",
            path.display(),
            dest_override.as_deref().unwrap_or(&path).display()
        );

        crate::util::fs::copy_dir(input.clone(), output.clone()).await?;

        let dest_path = dest_override.as_deref().unwrap_or(&path);
        let dest = output.join(dest_path);

        if path.is_dir() {
            crate::util::fs::copy_dir(path.clone(), dest.clone()).await?;
        } else {
            tokio::fs::copy(&path, &dest).await.map_err(|err| {
                Error::io(
                    err,
                    format!(
                        "Could not copy `{}` into `{}`",
                        path.display(),
                        dest.display()
                    ),
                )
            })?;
        }

        Ok(())
    }
}