mirror of
https://github.com/anthonyoteri/dredge.git
synced 2026-06-05 15:26:53 -04:00
Merge pull request #28 from anthonyoteri/misc-cleanup
Miscellaneous code cleanup
This commit is contained in:
@@ -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<Self, Self::Error> {
|
|
||||||
let contents = fs::read_to_string(path)?;
|
|
||||||
let config: Self = toml::from_str(&contents)?;
|
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -21,10 +21,6 @@ use thiserror::Error;
|
|||||||
/// The common error type for this Application.
|
/// The common error type for this Application.
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum DredgeError {
|
pub enum DredgeError {
|
||||||
/// An error related to the configuration of the program.
|
|
||||||
#[error(transparent)]
|
|
||||||
ConfigError(#[from] ConfigError),
|
|
||||||
|
|
||||||
/// An error communicating with the Registry API
|
/// An error communicating with the Registry API
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
ApiError(#[from] ApiError),
|
ApiError(#[from] ApiError),
|
||||||
@@ -34,40 +30,6 @@ pub enum DredgeError {
|
|||||||
RegistryUrlError(String),
|
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<dyn std::error::Error>),
|
|
||||||
|
|
||||||
/// An error writing the configuration to disk.
|
|
||||||
#[error("Failed to write configuration data")]
|
|
||||||
WriteError(Box<dyn std::error::Error>),
|
|
||||||
|
|
||||||
/// A generic IOError
|
|
||||||
#[error(transparent)]
|
|
||||||
IOError(#[from] std::io::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<toml::ser::Error> for ConfigError {
|
|
||||||
fn from(other: toml::ser::Error) -> Self {
|
|
||||||
Self::WriteError(Box::from(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<toml::de::Error> for ConfigError {
|
|
||||||
fn from(other: toml::de::Error) -> Self {
|
|
||||||
Self::ParseError(Box::from(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<xdg::BaseDirectoriesError> for ConfigError {
|
|
||||||
fn from(other: xdg::BaseDirectoriesError) -> Self {
|
|
||||||
Self::WriteError(Box::from(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An error related to the communication with the registry API.
|
/// An error related to the communication with the registry API.
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
|
|||||||
+10
-10
@@ -27,10 +27,12 @@ use crate::error::DredgeError;
|
|||||||
mod api;
|
mod api;
|
||||||
pub(crate) mod cli;
|
pub(crate) mod cli;
|
||||||
mod commands;
|
mod commands;
|
||||||
mod config;
|
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
/// Generate the full Docker Registry URL from a given `host[:port]`
|
/// Name of "latest" tag
|
||||||
|
const LATEST: &str = "latest";
|
||||||
|
|
||||||
|
/// Parse the "<REGISTRY>" argument into a complete Docker Registry URL.
|
||||||
///
|
///
|
||||||
/// This prepends the HTTPS scheme and converts the given string to a `Url`
|
/// This prepends the HTTPS scheme and converts the given string to a `Url`
|
||||||
/// instance.
|
/// instance.
|
||||||
@@ -42,14 +44,11 @@ mod error;
|
|||||||
///
|
///
|
||||||
/// If there is a problem parsing the resulting string as a valid URL, a
|
/// If there is a problem parsing the resulting string as a valid URL, a
|
||||||
/// `DredgeError::RegistryUrlError` will be returned.
|
/// `DredgeError::RegistryUrlError` will be returned.
|
||||||
fn make_registry_url(host: &str) -> Result<Url, DredgeError> {
|
fn parse_registry_arg(host: &str) -> Result<Url, DredgeError> {
|
||||||
log::trace!("make_registry_url(host: {host})");
|
log::trace!("make_registry_url(host: {host})");
|
||||||
|
|
||||||
Url::parse(host)
|
Url::parse(host)
|
||||||
.or_else(|_| {
|
.or_else(|_| Url::parse(&format!("https://{host}")))
|
||||||
let url_string = format!("https://{host}");
|
|
||||||
Url::parse(&url_string)
|
|
||||||
})
|
|
||||||
.or(Err(DredgeError::RegistryUrlError(host.to_string())))
|
.or(Err(DredgeError::RegistryUrlError(host.to_string())))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,14 +60,15 @@ async fn main() -> Result<(), DredgeError> {
|
|||||||
let log_level = args.log_level;
|
let log_level = args.log_level;
|
||||||
femme::with_level(log::LevelFilter::from(log_level));
|
femme::with_level(log::LevelFilter::from(log_level));
|
||||||
|
|
||||||
// -- Generate the complete registry URL from the given host[:path]
|
// -- Parse the given <REGISTRY> argument into a complete URL
|
||||||
let registry_url: Url = make_registry_url(&args.registry)?;
|
let registry_url: Url = parse_registry_arg(&args.registry)?;
|
||||||
|
|
||||||
|
// -- Dispatch control to the appropriate command handler.
|
||||||
match args.command {
|
match args.command {
|
||||||
Commands::Catalog => commands::catalog_handler(®istry_url).await?,
|
Commands::Catalog => commands::catalog_handler(®istry_url).await?,
|
||||||
Commands::Tags { name } => commands::tags_handler(®istry_url, &name).await?,
|
Commands::Tags { name } => commands::tags_handler(®istry_url, &name).await?,
|
||||||
Commands::Show { image, tag } => {
|
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?;
|
.await?;
|
||||||
}
|
}
|
||||||
Commands::Delete { image, tag } => {
|
Commands::Delete { image, tag } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user