vibe-workspace 0.0.12

Extremely lightweight CLI for managing multiple git repositories and workspace configurations
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use anyhow::{Context, Result};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio::process::Command;

use crate::git::{GitError, Repository, SearchQuery};
use crate::utils::git::is_github_cli_available;

use super::SearchProvider;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubCliConfig {
    pub executable_path: Option<PathBuf>,
    pub default_limit: usize,
    pub include_forks: bool,
}

impl Default for GitHubCliConfig {
    fn default() -> Self {
        Self {
            executable_path: None,
            default_limit: 20,
            include_forks: false,
        }
    }
}

pub struct GitHubCliProvider {
    gh_path: PathBuf,
    config: GitHubCliConfig,
}

impl GitHubCliProvider {
    pub fn new() -> Result<Self> {
        Self::with_config(GitHubCliConfig::default())
    }

    pub fn with_config(config: GitHubCliConfig) -> Result<Self> {
        if !is_github_cli_available() {
            return Err(GitError::GitHubCliNotFound.into());
        }

        let gh_path = config
            .executable_path
            .clone()
            .unwrap_or_else(|| PathBuf::from("gh"));

        Ok(Self { gh_path, config })
    }

    /// Get the currently authenticated GitHub username
    pub async fn get_username(&self) -> Result<String> {
        let output = Command::new(&self.gh_path)
            .args(["api", "user", "--jq", ".login"])
            .output()
            .await
            .context("Failed to get GitHub username")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!("Failed to get GitHub username: {}", error_msg);
        }

        let username = String::from_utf8(output.stdout)
            .context("Invalid UTF-8 in username response")?
            .trim()
            .to_string();

        if username.is_empty() {
            anyhow::bail!("No GitHub username found. Please authenticate with 'gh auth login'");
        }

        Ok(username)
    }

    /// Get the organizations the authenticated user belongs to
    pub async fn get_user_organizations(&self) -> Result<Vec<String>> {
        let output = Command::new(&self.gh_path)
            .args(["api", "user/orgs", "--jq", ".[].login"])
            .output()
            .await
            .context("Failed to get GitHub organizations")?;

        if !output.status.success() {
            // Organizations query might fail if user has no orgs, which is fine
            return Ok(Vec::new());
        }

        let orgs_output =
            String::from_utf8(output.stdout).context("Invalid UTF-8 in organizations response")?;

        let organizations = orgs_output
            .lines()
            .filter(|line| !line.trim().is_empty())
            .map(|login| login.trim().to_string())
            .collect();

        Ok(organizations)
    }

    /// Check if a repository exists for the given owner and name
    pub async fn repository_exists(&self, owner: &str, repo_name: &str) -> Result<bool> {
        let output = Command::new(&self.gh_path)
            .args(["api", &format!("repos/{owner}/{repo_name}")])
            .output()
            .await
            .context("Failed to check repository existence")?;

        // If the repository exists, the command will succeed
        // If it doesn't exist, it will fail with 404
        Ok(output.status.success())
    }

    /// Get all repositories for a specific user
    pub async fn get_user_repositories(&self, username: &str) -> Result<Vec<Repository>> {
        let output = Command::new(&self.gh_path)
            .args([
                "api",
                &format!("users/{}/repos", username),
                "--paginate",
                "--jq",
                r#".[] | {
                    fullName: .full_name,
                    name: .name,
                    description: .description,
                    url: .clone_url,
                    sshUrl: .ssh_url,
                    stargazersCount: .stargazers_count,
                    language: .language,
                    fork: .fork,
                    archived: .archived,
                    topics: .topics
                }"#,
            ])
            .output()
            .await
            .context("Failed to get user repositories")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!(
                "Failed to get user repositories for '{}': {}",
                username,
                error_msg
            );
        }

        self.parse_repository_list(&output.stdout).await
    }

    /// Get all repositories for an organization
    pub async fn get_organization_repositories(&self, org: &str) -> Result<Vec<Repository>> {
        let output = Command::new(&self.gh_path)
            .args([
                "api",
                &format!("orgs/{}/repos", org),
                "--paginate",
                "--jq",
                r#".[] | {
                    fullName: .full_name,
                    name: .name,
                    description: .description,
                    url: .clone_url,
                    sshUrl: .ssh_url,
                    stargazersCount: .stargazers_count,
                    language: .language,
                    fork: .fork,
                    archived: .archived,
                    topics: .topics
                }"#,
            ])
            .output()
            .await
            .context("Failed to get organization repositories")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!(
                "Failed to get organization repositories for '{}': {}",
                org,
                error_msg
            );
        }

        self.parse_repository_list(&output.stdout).await
    }

    /// Check if a target exists as either a user or organization
    pub async fn user_or_org_exists(&self, target: &str) -> Result<bool> {
        // Try user first
        let user_check = Command::new(&self.gh_path)
            .args(["api", &format!("users/{}", target)])
            .output()
            .await
            .context("Failed to check user existence")?;

        if user_check.status.success() {
            return Ok(true);
        }

        // Try organization
        let org_check = Command::new(&self.gh_path)
            .args(["api", &format!("orgs/{}", target)])
            .output()
            .await
            .context("Failed to check organization existence")?;

        Ok(org_check.status.success())
    }

    /// Get the target type (user or organization)
    pub async fn get_target_type(
        &self,
        target: &str,
    ) -> Result<crate::git::bulk_clone::TargetType> {
        // Try organization first (more likely to have multiple repos)
        let org_check = Command::new(&self.gh_path)
            .args(["api", &format!("orgs/{}", target)])
            .output()
            .await
            .context("Failed to check organization")?;

        if org_check.status.success() {
            return Ok(crate::git::bulk_clone::TargetType::Organization);
        }

        // Try user
        let user_check = Command::new(&self.gh_path)
            .args(["api", &format!("users/{}", target)])
            .output()
            .await
            .context("Failed to check user")?;

        if user_check.status.success() {
            Ok(crate::git::bulk_clone::TargetType::User)
        } else {
            Ok(crate::git::bulk_clone::TargetType::Unknown)
        }
    }

    /// Count repositories for a user or organization (quick check)
    pub async fn count_repositories(&self, target: &str) -> Result<usize> {
        // Try as organization first
        match self.count_organization_repositories(target).await {
            Ok(count) => Ok(count),
            Err(_) => {
                // Try as user
                self.count_user_repositories(target).await
            }
        }
    }

    /// Count repositories for a user
    async fn count_user_repositories(&self, username: &str) -> Result<usize> {
        let output = Command::new(&self.gh_path)
            .args([
                "api",
                &format!("users/{}/repos", username),
                "--jq",
                "length",
            ])
            .output()
            .await
            .context("Failed to count user repositories")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!(
                "Failed to count repositories for user '{}': {}",
                username,
                error_msg
            );
        }

        let count_str = String::from_utf8(output.stdout)
            .context("Invalid UTF-8 in count response")?
            .trim()
            .to_string();

        count_str
            .parse::<usize>()
            .context("Failed to parse repository count")
    }

    /// Count repositories for an organization
    async fn count_organization_repositories(&self, org: &str) -> Result<usize> {
        let output = Command::new(&self.gh_path)
            .args(["api", &format!("orgs/{}/repos", org), "--jq", "length"])
            .output()
            .await
            .context("Failed to count organization repositories")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!(
                "Failed to count repositories for org '{}': {}",
                org,
                error_msg
            );
        }

        let count_str = String::from_utf8(output.stdout)
            .context("Invalid UTF-8 in count response")?
            .trim()
            .to_string();

        count_str
            .parse::<usize>()
            .context("Failed to parse repository count")
    }

    async fn parse_repository_list(&self, output: &[u8]) -> Result<Vec<Repository>> {
        #[derive(Deserialize)]
        struct RepoData {
            #[serde(rename = "fullName")]
            full_name: String,
            name: String,
            description: Option<String>,
            url: String,
            #[serde(rename = "sshUrl")]
            ssh_url: String,
            #[serde(rename = "stargazersCount")]
            stars: u32,
            language: Option<String>,
            #[serde(default)]
            #[allow(dead_code)]
            fork: bool,
            #[serde(default)]
            #[allow(dead_code)]
            archived: bool,
            #[serde(default)]
            topics: Vec<String>,
        }

        let output_str = String::from_utf8_lossy(output);
        if output_str.trim().is_empty() {
            return Ok(Vec::new());
        }

        let mut repositories = Vec::new();

        // Parse line by line since GitHub CLI outputs one JSON object per line
        for line in output_str.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let repo_data: RepoData = serde_json::from_str(line)
                .with_context(|| format!("Failed to parse repository data: {}", line))?;

            let repository = Repository {
                id: repo_data.full_name.clone(),
                name: repo_data.name,
                full_name: repo_data.full_name,
                description: repo_data.description,
                url: repo_data.url,
                ssh_url: repo_data.ssh_url,
                stars: repo_data.stars,
                language: repo_data.language,
                license: None, // Not available in this endpoint
                topics: repo_data.topics,
            };

            repositories.push(repository);
        }

        Ok(repositories)
    }

    async fn parse_search_results(&self, output: &[u8]) -> Result<Vec<Repository>> {
        #[derive(Deserialize)]
        struct SearchResult {
            #[serde(rename = "fullName")]
            full_name: String,
            name: Option<String>,
            description: Option<String>,
            url: String,
            #[serde(rename = "stargazersCount")]
            stars: u32,
            language: Option<String>,
            license: Option<LicenseInfo>,
            #[serde(default)]
            #[allow(dead_code)]
            visibility: Option<String>,
        }

        #[derive(Deserialize)]
        struct LicenseInfo {
            key: String,
            #[allow(dead_code)]
            name: Option<String>,
        }

        let results: Vec<SearchResult> = serde_json::from_slice(output).with_context(|| {
            format!(
                "Failed to parse search results. Raw output: {}",
                String::from_utf8_lossy(output)
            )
        })?;

        let mut repositories = Vec::new();

        for result in results {
            let name = result.name.unwrap_or_else(|| {
                result
                    .full_name
                    .split('/')
                    .next_back()
                    .unwrap_or(&result.full_name)
                    .to_string()
            });

            let repo =
                Repository {
                    id: result.full_name.clone(),
                    name,
                    full_name: result.full_name.clone(),
                    description: result.description,
                    url: result.url,
                    ssh_url: format!("git@github.com:{}.git", result.full_name),
                    stars: result.stars,
                    language: result.language,
                    license: result.license.and_then(|l| {
                        if l.key.is_empty() {
                            None
                        } else {
                            Some(l.key)
                        }
                    }),
                    topics: vec![], // Topics not returned in search results
                };

            repositories.push(repo);
        }

        Ok(repositories)
    }

    async fn get_repo_details(&self, repo_name: &str) -> Result<Repository> {
        let output = Command::new(&self.gh_path)
            .args([
                "repo",
                "view",
                repo_name,
                "--json",
                "name,description,url,sshUrl,stargazerCount,primaryLanguage,repositoryTopics",
            ])
            .output()
            .await
            .context("Failed to execute gh repo view")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!("Failed to get repository details: {}", error_msg);
        }

        #[derive(Deserialize)]
        struct RepoDetails {
            name: String,
            description: Option<String>,
            url: String,
            #[serde(rename = "sshUrl")]
            ssh_url: String,
            #[serde(rename = "stargazerCount", default)]
            stars: u32,
            #[serde(rename = "primaryLanguage")]
            primary_language: Option<LanguageInfo>,
            #[serde(rename = "repositoryTopics", default)]
            topics: Vec<String>,
        }

        #[derive(Deserialize)]
        struct LanguageInfo {
            name: String,
        }

        let details: RepoDetails =
            serde_json::from_slice(&output.stdout).context("Failed to parse repository details")?;

        Ok(Repository {
            id: repo_name.to_string(),
            name: details.name,
            full_name: repo_name.to_string(),
            description: details.description,
            url: details.url,
            ssh_url: details.ssh_url,
            stars: details.stars,
            language: details.primary_language.map(|l| l.name),
            license: None, // License not available in repo view, only in search
            topics: details.topics,
        })
    }
}

#[async_trait]
impl SearchProvider for GitHubCliProvider {
    async fn search(&self, query: &SearchQuery) -> Result<Vec<Repository>> {
        let mut cmd = Command::new(&self.gh_path);
        cmd.args(["search", "repos"]);

        // Build search string
        let mut search_parts = query.keywords.clone();

        if let Some(org) = &query.organization {
            search_parts.push(format!("org:{org}"));
        }

        if let Some(lang) = &query.language {
            search_parts.push(format!("language:{lang}"));
        }

        for tag in &query.tags {
            search_parts.push(format!("topic:{tag}"));
        }

        if !self.config.include_forks {
            search_parts.push("fork:false".to_string());
        }

        let search_string = search_parts.join(" ");
        cmd.arg(&search_string);

        // Add sort method (only if not best-match, which is the default)
        if query.sort != crate::git::SortMethod::BestMatch {
            cmd.args(["--sort", query.sort.as_str()]);
        }

        // Add limit and request JSON output
        let limit = query.limit.unwrap_or(self.config.default_limit);
        cmd.args(["--limit", &limit.to_string()]);
        cmd.args([
            "--json",
            "fullName,name,description,url,stargazersCount,language,license",
        ]);

        let output = cmd
            .output()
            .await
            .context("Failed to execute gh search repos")?;

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            if error_msg.contains("No repositories matched") {
                return Err(GitError::NoSearchResults {
                    query: search_string,
                }
                .into());
            }
            anyhow::bail!("GitHub CLI search failed: {}", error_msg);
        }

        self.parse_search_results(&output.stdout).await
    }

    async fn get_repository(&self, id: &str) -> Result<Repository> {
        self.get_repo_details(id).await
    }

    fn name(&self) -> &str {
        "github_cli"
    }
}