|
1
|
use { |
|
2
|
crate::{ |
|
3
|
cli::RepoCommand, |
|
4
|
context::Ctx, |
|
5
|
credential, |
|
6
|
git::RepoSlug, |
|
7
|
ui::{self, prompt}, |
|
8
|
}, |
|
9
|
anyhow::{Context, Result}, |
|
10
|
sigit_si_api::NewRepository, |
|
11
|
}; |
|
12
|
|
|
13
|
pub async fn run(ctx: &Ctx, command: RepoCommand) -> Result<()> { |
|
14
|
// A public repository clones without signing in. |
|
15
|
if let RepoCommand::Clone { |
|
16
|
repo, |
|
17
|
dir, |
|
18
|
git_args, |
|
19
|
} = command |
|
20
|
{ |
|
21
|
return clone(ctx, &repo, dir.as_deref(), &git_args); |
|
22
|
} |
|
23
|
|
|
24
|
ctx.require_token()?; |
|
25
|
|
|
26
|
match command { |
|
27
|
RepoCommand::List => list(ctx).await, |
|
28
|
RepoCommand::Search { query, limit } => search(ctx, query, limit).await, |
|
29
|
RepoCommand::View { repo } => view(ctx, repo).await, |
|
30
|
RepoCommand::Create { |
|
31
|
name, |
|
32
|
private, |
|
33
|
description, |
|
34
|
yes, |
|
35
|
} => create(ctx, name, private, description, yes).await, |
|
36
|
RepoCommand::Browse { repo } => browse(ctx, repo).await, |
|
37
|
RepoCommand::Clone { .. } => unreachable!("handled before the token check"), |
|
38
|
} |
|
39
|
} |
|
40
|
|
|
41
|
fn clone(ctx: &Ctx, repo: &str, dir: Option<&str>, git_args: &[String]) -> Result<()> { |
|
42
|
let slug = RepoSlug::parse(repo)?; |
|
43
|
let exe = std::env::current_exe().context("Could not locate the `si` executable.")?; |
|
44
|
let args = credential::clone_args(ctx.client.base_url(), &exe, &slug, dir, git_args); |
|
45
|
credential::clone(&args) |
|
46
|
} |
|
47
|
|
|
48
|
async fn list(ctx: &Ctx) -> Result<()> { |
|
49
|
let repos = ctx.client.repos().await?; |
|
50
|
ui::emit(ctx.json, &repos, |repos| ui::render::repos(repos)) |
|
51
|
} |
|
52
|
|
|
53
|
async fn search(ctx: &Ctx, query: Option<String>, limit: u32) -> Result<()> { |
|
54
|
let repos = ctx |
|
55
|
.client |
|
56
|
.search_repos(query.as_deref(), Some(limit)) |
|
57
|
.await?; |
|
58
|
ui::emit(ctx.json, &repos, |repos| ui::render::repo_summaries(repos)) |
|
59
|
} |
|
60
|
|
|
61
|
async fn view(ctx: &Ctx, repo: Option<String>) -> Result<()> { |
|
62
|
let slug = ctx.resolve_repo(repo)?; |
|
63
|
|
|
64
|
// `GET /api/v1/repos` only lists the signed-in user's own repos, but |
|
65
|
// `view` should also work on repos owned by someone else — so try the |
|
66
|
// owned list first (it has the fuller `Repository` shape) and fall back |
|
67
|
// to the cross-user MCP search. |
|
68
|
if let Some(repo) = ctx |
|
69
|
.client |
|
70
|
.repos() |
|
71
|
.await? |
|
72
|
.into_iter() |
|
73
|
.find(|r| r.full_name.eq_ignore_ascii_case(&slug.to_string())) |
|
74
|
{ |
|
75
|
return ui::emit(ctx.json, &repo, ui::render::repo_detail); |
|
76
|
} |
|
77
|
|
|
78
|
let matches = ctx.client.search_repos(Some(&slug.name), None).await?; |
|
79
|
let summary = matches |
|
80
|
.into_iter() |
|
81
|
.find(|r| r.full_name.eq_ignore_ascii_case(&slug.to_string())) |
|
82
|
.ok_or_else(|| anyhow::anyhow!("Repository {slug} was not found."))?; |
|
83
|
ui::emit(ctx.json, &summary, |summary| { |
|
84
|
ui::render::repo_summaries(std::slice::from_ref(summary)) |
|
85
|
}) |
|
86
|
} |
|
87
|
|
|
88
|
async fn create( |
|
89
|
ctx: &Ctx, |
|
90
|
name: String, |
|
91
|
private: bool, |
|
92
|
description: Option<String>, |
|
93
|
yes: bool, |
|
94
|
) -> Result<()> { |
|
95
|
if !yes { |
|
96
|
let confirmed = prompt::confirm( |
|
97
|
&format!( |
|
98
|
"Create {} repository {}?", |
|
99
|
if private { "private" } else { "public" }, |
|
100
|
ui::highlight(&name) |
|
101
|
), |
|
102
|
true, |
|
103
|
)?; |
|
104
|
if !confirmed { |
|
105
|
ui::note("Cancelled."); |
|
106
|
return Ok(()); |
|
107
|
} |
|
108
|
} |
|
109
|
|
|
110
|
let new_repo = NewRepository::new(&name) |
|
111
|
.private(private) |
|
112
|
.description(description); |
|
113
|
let repo = ctx.client.create_repo(&new_repo).await?; |
|
114
|
|
|
115
|
ui::emit(ctx.json, &repo, |repo| { |
|
116
|
ui::success(format!("Created {}.", ui::highlight(&repo.full_name))); |
|
117
|
println!(" {}", repo.clone_url); |
|
118
|
}) |
|
119
|
} |
|
120
|
|
|
121
|
async fn browse(ctx: &Ctx, repo: Option<String>) -> Result<()> { |
|
122
|
let slug = ctx.resolve_repo(repo)?; |
|
123
|
let url = format!("{}/{slug}", ctx.client.base_url()); |
|
124
|
open::that(&url)?; |
|
125
|
ui::note(format!("Opened {url}")); |
|
126
|
Ok(()) |
|
127
|
} |