Repository navigation
Expand file tree
/
Copy pathwhich.rs
More file actions
75 lines (63 loc) · 2.65 KB
/
Copy pathwhich.rs
File metadata and controls
75 lines (63 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::env;
use std::ffi::OsString;
use which::which_in;
use volta_core::error::{Context, ErrorKind, ExitCode, Fallible};
use volta_core::platform::{Platform, System};
use volta_core::run::binary::DefaultBinary;
use volta_core::session::{ActivityKind, Session};
use crate::command::Command;
#[derive(clap::Args)]
pub(crate) struct Which {
/// The binary to find, e.g. `node` or `npm`
binary: OsString,
}
impl Command for Which {
// 1. Start by checking if the user has a tool installed in the project or
// as a user default. If so, we're done.
// 2. Otherwise, use the platform image and/or the system environment to
// determine a lookup path to run `which` in.
fn run(self, session: &mut Session) -> Fallible<ExitCode> {
session.add_event_start(ActivityKind::Which);
let default_tool = DefaultBinary::from_name(&self.binary, session)?;
let project_bin_path = session
.project()?
.and_then(|project| project.find_bin(&self.binary));
let tool_path = match (default_tool, project_bin_path) {
(Some(_), Some(bin_path)) => Some(bin_path),
(Some(tool), _) => Some(tool.bin_path),
_ => None,
};
if let Some(path) = tool_path {
println!("{}", path.to_string_lossy());
let exit_code = ExitCode::Success;
session.add_event_end(ActivityKind::Which, exit_code);
return Ok(exit_code);
}
// Treat any error with obtaining the current platform image as if the image doesn't exist
// However, errors in obtaining the current working directory or the System path should
// still be treated as errors.
let path = match Platform::current(session)
.unwrap_or(None)
.and_then(|platform| platform.checkout(session).ok())
.and_then(|image| image.path().ok())
{
Some(path) => path,
None => System::path()?,
};
let cwd = env::current_dir().with_context(|| ErrorKind::CurrentDirError)?;
let exit_code = match which_in(&self.binary, Some(path), cwd) {
Ok(result) => {
println!("{}", result.to_string_lossy());
ExitCode::Success
}
Err(_) => {
// `which_in` Will return an Err if it can't find the binary in the path
// In that case, we don't want to print anything out, but we want to return
// Exit Code 1 (ExitCode::UnknownError)
ExitCode::UnknownError
}
};
session.add_event_end(ActivityKind::Which, exit_code);
Ok(exit_code)
}
}