1use crate::provider::CnfError;
10use crate::provider::prelude::*;
11
12use is_executable::is_executable;
13use tokio::fs;
14
15#[derive(Debug, PartialEq)]
17pub struct Cwd {
18 path: std::path::PathBuf,
19}
20
21impl Cwd {
22 pub fn new() -> Result<Self, CnfError> {
24 Ok(Cwd {
25 path: std::env::current_dir().context("cannot create 'cwd' provider")?,
26 })
27 }
28
29 pub fn with_path(path: std::path::PathBuf) -> Self {
31 Cwd { path }
32 }
33}
34
35impl fmt::Display for Cwd {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 write!(f, "cwd")
38 }
39}
40
41#[async_trait]
42impl IsProvider for Cwd {
43 async fn search_internal(
44 &self,
45 command: &str,
46 _target_env: Arc<crate::environment::Environment>,
47 ) -> ProviderResult<Vec<Candidate>> {
48 let mut results = vec![];
49
50 if let Ok(mut diriter) = fs::read_dir(&self.path).await {
51 while let Some(entry) = diriter.next_entry().await.transpose() {
52 if entry.is_err() {
54 continue;
55 }
56 let direntry = entry.unwrap();
57 let entrypath = &direntry.path();
58 trace!("checking file {}", entrypath.display());
59 let metadata = match fs::metadata(entrypath).await {
60 Ok(val) => val,
61 Err(_) => continue,
62 };
63
64 if !metadata.file_type().is_file() {
65 trace!("skipping {}: Not a file", entrypath.display());
66 continue;
67 }
68
69 let filename = direntry.file_name();
71 let filename = match filename.to_str().to_owned() {
72 Some(name) => name,
73 None => {
74 trace!("Skipping {}: Invalid unicode name", entrypath.display());
75 continue;
76 }
77 };
78
79 if filename.starts_with(command) {
80 let pwd = std::env::current_dir().unwrap();
82 let filename = pwd.join(filename).display().to_string().to_owned();
83
84 let mut candidate = Candidate {
85 package: filename.clone(),
86 origin: pwd.display().to_string(),
87 actions: Actions {
88 install: None,
89 execute: cmd!(&filename),
90 },
91 ..Candidate::default()
92 };
93
94 if !is_executable(entrypath) {
95 candidate.actions.install = Some(cmd!("chmod", "+x", &filename));
96 }
97 results.push(candidate);
98 }
99 }
100 if !results.is_empty() {
101 return Ok(results);
102 }
103 }
104 Err(ProviderError::NotFound(command.to_string()))
105 }
106}