79 lines
2.2 KiB
Rust
79 lines
2.2 KiB
Rust
|
|
// Simplify build
|
||
|
|
// -
|
||
|
|
|
||
|
|
use std::fs::File;
|
||
|
|
|
||
|
|
use chrono::{DateTime, Utc};
|
||
|
|
use rayon::iter::{ParallelBridge, ParallelIterator};
|
||
|
|
use tar::Archive;
|
||
|
|
|
||
|
|
use crate::recipe_functions::common;
|
||
|
|
|
||
|
|
pub trait Peekable {
|
||
|
|
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>>;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Is expected to get information about last build
|
||
|
|
pub struct LastBuildFirmware {
|
||
|
|
pub path: String,
|
||
|
|
pub date: DateTime<Utc>,
|
||
|
|
pub is_full: bool,
|
||
|
|
/// Should be added later by calling implemented functions
|
||
|
|
pub entries: Vec<String>,
|
||
|
|
pub tags: Option<Vec<String>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Peekable for LastBuildFirmware {
|
||
|
|
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||
|
|
let mut peeked = Vec::new();
|
||
|
|
|
||
|
|
let file = File::open(self.path).unwrap();
|
||
|
|
let mut ar = Archive::new(file);
|
||
|
|
|
||
|
|
for file2 in ar.entries().unwrap() {
|
||
|
|
let mut f = file2.unwrap();
|
||
|
|
peeked.push(f.path().unwrap().file_name().unwrap().to_str().unwrap());
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(peeked)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn read_last_build_firmwares() -> Result<Vec<LastBuildFirmware>, Box<dyn std::error::Error>> {
|
||
|
|
let mut result = Vec::new();
|
||
|
|
|
||
|
|
// get config
|
||
|
|
let ucfg = common::get_config();
|
||
|
|
let firmware_dir = ucfg
|
||
|
|
.get("FIRMWARE_DIR")
|
||
|
|
.expect("Unknown firmware directory, check .tbcfg");
|
||
|
|
|
||
|
|
// parallel walk to dir
|
||
|
|
walkdir::WalkDir::new(firmware_dir)
|
||
|
|
.max_depth(2)
|
||
|
|
.into_iter()
|
||
|
|
.par_bridge()
|
||
|
|
.filter(|w| {
|
||
|
|
let filename = w.ok().unwrap().file_name().to_str().unwrap();
|
||
|
|
filename.ends_with(".tar") || filename.ends_with(".zip")
|
||
|
|
})
|
||
|
|
.for_each(|entry| {
|
||
|
|
let dir = entry?;
|
||
|
|
let meta = dir.clone().metadata()?;
|
||
|
|
|
||
|
|
let filename = dir.clone().file_name().to_str().unwrap();
|
||
|
|
let mod_time: DateTime<Utc> = meta.modified()?.into();
|
||
|
|
let full_firmware = filename.clone().contains("full");
|
||
|
|
|
||
|
|
result.push(LastBuildFirmware {
|
||
|
|
path: filename.clone(),
|
||
|
|
date: mod_time,
|
||
|
|
is_full: full_firmware,
|
||
|
|
entries: Vec::new(),
|
||
|
|
tags: None,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
Ok(result)
|
||
|
|
}
|