Compare commits
No commits in common. "6575ccd38b00e7dfb402a7b9a7fa25ee2574ea3f" and "67eca5685bed2450dc4bcea9c1e69faa136daafe" have entirely different histories.
6575ccd38b
...
67eca5685b
104
Cargo.lock
generated
104
Cargo.lock
generated
@ -193,37 +193,15 @@ version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
|
||||
|
||||
[[package]]
|
||||
name = "directories"
|
||||
version = "5.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"option-ext",
|
||||
"redox_users",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "docker-tags"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"clap",
|
||||
"directories",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -296,12 +274,6 @@ version = "0.3.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c"
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.29"
|
||||
@ -321,23 +293,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -507,17 +465,6 @@ version = "0.2.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
|
||||
dependencies = [
|
||||
"bitflags 2.4.1",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.11"
|
||||
@ -649,12 +596,6 @@ dependencies = [
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.0"
|
||||
@ -706,17 +647,6 @@ dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"libredox",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.11.22"
|
||||
@ -935,26 +865,6 @@ dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
@ -983,9 +893,21 @@ dependencies = [
|
||||
"num_cpus",
|
||||
"pin-project-lite",
|
||||
"socket2 0.5.5",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-native-tls"
|
||||
version = "0.3.1"
|
||||
|
@ -6,9 +6,8 @@ authors = ["Yezzi Hsueh"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.11.22", features = ["blocking"] }
|
||||
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"] }
|
||||
directories = "5.0.1"
|
||||
base64 = "0.21.5"
|
||||
clap = { version = "4.4.7", features = ["derive"] }
|
@ -1,14 +1,14 @@
|
||||
use std::error::Error;
|
||||
use reqwest;
|
||||
use clap::{arg, Parser};
|
||||
//use docker_tags::docker::DockerHubTagsFetcher;
|
||||
use docker_tags::{DockerTags, QueryArgs};
|
||||
use docker_tags::registry::RegistryTagsFetcher;
|
||||
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")]
|
||||
repository: Option<String>,
|
||||
image: Option<String>,
|
||||
/// docker image architecture
|
||||
#[arg(short, long, value_name = "ARCHITECTURE")]
|
||||
arch: Option<String>,
|
||||
@ -17,19 +17,51 @@ struct Args {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
async fn get_images(namespace: &str, repository: &str) -> Result<Vec<DockerResult>, Box<dyn Error>> {
|
||||
let page = 0;
|
||||
let page_size = 1000;
|
||||
|
||||
fn main() {
|
||||
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 query_args = QueryArgs {
|
||||
repository: args.repository,
|
||||
name: args.name,
|
||||
arch: args.arch,
|
||||
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>>();
|
||||
};
|
||||
// let docker_tags_fetcher = DockerHubTagsFetcher {};
|
||||
let docker_tags_fetcher = RegistryTagsFetcher {};
|
||||
let tags = docker_tags_fetcher.get_tags(&query_args);
|
||||
|
||||
for tag in tags {
|
||||
println!("{}", &tag)
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
@ -1,95 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::{DockerTags, QueryArgs};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerResult {
|
||||
content_type: Option<String>,
|
||||
creator: u64,
|
||||
full_size: u64,
|
||||
id: u64,
|
||||
last_updated: Option<String>,
|
||||
last_updater: u64,
|
||||
last_updater_username: Option<String>,
|
||||
media_type: Option<String>,
|
||||
name: Option<String>,
|
||||
repository: u32,
|
||||
tag_last_pulled: Option<String>,
|
||||
tag_last_pushed: Option<String>,
|
||||
tag_status: Option<String>,
|
||||
v2: bool,
|
||||
images: Vec<DockerImage>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerImage {
|
||||
architecture: Option<String>,
|
||||
digest: Option<String>,
|
||||
features: Option<String>,
|
||||
last_pulled: Option<String>,
|
||||
last_pushed: Option<String>,
|
||||
os: Option<String>,
|
||||
os_features: Option<String>,
|
||||
os_version: Option<String>,
|
||||
size: u64,
|
||||
status: Option<String>,
|
||||
variant: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerResponse {
|
||||
count: u32,
|
||||
next: Option<String>,
|
||||
previous: Option<String>,
|
||||
results: Vec<DockerResult>,
|
||||
}
|
||||
|
||||
pub struct DockerHubTagsFetcher {}
|
||||
|
||||
impl DockerTags for DockerHubTagsFetcher {
|
||||
fn get_tags(&self, args: &QueryArgs) -> Vec<String> {
|
||||
let page = 0;
|
||||
let page_size = 1000;
|
||||
|
||||
let mut namespace = "library";
|
||||
// let mut repository = args.repository.clone().unwrap();
|
||||
let repository = match &args.repository {
|
||||
Some(s) => {
|
||||
let vec = s.split('/')
|
||||
.into_iter()
|
||||
.collect::<Vec<&str>>();
|
||||
namespace = vec[0];
|
||||
vec[1]
|
||||
}
|
||||
None => { "" }
|
||||
};
|
||||
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::blocking::get(url).unwrap().text().unwrap();
|
||||
let mut response: DockerResponse = serde_json::from_str(&text).unwrap();
|
||||
results.append(&mut response.results);
|
||||
url = match response.next {
|
||||
None => { break; }
|
||||
Some(next) => { next }
|
||||
};
|
||||
}
|
||||
let mut filtered = results.into_iter()
|
||||
.filter(|x| !x.images.is_empty())
|
||||
.collect::<Vec<DockerResult>>();
|
||||
if args.name.is_some() {
|
||||
let pat = args.name.clone().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.clone().unwrap();
|
||||
filtered = filtered.into_iter().filter(|x| x.images.get(0).as_ref().unwrap().architecture.clone().unwrap().contains(&pat))
|
||||
.collect::<Vec<DockerResult>>();
|
||||
};
|
||||
let mut results = Vec::<String>::new();
|
||||
for x in &filtered {
|
||||
results.push(x.name.clone().unwrap());
|
||||
}
|
||||
results
|
||||
}
|
||||
}
|
47
src/lib.rs
47
src/lib.rs
@ -1,14 +1,43 @@
|
||||
pub mod registry;
|
||||
pub mod docker;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// query args
|
||||
pub struct QueryArgs {
|
||||
pub repository: Option<String>,
|
||||
#[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 arch: 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>,
|
||||
}
|
||||
|
||||
pub trait DockerTags {
|
||||
/// query tags
|
||||
fn get_tags(&self, args: &QueryArgs) -> Vec<String>;
|
||||
#[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>,
|
||||
}
|
117
src/registry.rs
117
src/registry.rs
@ -1,117 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use directories::UserDirs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::{DockerTags, QueryArgs};
|
||||
|
||||
/// registry api response struct
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RegistryTagsResponse {
|
||||
name: Option<String>,
|
||||
tags: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RegistryTokenResponse {
|
||||
token: Option<String>,
|
||||
access_token: Option<String>,
|
||||
expires_in: u32,
|
||||
issued_at: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
pub struct RegistryTagsFetcher {}
|
||||
|
||||
impl DockerTags for RegistryTagsFetcher {
|
||||
fn get_tags(&self, args: &QueryArgs) -> Vec<String> {
|
||||
let repository = args.repository.clone().unwrap();
|
||||
let mut namespace = "library";
|
||||
let mut repository = repository.as_str();
|
||||
let option = repository.find('/');
|
||||
if option.is_some() {
|
||||
namespace = &repository[0..option.unwrap()];
|
||||
repository = &repository[(option.unwrap() + 1)..];
|
||||
}
|
||||
let token = get_token(namespace, repository).unwrap();
|
||||
|
||||
let url = format!("https://registry-1.docker.io/v2/{namespace}/{repository}/tags/list");
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client.get(url)
|
||||
.header("Authorization", format!("Bearer {token}"))
|
||||
.send();
|
||||
let mut results = Vec::<String>::new();
|
||||
if let Ok(r) = response {
|
||||
let json: RegistryTagsResponse = serde_json::from_str(r.text().unwrap().as_str()).unwrap();
|
||||
results.append(&mut json.tags.unwrap());
|
||||
};
|
||||
results
|
||||
}
|
||||
}
|
||||
|
||||
/// get the access token for tags request
|
||||
fn get_token(namespace: &str, repository: &str) -> Option<String> {
|
||||
let config = match read_config() {
|
||||
None => { return None; }
|
||||
Some(x) => { x }
|
||||
};
|
||||
let service = "registry.docker.io";
|
||||
let scope = format!("repository:{namespace}/{repository}:pull");
|
||||
let auth = format!("https://auth.docker.io/token?service={service}&scope={scope}");
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client.get(auth)
|
||||
.basic_auth(config.username, Some(config.password))
|
||||
.send();
|
||||
match response {
|
||||
Ok(r) => {
|
||||
let json: RegistryTokenResponse = serde_json::from_str(r.text().unwrap().as_str()).unwrap();
|
||||
Some(json.token.unwrap())
|
||||
}
|
||||
Err(_) => { None }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerConfig {
|
||||
auths: HashMap<String, DockerAuth>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerAuth {
|
||||
auth: Option<String>,
|
||||
}
|
||||
|
||||
use base64::{Engine as _, engine::general_purpose};
|
||||
|
||||
/// read authentication info from local docker config file
|
||||
fn read_config() -> Option<DockerPassword> {
|
||||
let user_dirs = UserDirs::new().unwrap();
|
||||
let home_dir = user_dirs.home_dir();
|
||||
let config_path = home_dir.join(".docker").join("config.json");
|
||||
let config_context = fs::read_to_string(config_path);
|
||||
let docker_config: DockerConfig = serde_json::from_str(config_context.unwrap().as_str()).unwrap();
|
||||
let auths = docker_config.auths;
|
||||
let auth = match auths.get("https://index.docker.io/v1/") {
|
||||
None => { return None; }
|
||||
Some(docker_auth) => { docker_auth.auth.clone().unwrap() }
|
||||
};
|
||||
let mut buffer = Vec::<u8>::new();
|
||||
let _result = general_purpose::URL_SAFE_NO_PAD.decode_vec(auth.as_bytes(), &mut buffer);
|
||||
let decoded = match std::str::from_utf8(&buffer) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
|
||||
};
|
||||
let idx = match decoded.find(':') {
|
||||
None => { panic!("Invalid authentication: {}", decoded) }
|
||||
Some(i) => { i }
|
||||
};
|
||||
return Some(DockerPassword {
|
||||
username: String::from(&decoded[0..idx]),
|
||||
password: String::from(&decoded[(idx + 1)..]),
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DockerPassword {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
Loading…
Reference in New Issue
Block a user