Initial commit

This commit is contained in:
椰子 2023-11-10 20:52:22 +08:00
commit 67eca5685b
7 changed files with 1346 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
/target

1197
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "docker-tags"
version = "0.1.0"
edition = "2021"
authors = ["Yezzi Hsueh"]
license = "MIT"
[dependencies]
reqwest = "0.11.22"
tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread", "rt"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
clap = { version = "4.4.7", features = ["derive"] }

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Yezzi Hsueh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# docker-tags
list all tags for a Docker image on a remote registry.

67
src/bin/dockertags.rs Normal file
View File

@ -0,0 +1,67 @@
use std::error::Error;
use reqwest;
use clap::{arg, Parser};
use docker_tags::{DockerResponse, DockerResult};
#[derive(Parser, Debug)]
#[command(name = "dockertags", author, version, about = "List all tags for a Docker image on a remote registry", long_about = "List all tags for a Docker image on a remote registry")]
struct Args {
/// docker image name
#[arg(value_name = "REPOSITORY")]
image: Option<String>,
/// docker image architecture
#[arg(short, long, value_name = "ARCHITECTURE")]
arch: Option<String>,
/// image tags name to filter
#[arg(short, long, value_name = "TAG")]
name: Option<String>,
}
async fn get_images(namespace: &str, repository: &str) -> Result<Vec<DockerResult>, Box<dyn Error>> {
let page = 0;
let page_size = 1000;
let mut url = format!("https://hub.docker.com/v2/namespaces/{namespace}/repositories/{repository}/tags?page={page}&page_size={page_size}");
let mut results: Vec<DockerResult> = Vec::new();
loop {
let text = reqwest::get(url).await?
.text().await?;
let mut response: DockerResponse = serde_json::from_str(&text).unwrap();
results.append(&mut response.results);
url = match response.next {
None => { break; }
Some(next) => { next }
};
}
// println!("{:?}", results);
Ok(results)
}
#[tokio::main]
async fn main() {
let args = Args::parse();
let mut namespace = String::from("library");
let mut repository = args.image.unwrap();
let split = repository.split('/').collect::<Vec<&str>>();
if split.len() > 1 {
namespace = split.get(0).unwrap().to_string();
repository = split.get(1).unwrap().to_string();
}
let results = get_images(&namespace, &repository).await.unwrap();
let mut filtered = results.into_iter()
.filter(|x| !x.images.is_empty())
.collect::<Vec<DockerResult>>();
if args.name.is_some() {
let pat = args.name.unwrap();
filtered = filtered.into_iter().filter(|x| x.name.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
};
if args.arch.is_some() {
let pat = args.arch.unwrap();
filtered = filtered.into_iter().filter(|x| x.images.get(0).as_ref().unwrap().architecture.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
};
for result in filtered {
println!("{}", result.name.unwrap())
}
}

43
src/lib.rs Normal file
View File

@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerResult {
pub content_type: Option<String>,
pub creator: u64,
pub full_size: u64,
pub id: u64,
pub last_updated: Option<String>,
pub last_updater: u64,
pub last_updater_username: Option<String>,
pub media_type: Option<String>,
pub name: Option<String>,
pub repository: u32,
pub tag_last_pulled: Option<String>,
pub tag_last_pushed: Option<String>,
pub tag_status: Option<String>,
pub v2: bool,
pub images: Vec<DockerImage>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerImage {
pub architecture: Option<String>,
pub digest: Option<String>,
pub features: Option<String>,
pub last_pulled: Option<String>,
pub last_pushed: Option<String>,
pub os: Option<String>,
pub os_features: Option<String>,
pub os_version: Option<String>,
pub size: u64,
pub status: Option<String>,
pub variant: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerResponse {
pub count: u32,
pub next: Option<String>,
pub previous: Option<String>,
pub results: Vec<DockerResult>,
}