Skip to main content

kea_lifecycle/buildpack/
executable.rs

1use std::path::{Path, PathBuf};
2
3use super::error::{BuildpackError, Result};
4
5pub struct ExecutablePath(pub PathBuf);
6
7impl ExecutablePath {
8    pub fn from_execuing_path() -> Result<Self> {
9        let executable_path = std::env::current_exe().map_err(|_| {
10            BuildpackError::InvalidExecutable("can't get current executable path".into())
11        })?;
12        Self::from_path(executable_path)
13    }
14
15    pub fn from_path(executable_path: impl AsRef<Path>) -> Result<Self> {
16        Ok(Self(executable_path.as_ref().to_owned()))
17    }
18
19    pub fn executable_name(&self) -> Result<String> {
20        let executable_filename = self
21            .0
22            .file_name()
23            .ok_or_else(|| {
24                BuildpackError::InvalidExecutable(format!(
25                    "cant get file name from '{}'",
26                    self.0.to_string_lossy()
27                ))
28            })?
29            .to_string_lossy();
30
31        let executable = executable_filename
32            .strip_suffix(std::env::consts::EXE_SUFFIX)
33            .unwrap_or(executable_filename.as_ref());
34
35        Ok(executable.into())
36    }
37}