|
1
|
use { |
|
2
|
crate::{cli::CodeCommand, context::Ctx, ui}, |
|
3
|
anyhow::Result, |
|
4
|
}; |
|
5
|
|
|
6
|
pub async fn run(ctx: &Ctx, command: CodeCommand) -> Result<()> { |
|
7
|
ctx.require_token()?; |
|
8
|
|
|
9
|
match command { |
|
10
|
CodeCommand::View { path, r#ref, repo } => view(ctx, path, r#ref, repo).await, |
|
11
|
CodeCommand::Search { |
|
12
|
query, |
|
13
|
r#ref, |
|
14
|
limit, |
|
15
|
repo, |
|
16
|
} => search(ctx, query, r#ref, limit, repo).await, |
|
17
|
} |
|
18
|
} |
|
19
|
|
|
20
|
async fn view( |
|
21
|
ctx: &Ctx, |
|
22
|
path: String, |
|
23
|
git_ref: Option<String>, |
|
24
|
repo: Option<String>, |
|
25
|
) -> Result<()> { |
|
26
|
let slug = ctx.resolve_repo(repo)?; |
|
27
|
let file = ctx |
|
28
|
.client |
|
29
|
.file_contents(&slug.to_string(), &path, git_ref.as_deref()) |
|
30
|
.await?; |
|
31
|
|
|
32
|
if ctx.json { |
|
33
|
println!("{}", serde_json::to_string_pretty(&file)?); |
|
34
|
} else { |
|
35
|
print!("{}", file.content); |
|
36
|
} |
|
37
|
Ok(()) |
|
38
|
} |
|
39
|
|
|
40
|
async fn search( |
|
41
|
ctx: &Ctx, |
|
42
|
query: String, |
|
43
|
git_ref: Option<String>, |
|
44
|
limit: u32, |
|
45
|
repo: Option<String>, |
|
46
|
) -> Result<()> { |
|
47
|
let slug = ctx.resolve_repo(repo)?; |
|
48
|
let hits = ctx |
|
49
|
.client |
|
50
|
.search_code(&slug.to_string(), &query, git_ref.as_deref(), Some(limit)) |
|
51
|
.await?; |
|
52
|
ui::emit(ctx.json, &hits, |hits| ui::render::code_hits(hits)) |
|
53
|
} |