forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.rs
More file actions
57 lines (51 loc) · 2.07 KB
/
Copy pathresolve.rs
File metadata and controls
57 lines (51 loc) · 2.07 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
//! Internal resolution of the GitHub Copilot CLI binary.
//!
//! Resolution order (matches the .NET and TypeScript SDKs):
//!
//! 1. An explicit path supplied by the application via
//! [`CliProgram::Path`](crate::CliProgram::Path).
//! 2. The `COPILOT_CLI_PATH` environment variable.
//! 3. The bundled CLI embedded in this crate at build time (gated on the
//! default `bundled-cli` cargo feature).
//!
//! There is no PATH scanning and no walking of standard install locations.
//! If you've opted out of bundling (via `default-features = false`) and
//! neither `CliProgram::Path` nor `COPILOT_CLI_PATH` is set,
//! [`Client::start`](crate::Client::start) returns
//! [`Error::BinaryNotFound`](crate::Error::BinaryNotFound).
use std::env;
use std::path::{Path, PathBuf};
use tracing::warn;
use crate::Error;
/// Resolve the CLI binary, optionally overriding the directory the bundled
/// CLI is extracted to. Called by `Client::start` to thread
/// `ClientOptions::bundled_cli_extract_dir` through to
/// `embeddedcli::install_at`.
pub(crate) fn copilot_binary_with_extract_dir(
extract_dir: Option<&Path>,
) -> Result<PathBuf, Error> {
if let Ok(value) = env::var("COPILOT_CLI_PATH") {
let candidate = PathBuf::from(&value);
if candidate.is_file() {
return Ok(candidate);
}
warn!(
path = %candidate.display(),
"COPILOT_CLI_PATH is set but does not point to a file; falling back to bundled CLI"
);
}
let bundled = match extract_dir {
Some(dir) => crate::embeddedcli::install_at(dir),
None => crate::embeddedcli::path(),
};
if let Some(path) = bundled {
return Ok(path);
}
Err(Error::BinaryNotFound {
name: "copilot",
hint: "the Copilot CLI is not bundled in this build of github-copilot-sdk and \
COPILOT_CLI_PATH is not set. Either keep the default `bundled-cli` cargo \
feature enabled, set COPILOT_CLI_PATH, or supply an explicit path via \
`CliProgram::Path(...)` on `ClientOptions::program`.",
})
}