From 770a4180d1008863a4ab29aadaa1213db3deff03 Mon Sep 17 00:00:00 2001 From: Yezzi Hsueh Date: Mon, 13 Nov 2023 20:37:16 +0800 Subject: [PATCH] refactor: extract args parse function --- src/bin/dockertags.rs | 12 ++++-------- src/docker.rs | 14 ++------------ src/lib.rs | 23 +++++++++++++++++++++-- src/registry.rs | 12 +++--------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/bin/dockertags.rs b/src/bin/dockertags.rs index dd5d39c..bbb752f 100644 --- a/src/bin/dockertags.rs +++ b/src/bin/dockertags.rs @@ -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, + repository: String, /// docker image architecture #[arg(short, long, value_name = "ARCHITECTURE")] arch: Option, /// image tags name to filter #[arg(short, long, value_name = "TAG")] - name: Option, + filter: Option, } 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); diff --git a/src/docker.rs b/src/docker.rs index 7f21869..d4359cf 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -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::>(); - 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 = Vec::new(); loop { diff --git a/src/lib.rs b/src/lib.rs index 3ea48c4..64d63b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,8 @@ pub mod docker; /// query args pub struct QueryArgs { - pub repository: Option, + pub namespace: String, + pub repository: String, pub name: Option, pub arch: Option, } @@ -11,4 +12,22 @@ pub struct QueryArgs { pub trait DockerTags { /// query tags fn get_tags(&self, args: &QueryArgs) -> Vec; -} \ No newline at end of file +} + +impl QueryArgs { + pub fn new(repository: &str, name: Option, arch: Option) -> 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, + } + } +} diff --git a/src/registry.rs b/src/registry.rs index cc2fead..fc401cc 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -24,16 +24,10 @@ pub struct RegistryTagsFetcher {} impl DockerTags for RegistryTagsFetcher { fn get_tags(&self, args: &QueryArgs) -> Vec { - 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)