add option to get only part of output with startline
This commit is contained in:
		@@ -2,7 +2,7 @@ use crate::aur::aur::get_info_by_name;
 | 
			
		||||
use crate::builder::types::Action;
 | 
			
		||||
use crate::db::prelude::{Packages, Versions};
 | 
			
		||||
use crate::db::{packages, versions};
 | 
			
		||||
use rocket::response::status::NotFound;
 | 
			
		||||
use rocket::response::status::BadRequest;
 | 
			
		||||
use rocket::serde::json::Json;
 | 
			
		||||
use rocket::serde::Deserialize;
 | 
			
		||||
use rocket::{post, State};
 | 
			
		||||
@@ -25,14 +25,14 @@ pub async fn package_add(
 | 
			
		||||
    db: &State<DatabaseConnection>,
 | 
			
		||||
    input: Json<AddBody>,
 | 
			
		||||
    tx: &State<Sender<Action>>,
 | 
			
		||||
) -> Result<(), NotFound<String>> {
 | 
			
		||||
) -> Result<(), BadRequest<String>> {
 | 
			
		||||
    let db = db as &DatabaseConnection;
 | 
			
		||||
 | 
			
		||||
    let pkt_model = match Packages::find()
 | 
			
		||||
        .filter(packages::Column::Name.eq(input.name.clone()))
 | 
			
		||||
        .one(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|e| NotFound(e.to_string()))?
 | 
			
		||||
        .map_err(|e| BadRequest(Some(e.to_string())))?
 | 
			
		||||
    {
 | 
			
		||||
        None => {
 | 
			
		||||
            let new_package = packages::ActiveModel {
 | 
			
		||||
@@ -47,13 +47,13 @@ pub async fn package_add(
 | 
			
		||||
 | 
			
		||||
    let pkg = get_info_by_name(input.name.clone().as_str())
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|_| NotFound("couldn't download package metadata".to_string()))?;
 | 
			
		||||
        .map_err(|_| BadRequest(Some("couldn't download package metadata".to_string())))?;
 | 
			
		||||
 | 
			
		||||
    let version_model = match Versions::find()
 | 
			
		||||
        .filter(versions::Column::Version.eq(pkg.version.clone()))
 | 
			
		||||
        .one(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|e| NotFound(e.to_string()))?
 | 
			
		||||
        .map_err(|e| BadRequest(Some(e.to_string())))?
 | 
			
		||||
    {
 | 
			
		||||
        None => {
 | 
			
		||||
            let new_version = versions::ActiveModel {
 | 
			
		||||
@@ -70,7 +70,7 @@ pub async fn package_add(
 | 
			
		||||
            if input.force_build {
 | 
			
		||||
                p.into()
 | 
			
		||||
            } else {
 | 
			
		||||
                return Err(NotFound("Version already existing".to_string()));
 | 
			
		||||
                return Err(BadRequest(Some("Version already existing".to_string())));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,6 @@ pub fn build_api() -> Vec<Route> {
 | 
			
		||||
        package_del,
 | 
			
		||||
        version_del,
 | 
			
		||||
        build_output,
 | 
			
		||||
        list_builds
 | 
			
		||||
        list_builds,
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -69,10 +69,11 @@ pub async fn package_list(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[openapi(tag = "test")]
 | 
			
		||||
#[get("/builds/output?<buildid>")]
 | 
			
		||||
#[get("/builds/output?<buildid>&<startline>")]
 | 
			
		||||
pub async fn build_output(
 | 
			
		||||
    db: &State<DatabaseConnection>,
 | 
			
		||||
    buildid: i32,
 | 
			
		||||
    startline: Option<i32>,
 | 
			
		||||
) -> Result<String, NotFound<String>> {
 | 
			
		||||
    let db = db as &DatabaseConnection;
 | 
			
		||||
 | 
			
		||||
@@ -82,7 +83,32 @@ pub async fn build_output(
 | 
			
		||||
        .map_err(|e| NotFound(e.to_string()))?
 | 
			
		||||
        .ok_or(NotFound("couldn't find id".to_string()))?;
 | 
			
		||||
 | 
			
		||||
    build.ouput.ok_or(NotFound("No Output".to_string()))
 | 
			
		||||
    return match build.ouput {
 | 
			
		||||
        None => Err(NotFound("No Output".to_string())),
 | 
			
		||||
        Some(v) => match startline {
 | 
			
		||||
            None => Ok(v),
 | 
			
		||||
            Some(startline) => {
 | 
			
		||||
                let output: Vec<String> = v.split("\n").map(|x| x.to_string()).collect();
 | 
			
		||||
                let len = output.len();
 | 
			
		||||
                let len_missing = len as i32 - startline;
 | 
			
		||||
 | 
			
		||||
                let output = output
 | 
			
		||||
                    .iter()
 | 
			
		||||
                    .rev()
 | 
			
		||||
                    .take(if len_missing > 0 {
 | 
			
		||||
                        len_missing as usize
 | 
			
		||||
                    } else {
 | 
			
		||||
                        0
 | 
			
		||||
                    })
 | 
			
		||||
                    .rev()
 | 
			
		||||
                    .map(|x1| x1.clone())
 | 
			
		||||
                    .collect::<Vec<_>>();
 | 
			
		||||
 | 
			
		||||
                let output = output.join("\n");
 | 
			
		||||
                Ok(output)
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user