Documentation
¶
Overview ¶
Package containerx provides utilities for managing container images and their versions. It includes functions to set default image names and versions if they are not provided, as well as options for constructing full image URLs with fallback values.
The package is designed to simplify the process of working with container images by providing sensible defaults and fallback mechanisms. This is particularly useful in scenarios where image names or versions might be dynamically determined or not always specified.
The main functionalities provided by this package include: - Setting default image names and versions if they are empty. - Constructing full image URLs from provided options, with support for fallback values.
Example usage:
import (
"github.com/Excoriate/daggerx/pkg/containerx"
)
func main() {
opts := containerx.NewBaseContainerOpts{
Image: "my-image",
Version: "1.0.0",
FallbackImage: "default-image",
FallBackVersion: "latest",
}
imageURL := containerx.GetImageURL(&opts)
fmt.Println(imageURL) // Output: my-image:1.0.0
}
This package is part of the Excoriate DaggerX project and is intended to be used as a utility for managing container images in a consistent and reliable manner.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetImageURL ¶
func GetImageURL(opts *NewBaseContainerOpts) (string, error)
GetImageURL constructs the full image URL from the provided options.
This function takes a pointer to NewBaseContainerOpts and constructs a Docker image URL by combining the image name and version. If the provided image name or version is empty, it uses fallback values. If both the provided and fallback values are empty, it uses default values.
Parameters:
- opts (*NewBaseContainerOpts): A pointer to a NewBaseContainerOpts struct containing the image name, version, fallback image name, and fallback version.
Returns:
- (string, error): Returns the constructed image URL as a string and an error if the options pointer is nil.
Example:
opts := &NewBaseContainerOpts{
Image: "myimage",
Version: "1.0",
FallbackImage: "fallbackimage",
FallBackVersion: "latest",
}
imageURL, err := GetImageURL(opts)
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Println(imageURL) // Output: myimage:1.0
func SetDefaultImageNameIfEmpty ¶ added in v0.0.10
SetDefaultImageNameIfEmpty returns a fallback image if the provided image is empty. If both the provided image and the fallback image are empty, it returns the default image from fixtures.
func SetDefaultImageVersionIfEmpty ¶ added in v0.0.10
SetDefaultImageVersionIfEmpty returns a fallback version if the provided version is empty. If both the provided version and the fallback version are empty, it returns "latest".
func ValidateImageURL ¶ added in v0.0.17
ValidateImageURL verifies the correctness and validity of a given Docker image URL.
This function performs several validation steps to ensure the image URL adheres to expected formats and does not contain any invalid characters or components. The validation includes:
- **Empty Check**: Ensures that the `imageURL` is not an empty string.
- **Structure Validation**: Splits the `imageURL` into segments using the '/' delimiter and checks that the number of components does not exceed four, which could indicate an improperly formatted URL.
- **Repository Name Validation**: Iterates through all repository components except the last one to verify that they do not contain the '@' character, which is reserved for specifying digests.
- **Registry Validation**: If a registry is present in the URL, it validates the registry's format to ensure it conforms to expected naming conventions.
- **Namespace and Repository Validation**: Checks that each namespace and repository name within the URL adheres to valid naming rules, preventing the inclusion of invalid characters or formats.
- **Tag and Digest Validation**: Examines the last part of the URL to ensure that any specified tag or digest is correctly formatted. Specifically, if a digest is present, it must start with a supported hashing algorithm prefix like `sha256:` or `sha512:`.
**Parameters:** - `imageURL` (string): The Docker image URL to be validated.
**Returns:** - `bool`: Returns `true` if the `imageURL` passes all validation checks; otherwise, returns `false`. - `error`: Provides an error detailing the reason for validation failure, if any.
Types ¶
type NewBaseContainerOpts ¶
type NewBaseContainerOpts struct {
// Image is the name of the image to use.
Image string
// Version is the version of the image to use.
Version string
// FallbackImage is the name of the fallback image to use if the primary image is empty.
FallbackImage string
// FallBackVersion is the version of the fallback image to use if the primary image is empty.
FallBackVersion string
}