refactor: extract args parse function

This commit is contained in:
椰子 2023-11-13 20:37:16 +08:00
parent 9e44df4973
commit 770a4180d1
4 changed files with 30 additions and 31 deletions

View File

@ -4,28 +4,24 @@ use clap::{arg, Parser};
use docker_tags::{DockerTags, QueryArgs};
use docker_tags::registry::RegistryTagsFetcher;
#[derive(Parser, Debug)]
#[derive(Parser)]
#[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>,
repository: 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>,
filter: Option<String>,
}
fn main() {
let args = Args::parse();
let query_args = QueryArgs {
repository: args.repository,
name: args.name,
arch: args.arch,
};
let query_args = QueryArgs::new(args.repository.as_str(), args.filter, args.arch);
// let docker_tags_fetcher = DockerHubTagsFetcher {};
let docker_tags_fetcher = RegistryTagsFetcher {};
let tags = docker_tags_fetcher.get_tags(&query_args);

View File

@ -50,18 +50,8 @@ impl DockerTags for DockerHubTagsFetcher {
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 namespace = args.namespace.as_str();
let repository = args.repository.as_str();
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 {

View File

@ -3,7 +3,8 @@ pub mod docker;
/// query args
pub struct QueryArgs {
pub repository: Option<String>,
pub namespace: String,
pub repository: String,
pub name: Option<String>,
pub arch: Option<String>,
}
@ -11,4 +12,22 @@ pub struct QueryArgs {
pub trait DockerTags {
/// query tags
fn get_tags(&self, args: &QueryArgs) -> Vec<String>;
}
}
impl QueryArgs {
pub fn new(repository: &str, name: Option<String>, arch: Option<String>) -> Self {
let mut repository = repository;
let mut namespace = "library";
let option = repository.find('/');
if option.is_some() {
namespace = &repository[0..option.unwrap()];
repository = &repository[(option.unwrap() + 1)..];
}
Self {
namespace: String::from(namespace),
repository: String::from(repository),
name,
arch,
}
}
}

View File

@ -24,16 +24,10 @@ 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 namespace = args.namespace.as_str();
let repository = args.repository.as_str();
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)