forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddedcli.rs
More file actions
278 lines (241 loc) · 8.41 KB
/
Copy pathembeddedcli.rs
File metadata and controls
278 lines (241 loc) · 8.41 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#[cfg(any(has_bundled_cli, test))]
use std::fs;
#[cfg(any(has_bundled_cli, test))]
use std::io::{self, Read, Write};
#[cfg(any(has_bundled_cli, test))]
use std::path::Path;
use std::path::PathBuf;
use std::sync::OnceLock;
#[cfg(has_bundled_cli)]
use tracing::{info, warn};
// When the SDK is built with COPILOT_CLI_VERSION set, build.rs generates
// bundled_cli.rs with the compressed binary bytes, hash, and version.
#[cfg(has_bundled_cli)]
mod build_time {
include!(concat!(env!("OUT_DIR"), "/bundled_cli.rs"));
}
static INSTALLED_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
/// Returns the bundled CLI version string, if one was embedded at build time.
pub fn bundled_version() -> Option<&'static str> {
#[cfg(has_bundled_cli)]
{
Some(build_time::CLI_VERSION)
}
#[cfg(not(has_bundled_cli))]
{
None
}
}
/// Returns the path to the installed CLI binary, lazily extracting on first call.
///
/// When the SDK was built with `COPILOT_CLI_VERSION` set, this extracts the
/// embedded binary to `~/.cache/github-copilot-sdk-{version}/copilot` (or
/// `copilot.exe` on Windows), verifies the SHA-256 hash, and returns the
/// path. Subsequent calls return the cached result.
///
/// Returns `None` if no CLI was embedded at build time.
pub fn path() -> Option<PathBuf> {
INSTALLED_PATH
.get_or_init(|| {
#[cfg(has_bundled_cli)]
{
match install(
build_time::CLI_BYTES,
build_time::CLI_HASH,
build_time::CLI_VERSION,
) {
Ok(path) => {
info!(path = %path.display(), version = build_time::CLI_VERSION, "embedded CLI installed");
return Some(path);
}
Err(e) => {
warn!(error = %e, "embedded CLI installation failed");
}
}
}
None
})
.clone()
}
#[cfg(has_bundled_cli)]
fn install(
compressed: &[u8],
expected_hash: [u8; 32],
version: &str,
) -> Result<PathBuf, EmbeddedCliError> {
let verbose = std::env::var("COPILOT_CLI_INSTALL_VERBOSE").ok().as_deref() == Some("1");
let cache = dirs::cache_dir().unwrap_or_else(std::env::temp_dir);
// Use a versioned directory so multiple versions can coexist,
// but keep the binary named `copilot` — the CLI checks argv[0]
// for this exact name.
let install_dir = if version.is_empty() {
cache.join("github-copilot-sdk")
} else {
cache.join(format!("github-copilot-sdk-{}", sanitize_version(version)))
};
fs::create_dir_all(&install_dir).map_err(EmbeddedCliError::CreateDir)?;
let binary_name = binary_name();
let final_path = install_dir.join(&binary_name);
// If the binary already exists and hash matches, skip extraction.
if final_path.is_file() {
let existing_hash = hash_file(&final_path)?;
if existing_hash == expected_hash {
if verbose {
eprintln!("embedded CLI already installed at {}", final_path.display());
}
return Ok(final_path);
}
if verbose {
eprintln!("embedded CLI hash mismatch, reinstalling");
}
}
let start = std::time::Instant::now();
let decompressed = decompress(compressed)?;
let actual_hash = sha256(&decompressed);
if actual_hash != expected_hash {
return Err(EmbeddedCliError::HashMismatch);
}
write_binary(&final_path, &decompressed)?;
if verbose {
eprintln!(
"embedded CLI installed at {} in {:?}",
final_path.display(),
start.elapsed()
);
}
Ok(final_path)
}
#[cfg(any(has_bundled_cli, test))]
fn binary_name() -> String {
if cfg!(target_os = "windows") {
"copilot.exe".to_string()
} else {
"copilot".to_string()
}
}
#[cfg(has_bundled_cli)]
fn sanitize_version(version: &str) -> String {
version
.chars()
.map(|c| match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '.' | '-' | '_' => c,
_ => '_',
})
.collect()
}
#[cfg(any(has_bundled_cli, test))]
fn decompress(data: &[u8]) -> Result<Vec<u8>, EmbeddedCliError> {
let mut decoder = zstd::Decoder::new(data).map_err(EmbeddedCliError::Decompress)?;
let mut out = Vec::new();
decoder
.read_to_end(&mut out)
.map_err(EmbeddedCliError::Decompress)?;
Ok(out)
}
#[cfg(any(has_bundled_cli, test))]
fn sha256(data: &[u8]) -> [u8; 32] {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(data);
hasher.finalize().into()
}
#[cfg(has_bundled_cli)]
fn hash_file(path: &Path) -> Result<[u8; 32], EmbeddedCliError> {
use sha2::Digest;
let mut file = fs::File::open(path).map_err(EmbeddedCliError::Io)?;
let mut hasher = sha2::Sha256::new();
let mut buf = [0u8; 8192];
loop {
let n = file.read(&mut buf).map_err(EmbeddedCliError::Io)?;
if n == 0 {
break;
}
hasher.update(&buf[..n]);
}
Ok(hasher.finalize().into())
}
#[cfg(any(has_bundled_cli, test))]
fn write_binary(path: &Path, data: &[u8]) -> Result<(), EmbeddedCliError> {
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.map_err(EmbeddedCliError::Io)?;
file.write_all(data).map_err(EmbeddedCliError::Io)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(0o755))
.map_err(EmbeddedCliError::Io)?;
}
Ok(())
}
#[cfg(any(has_bundled_cli, test))]
#[derive(Debug, thiserror::Error)]
#[allow(dead_code)]
enum EmbeddedCliError {
#[error("failed to create install directory: {0}")]
CreateDir(io::Error),
#[error("decompression failed: {0}")]
Decompress(io::Error),
#[error("SHA-256 hash of decompressed binary does not match expected hash")]
HashMismatch,
#[error("I/O error: {0}")]
Io(io::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn install_extracts_to_cache_dir() {
let temp = tempfile::tempdir().expect("should create temp dir");
let original = b"fake copilot binary";
let hash = sha256(original);
let compressed = zstd::encode_all(&original[..], 3).expect("compression should succeed");
// Override cache dir via env for test isolation.
let path = install_to_dir(&temp, &compressed, hash);
let expected_name = binary_name();
assert!(path.is_file());
assert_eq!(
path.file_name().and_then(|s| s.to_str()),
Some(expected_name.as_str())
);
let installed_content = fs::read(&path).expect("should read installed binary");
assert_eq!(installed_content, original);
// Second install should be idempotent (hash matches, skips extraction).
let path2 = install_to_dir(&temp, &compressed, hash);
assert_eq!(path, path2);
}
#[test]
fn install_rejects_hash_mismatch() {
let temp = tempfile::tempdir().expect("should create temp dir");
let original = b"fake copilot binary";
let wrong_hash = [0u8; 32];
let compressed = zstd::encode_all(&original[..], 3).expect("compression should succeed");
let result = install_to_dir_result(&temp, &compressed, wrong_hash);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("SHA-256"),);
}
// Test helpers that install to a specific directory instead of the global cache.
fn install_to_dir(temp: &tempfile::TempDir, compressed: &[u8], hash: [u8; 32]) -> PathBuf {
install_to_dir_result(temp, compressed, hash).expect("install should succeed")
}
fn install_to_dir_result(
temp: &tempfile::TempDir,
compressed: &[u8],
hash: [u8; 32],
) -> Result<PathBuf, EmbeddedCliError> {
let install_dir = temp.path().to_path_buf();
fs::create_dir_all(&install_dir).expect("create dir");
let binary_name = binary_name();
let final_path = install_dir.join(&binary_name);
let decompressed = decompress(compressed)?;
let actual_hash = sha256(&decompressed);
if actual_hash != hash {
return Err(EmbeddedCliError::HashMismatch);
}
write_binary(&final_path, &decompressed)?;
Ok(final_path)
}
}