Add Attachment struct, new iface for create-rel
The Attachment struct exists, but this makes it glaringly obvious that I've made a bad interface. The create_release_attachment should only accept one file at a time and the loop over all files should happen in main.rs I've changed the signature, removed the loops, and wired in the newer error handling routines. Needs fixing at call sites.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use crate::ApiError;
|
use crate::structs::Attachment;
|
||||||
|
|
||||||
pub fn check_release_match_repo() {}
|
pub fn check_release_match_repo() {}
|
||||||
pub fn get_release_attachment() {}
|
pub fn get_release_attachment() {}
|
||||||
@@ -12,23 +12,19 @@ pub async fn create_release_attachment(
|
|||||||
gitea_url: &str,
|
gitea_url: &str,
|
||||||
repo: &str,
|
repo: &str,
|
||||||
release_id: usize,
|
release_id: usize,
|
||||||
files: Vec<String>,
|
file: String,
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<Attachment> {
|
||||||
let request_url = format!("{gitea_url}/api/v1/repos/{repo}/releases/{release_id}/assets");
|
let request_url = format!("{gitea_url}/api/v1/repos/{repo}/releases/{release_id}/assets");
|
||||||
|
|
||||||
// Ensure all files exists before starting the uploads
|
match fs::exists(&file) {
|
||||||
for file in &files {
|
Ok(true) => (),
|
||||||
match fs::exists(file) {
|
|
||||||
Ok(true) => continue,
|
|
||||||
Ok(false) => return Err(crate::Error::NoSuchFile),
|
Ok(false) => return Err(crate::Error::NoSuchFile),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Uh oh! The file-exists check couldn't be done: {e}");
|
eprintln!("Uh oh! The file-exists check couldn't be done: {e}");
|
||||||
panic!("TODO: Deal with scenario where the file's existence cannot be checked (e.g.: no permission)");
|
panic!("TODO: Deal with scenario where the file's existence cannot be checked (e.g.: no permission)");
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for file in files {
|
|
||||||
println!("Uploading file {}", &file);
|
println!("Uploading file {}", &file);
|
||||||
let data = reqwest::multipart::Part::stream(fs::read(&file).unwrap())
|
let data = reqwest::multipart::Part::stream(fs::read(&file).unwrap())
|
||||||
.file_name("attachment")
|
.file_name("attachment")
|
||||||
@@ -44,12 +40,16 @@ pub async fn create_release_attachment(
|
|||||||
.await?;
|
.await?;
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
// TODO: create a struct Attachment and return it to the caller.
|
// TODO: create a struct Attachment and return it to the caller.
|
||||||
|
let attachment_desc = response
|
||||||
|
.json::<Attachment>()
|
||||||
|
.await
|
||||||
|
.map_err(|e| crate::Error::from(e))?;
|
||||||
|
return Ok(attachment_desc);
|
||||||
} else if response.status().is_client_error() {
|
} else if response.status().is_client_error() {
|
||||||
let mesg = crate::decode_client_error(response).await?;
|
let mesg = crate::decode_client_error(response).await?;
|
||||||
return Err(crate::Error::ApiErrorMessage(mesg));
|
return Err(crate::Error::ApiErrorMessage(mesg));
|
||||||
}
|
}
|
||||||
}
|
panic!("Reached end of release_attachment without matching a return path");
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
pub fn edit_release_attachment() {}
|
pub fn edit_release_attachment() {}
|
||||||
pub fn delete_release_attachment() {}
|
pub fn delete_release_attachment() {}
|
||||||
|
|||||||
@@ -1,2 +1,15 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod release;
|
pub mod release;
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Attachment {
|
||||||
|
id: usize,
|
||||||
|
name: String,
|
||||||
|
size: i64,
|
||||||
|
download_count: i64,
|
||||||
|
created: String, // TODO: Date-time struct
|
||||||
|
uuid: String,
|
||||||
|
download_url: String,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user