[fix] fix panic

This commit is contained in:
Jay 2020-11-12 17:49:18 +08:00
parent a5473f6436
commit bdf10a5176
2 changed files with 36 additions and 19 deletions

View File

@ -37,7 +37,6 @@ struct History {
created: Option<String>, created: Option<String>,
} }
impl<'a> Registry<'a> { impl<'a> Registry<'a> {
pub async fn get_repositories(&self) -> Result<RepositoryList, Box<dyn std::error::Error>> { pub async fn get_repositories(&self) -> Result<RepositoryList, Box<dyn std::error::Error>> {
let registry_url = Url::parse(self.url).unwrap(); let registry_url = Url::parse(self.url).unwrap();
@ -86,20 +85,25 @@ impl<'a> Registry<'a> {
// .unwrap() // .unwrap()
// .to_string(); // .to_string();
let digest = reqwest::Client::new() let mut digest = String::new();
let rr = reqwest::Client::new()
.get(api_url.clone()) .get(api_url.clone())
.header( .header(
reqwest::header::ACCEPT, reqwest::header::ACCEPT,
"application/vnd.docker.distribution.manifest.v2+json", "application/vnd.docker.distribution.manifest.v2+json",
) )
.send() .send()
.await? .await?;
.headers()
.get("Docker-Content-Digest") if rr.headers().get("Docker-Content-Digest").is_some() {
.unwrap() digest.push_str(
.to_str() rr.headers()
.unwrap() .get("Docker-Content-Digest")
.to_string(); .unwrap()
.to_str()
.unwrap_or(""),
);
}
let mut data = resp.json::<RepositoryTagManifest>().await?; let mut data = resp.json::<RepositoryTagManifest>().await?;
@ -153,7 +157,7 @@ impl<'a> Registry<'a> {
// println!("Status : {}", resp.status()); // println!("Status : {}", resp.status());
// if resp.status().as_u16() >= 400 { // if resp.status().as_u16() >= 400 {
// } // }
Ok(()) Ok(())

View File

@ -46,6 +46,7 @@ fn main() {
// println!("proc images ::: {:?}", proc_images); // println!("proc images ::: {:?}", proc_images);
for img in proc_images.into_iter() { for img in proc_images.into_iter() {
println!("ProcImage: {}", img);
let _repo_tags = registry.get_repository_tags(img.as_str()).await.unwrap(); let _repo_tags = registry.get_repository_tags(img.as_str()).await.unwrap();
let mut tags: Vec<api::registry::RepositoryTagManifest> = Vec::new(); let mut tags: Vec<api::registry::RepositoryTagManifest> = Vec::new();
@ -56,24 +57,36 @@ fn main() {
if _parsed.exclude.contains(x) { if _parsed.exclude.contains(x) {
continue; continue;
} }
let manifests = registry let manifests = registry.get_repository_tag_manifest(img.as_str(), x).await;
.get_repository_tag_manifest(img.as_str(), x) match manifests {
.await Err(_) => continue,
.unwrap(); Ok(v) => tags.push(v),
tags.push(manifests); };
} }
} }
_ => (), _ => continue,
} };
tags.sort_by(|a, b| b.ts.partial_cmp(&a.ts).unwrap()); tags.sort_by(|a, b| b.ts.partial_cmp(&a.ts).unwrap());
// println!("{:?}", tags); // println!("{:?}", tags);
tags.drain(0.._parsed.keep as usize); let del_count: usize = if tags.len() > _parsed.keep as usize {
_parsed.keep as usize
} else {
tags.len()
};
tags.drain(0..del_count);
println!("delete tag number : {}", tags.len()); println!("delete tag number : {}", tags.len());
for x in tags.iter() { for x in tags.iter() {
match registry.delete_repository_tag(img.as_str(), x.digest.as_str()).await { if x.digest.len() == 0 {
continue;
}
match registry
.delete_repository_tag(img.as_str(), x.digest.as_str())
.await
{
Ok(_) => println!("delete {} success", x.tag), Ok(_) => println!("delete {} success", x.tag),
Err(_) => println!("delete {} fail", x.tag), Err(_) => println!("delete {} fail", x.tag),
}; };