use std::env;
use std::path::{Path, PathBuf};
use tracing::warn;
use crate::Error;
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`.",
})
}