Skip to main content

ambient_ci/action_impl/
pwd.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    action::{ActionError, Context},
7    action_impl::ActionImpl,
8    runlog::RunLogSource,
9};
10
11/// Print working directory.
12///
13/// This is meant for debugging plans. It can be used in pre- and post-plans,
14/// where the `shell` action can't be.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct Pwd;
17
18impl ActionImpl for Pwd {
19    fn execute(&self, context: &mut Context) -> Result<(), ActionError> {
20        let path = context.source_dir();
21        let path = path
22            .canonicalize()
23            .map_err(|err| PwdError::Canonicalize(path.to_path_buf(), err))?;
24        context
25            .runlog()
26            .debug(RunLogSource::PostPlan, format!("cwd: {}", path.display()));
27        Ok(())
28    }
29}
30
31/// Errors from the Pwd action.
32#[derive(Debug, thiserror::Error)]
33pub enum PwdError {
34    /// Can't convert a path into an absolute one.
35    #[error("failed to make path absolute: {0}")]
36    Canonicalize(PathBuf, #[source] std::io::Error),
37}
38
39impl From<PwdError> for ActionError {
40    fn from(value: PwdError) -> Self {
41        Self::Pwd(value)
42    }
43}