-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathgithub_recipe.rs
More file actions
403 lines (347 loc) · 12.9 KB
/
github_recipe.rs
File metadata and controls
403 lines (347 loc) · 12.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use anyhow::{anyhow, Result};
use console::style;
use goose::recipe::template_recipe::parse_recipe_content;
use goose::recipe::RECIPE_FILE_EXTENSIONS;
use serde::{Deserialize, Serialize};
use goose::recipe::read_recipe_file_content::RecipeFile;
use goose::subprocess::SubprocessExt;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use tar::Archive;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecipeInfo {
pub name: String,
pub source: RecipeSource,
pub path: String,
pub title: Option<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RecipeSource {
Local,
GitHub,
}
pub const GOOSE_RECIPE_GITHUB_REPO_CONFIG_KEY: &str = "GOOSE_RECIPE_GITHUB_REPO";
pub fn retrieve_recipe_from_github(
recipe_name: &str,
recipe_repo_full_name: &str,
) -> Result<RecipeFile> {
println!(
"📦 Looking for recipe \"{}\" in github repo: {}",
recipe_name, recipe_repo_full_name
);
ensure_gh_authenticated()?;
let max_attempts = 2;
let mut last_err = None;
for attempt in 1..=max_attempts {
match clone_and_download_recipe(recipe_name, recipe_repo_full_name) {
Ok(download_dir) => match read_recipe_file(&download_dir) {
Ok((content, recipe_file_local_path)) => {
return Ok(RecipeFile {
content,
parent_dir: download_dir.clone(),
file_path: recipe_file_local_path,
})
}
Err(err) => return Err(err),
},
Err(err) => {
last_err = Some(err);
}
}
if attempt < max_attempts {
clean_cloned_dirs(recipe_repo_full_name)?;
}
}
Err(last_err.unwrap_or_else(|| anyhow::anyhow!("Unknown error occurred")))
}
fn clean_cloned_dirs(recipe_repo_full_name: &str) -> anyhow::Result<()> {
let local_repo_path = get_local_repo_path(&env::temp_dir(), recipe_repo_full_name)?;
if local_repo_path.exists() {
fs::remove_dir_all(&local_repo_path)?;
}
Ok(())
}
fn read_recipe_file(download_dir: &Path) -> Result<(String, PathBuf)> {
for ext in RECIPE_FILE_EXTENSIONS {
let candidate_file_path = download_dir.join(format!("recipe.{}", ext));
if candidate_file_path.exists() {
let content = fs::read_to_string(&candidate_file_path)?;
println!(
"⬇️ Retrieved recipe file: {}",
candidate_file_path
.strip_prefix(download_dir)
.unwrap()
.display()
);
return Ok((content, candidate_file_path));
}
}
Err(anyhow::anyhow!(
"No recipe file found in {} (looked for extensions: {:?})",
download_dir.display(),
RECIPE_FILE_EXTENSIONS
))
}
fn clone_and_download_recipe(recipe_name: &str, recipe_repo_full_name: &str) -> Result<PathBuf> {
let local_repo_path = ensure_repo_cloned(recipe_repo_full_name)?;
fetch_origin(&local_repo_path)?;
get_folder_from_github(&local_repo_path, recipe_name)
}
pub fn ensure_gh_authenticated() -> Result<()> {
// Check authentication status
let status = Command::new("gh")
.args(["auth", "status"])
.set_no_window()
.status()
.map_err(|_| {
anyhow::anyhow!("Failed to run `gh auth status`. Make sure you have `gh` installed.")
})?;
if status.success() {
return Ok(());
}
println!("GitHub CLI is not authenticated. Launching `gh auth login`...");
// Run `gh auth login` interactively
let login_status = Command::new("gh")
.args(["auth", "login", "--git-protocol", "https"])
.status()
.map_err(|_| anyhow::anyhow!("Failed to run `gh auth login`"))?;
if !login_status.success() {
Err(anyhow::anyhow!("Failed to authenticate using GitHub CLI."))
} else {
Ok(())
}
}
fn temp_child_name(name: &str) -> String {
let mut child = String::with_capacity(name.len());
for ch in name.chars() {
match ch {
'/' | '\\' => child.push_str("__"),
ch if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || ch == '.' => {
child.push(ch)
}
_ => child.push('_'),
}
}
if child.is_empty() {
"_".to_string()
} else {
child
}
}
fn get_local_repo_path(
local_repo_parent_path: &Path,
recipe_repo_full_name: &str,
) -> Result<PathBuf> {
let (owner, repo_name) = recipe_repo_full_name
.split_once('/')
.ok_or_else(|| anyhow::anyhow!("Invalid repository name format"))?;
let local_repo_path = local_repo_parent_path
.to_path_buf()
.join(temp_child_name(&format!("{owner}/{repo_name}")));
Ok(local_repo_path)
}
fn ensure_repo_cloned(recipe_repo_full_name: &str) -> Result<PathBuf> {
let local_repo_parent_path = env::temp_dir();
if !local_repo_parent_path.exists() {
std::fs::create_dir_all(local_repo_parent_path.clone())?;
}
let local_repo_path = get_local_repo_path(&local_repo_parent_path, recipe_repo_full_name)?;
if local_repo_path.join(".git").exists() {
Ok(local_repo_path)
} else {
let error_message: String = format!("Failed to clone repo: {}", recipe_repo_full_name);
let status = Command::new("gh")
.args(["repo", "clone", recipe_repo_full_name])
.current_dir(local_repo_parent_path.clone())
.set_no_window()
.status()
.map_err(|_: std::io::Error| anyhow::anyhow!(error_message.clone()))?;
if status.success() {
Ok(local_repo_path)
} else {
Err(anyhow::anyhow!(error_message))
}
}
}
fn fetch_origin(local_repo_path: &Path) -> Result<()> {
let error_message: String = format!("Failed to fetch at {}", local_repo_path.to_str().unwrap());
let status = Command::new("git")
.args(["fetch", "origin"])
.current_dir(local_repo_path)
.set_no_window()
.status()
.map_err(|_| anyhow::anyhow!(error_message.clone()))?;
if status.success() {
Ok(())
} else {
Err(anyhow::anyhow!(error_message))
}
}
fn get_folder_from_github(local_repo_path: &Path, recipe_name: &str) -> Result<PathBuf> {
let ref_and_path = format!("origin/main:{}", recipe_name);
let output_dir = env::temp_dir().join(temp_child_name(recipe_name));
if output_dir.exists() {
fs::remove_dir_all(&output_dir)?;
}
fs::create_dir_all(&output_dir)?;
let archive_output = Command::new("git")
.args(["archive", &ref_and_path])
.current_dir(local_repo_path)
.stdout(Stdio::piped())
.set_no_window()
.spawn()?;
let stdout = archive_output
.stdout
.ok_or_else(|| anyhow::anyhow!("Failed to capture stdout from git archive"))?;
let mut archive = Archive::new(stdout);
archive.unpack(&output_dir)?;
list_files(&output_dir)?;
Ok(output_dir)
}
fn list_files(dir: &Path) -> Result<()> {
println!("{}", style("Files downloaded from github:").bold());
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
println!(" - {}", path.display());
}
}
Ok(())
}
/// Lists all available recipes from a GitHub repository
pub fn list_github_recipes(repo: &str) -> Result<Vec<RecipeInfo>> {
discover_github_recipes(repo)
}
fn discover_github_recipes(repo: &str) -> Result<Vec<RecipeInfo>> {
use serde_json::Value;
use std::process::Command;
// Ensure GitHub CLI is authenticated
ensure_gh_authenticated()?;
// Get repository contents using GitHub CLI
let output = Command::new("gh")
.args(["api", &format!("repos/{}/contents", repo)])
.set_no_window()
.output()
.map_err(|e| anyhow!("Failed to fetch repository contents using 'gh api' command (executed when GOOSE_RECIPE_GITHUB_REPO is configured). This requires GitHub CLI (gh) to be installed and authenticated. Error: {}", e))?;
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!("GitHub API request failed: {}", error_msg));
}
let contents: Value = serde_json::from_slice(&output.stdout)
.map_err(|e| anyhow!("Failed to parse GitHub API response: {}", e))?;
let mut recipes = Vec::new();
if let Some(items) = contents.as_array() {
for item in items {
if let (Some(name), Some(item_type)) = (
item.get("name").and_then(|n| n.as_str()),
item.get("type").and_then(|t| t.as_str()),
) {
if item_type == "dir" {
// Check if this directory contains a recipe file
if let Ok(recipe_info) = check_github_directory_for_recipe(repo, name) {
recipes.push(recipe_info);
}
}
}
}
}
Ok(recipes)
}
fn check_github_directory_for_recipe(repo: &str, dir_name: &str) -> Result<RecipeInfo> {
use serde_json::Value;
use std::process::Command;
// Check directory contents for recipe files
let output = Command::new("gh")
.args(["api", &format!("repos/{}/contents/{}", repo, dir_name)])
.set_no_window()
.output()
.map_err(|e| anyhow!("Failed to check directory contents: {}", e))?;
if !output.status.success() {
return Err(anyhow!("Failed to access directory: {}", dir_name));
}
let contents: Value = serde_json::from_slice(&output.stdout)
.map_err(|e| anyhow!("Failed to parse directory contents: {}", e))?;
if let Some(items) = contents.as_array() {
for item in items {
if let Some(name) = item.get("name").and_then(|n| n.as_str()) {
if RECIPE_FILE_EXTENSIONS
.iter()
.any(|ext| name == format!("recipe.{}", ext))
{
// Found a recipe file, get its content
return get_github_recipe_info(repo, dir_name, name);
}
}
}
}
Err(anyhow!("No recipe file found in directory: {}", dir_name))
}
fn get_github_recipe_info(repo: &str, dir_name: &str, recipe_filename: &str) -> Result<RecipeInfo> {
use serde_json::Value;
use std::process::Command;
// Get the recipe file content
let output = Command::new("gh")
.args([
"api",
&format!("repos/{}/contents/{}/{}", repo, dir_name, recipe_filename),
])
.set_no_window()
.output()
.map_err(|e| anyhow!("Failed to get recipe file content: {}", e))?;
if !output.status.success() {
return Err(anyhow!(
"Failed to access recipe file: {}/{}",
dir_name,
recipe_filename
));
}
let file_info: Value = serde_json::from_slice(&output.stdout)
.map_err(|e| anyhow!("Failed to parse file info: {}", e))?;
if let Some(content_b64) = file_info.get("content").and_then(|c| c.as_str()) {
// Decode base64 content
use base64::{engine::general_purpose, Engine as _};
let content_bytes = general_purpose::STANDARD
.decode(content_b64.replace('\n', ""))
.map_err(|e| anyhow!("Failed to decode base64 content: {}", e))?;
let content = String::from_utf8(content_bytes)
.map_err(|e| anyhow!("Failed to convert content to string: {}", e))?;
// Parse the recipe content
let (recipe, _) = parse_recipe_content(&content, Some(format!("{}/{}", repo, dir_name)))?;
return Ok(RecipeInfo {
name: dir_name.to_string(),
source: RecipeSource::GitHub,
path: format!("{}/{}", repo, dir_name),
title: Some(recipe.title),
description: Some(recipe.description),
});
}
Err(anyhow!("Failed to get recipe content from GitHub"))
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn local_repo_path_includes_owner_to_avoid_collisions() {
let parent = Path::new("goose-recipes");
let first = get_local_repo_path(parent, "owner-one/shared").unwrap();
let second = get_local_repo_path(parent, "owner-two/shared").unwrap();
assert_ne!(first, second);
assert_eq!(first, parent.join("owner-one__shared"));
assert_eq!(second, parent.join("owner-two__shared"));
}
#[test]
fn temp_child_name_keeps_recipe_downloads_under_temp_dir() {
let parent = Path::new("goose-recipes");
let child = temp_child_name("../outside");
let output_dir = parent.join(&child);
assert_eq!(child, "..__outside");
assert!(output_dir.starts_with(parent));
}
}