hive-forge: rewrite bash CLI helper as a rust binary (closes #280)
This commit is contained in:
parent
560360d2e3
commit
595e3c040c
28 changed files with 1434 additions and 612 deletions
69
hive-forge/src/verbs/attach.rs
Normal file
69
hive-forge/src/verbs/attach.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
//! `attach-issue <number> <file> [repo]` and `attach-comment
|
||||
//! <comment-id> <file> [repo]` — upload a file as an attachment.
|
||||
//! Prints the browser download URL.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use clap::Args as ClapArgs;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::client::Client;
|
||||
|
||||
#[derive(ClapArgs)]
|
||||
pub struct IssueArgs {
|
||||
/// Issue number.
|
||||
number: u64,
|
||||
/// File path to upload.
|
||||
file: PathBuf,
|
||||
/// Repo override.
|
||||
repo: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(ClapArgs)]
|
||||
pub struct CommentArgs {
|
||||
/// Comment id.
|
||||
id: u64,
|
||||
/// File path to upload.
|
||||
file: PathBuf,
|
||||
/// Repo override.
|
||||
repo: Option<String>,
|
||||
}
|
||||
|
||||
pub fn run_issue(client: &Client, args: IssueArgs) -> Result<()> {
|
||||
if !args.file.is_file() {
|
||||
bail!(
|
||||
"hive-forge attach-issue: file not found: {}",
|
||||
args.file.display()
|
||||
);
|
||||
}
|
||||
let repo = client.repo(args.repo.as_deref());
|
||||
let resp = client.post_multipart_file(
|
||||
&format!("/repos/{repo}/issues/{}/assets", args.number),
|
||||
&args.file,
|
||||
)?;
|
||||
print_url(&resp);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_comment(client: &Client, args: CommentArgs) -> Result<()> {
|
||||
if !args.file.is_file() {
|
||||
bail!(
|
||||
"hive-forge attach-comment: file not found: {}",
|
||||
args.file.display()
|
||||
);
|
||||
}
|
||||
let repo = client.repo(args.repo.as_deref());
|
||||
let resp = client.post_multipart_file(
|
||||
&format!("/repos/{repo}/issues/comments/{}/assets", args.id),
|
||||
&args.file,
|
||||
)?;
|
||||
print_url(&resp);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_url(v: &Value) {
|
||||
if let Some(url) = v.get("browser_download_url").and_then(Value::as_str) {
|
||||
println!("{url}");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue