diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index 30aa3b3..0000000 --- a/src/config.rs +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2023 Anthony Oteri - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -use std::fs; -use std::path::Path; - -use serde::{Deserialize, Serialize}; -use url::Url; - -use crate::error::ConfigError; - -#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct Config { - pub registry_url: Url, -} - -impl Default for Config { - fn default() -> Self { - Self { - registry_url: Url::parse("https://localhost:5000").unwrap(), - } - } -} - -impl TryFrom<&Path> for Config { - type Error = ConfigError; - - fn try_from(path: &Path) -> Result { - let contents = fs::read_to_string(path)?; - let config: Self = toml::from_str(&contents)?; - Ok(config) - } -} diff --git a/src/error.rs b/src/error.rs index 36fc114..b41a20d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,10 +21,6 @@ use thiserror::Error; /// The common error type for this Application. #[derive(Error, Debug)] pub enum DredgeError { - /// An error related to the configuration of the program. - #[error(transparent)] - ConfigError(#[from] ConfigError), - /// An error communicating with the Registry API #[error(transparent)] ApiError(#[from] ApiError), @@ -34,40 +30,6 @@ pub enum DredgeError { RegistryUrlError(String), } -/// An error related to the configuration fo the program. -#[derive(Error, Debug)] -pub enum ConfigError { - /// An error parsing the configuration from disk. - #[error("Failed to parse configuration file")] - ParseError(Box), - - /// An error writing the configuration to disk. - #[error("Failed to write configuration data")] - WriteError(Box), - - /// A generic IOError - #[error(transparent)] - IOError(#[from] std::io::Error), -} - -impl From for ConfigError { - fn from(other: toml::ser::Error) -> Self { - Self::WriteError(Box::from(other)) - } -} - -impl From for ConfigError { - fn from(other: toml::de::Error) -> Self { - Self::ParseError(Box::from(other)) - } -} - -impl From for ConfigError { - fn from(other: xdg::BaseDirectoriesError) -> Self { - Self::WriteError(Box::from(other)) - } -} - /// An error related to the communication with the registry API. #[derive(Error, Debug)] pub enum ApiError { diff --git a/src/main.rs b/src/main.rs index be34ca9..4ae19d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,10 +27,12 @@ use crate::error::DredgeError; mod api; pub(crate) mod cli; mod commands; -mod config; mod error; -/// Generate the full Docker Registry URL from a given `host[:port]` +/// Name of "latest" tag +const LATEST: &str = "latest"; + +/// Parse the "" argument into a complete Docker Registry URL. /// /// This prepends the HTTPS scheme and converts the given string to a `Url` /// instance. @@ -42,14 +44,11 @@ mod error; /// /// If there is a problem parsing the resulting string as a valid URL, a /// `DredgeError::RegistryUrlError` will be returned. -fn make_registry_url(host: &str) -> Result { +fn parse_registry_arg(host: &str) -> Result { log::trace!("make_registry_url(host: {host})"); Url::parse(host) - .or_else(|_| { - let url_string = format!("https://{host}"); - Url::parse(&url_string) - }) + .or_else(|_| Url::parse(&format!("https://{host}"))) .or(Err(DredgeError::RegistryUrlError(host.to_string()))) } @@ -61,14 +60,15 @@ async fn main() -> Result<(), DredgeError> { let log_level = args.log_level; femme::with_level(log::LevelFilter::from(log_level)); - // -- Generate the complete registry URL from the given host[:path] - let registry_url: Url = make_registry_url(&args.registry)?; + // -- Parse the given argument into a complete URL + let registry_url: Url = parse_registry_arg(&args.registry)?; + // -- Dispatch control to the appropriate command handler. match args.command { Commands::Catalog => commands::catalog_handler(®istry_url).await?, Commands::Tags { name } => commands::tags_handler(®istry_url, &name).await?, Commands::Show { image, tag } => { - commands::show_handler(®istry_url, &image, &tag.unwrap_or("latest".to_string())) + commands::show_handler(®istry_url, &image, &tag.unwrap_or(LATEST.to_string())) .await?; } Commands::Delete { image, tag } => {