Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/utils: Separate container & image name resolution #838

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ func create(cmd *cobra.Command, args []string) error {
}
}

container, image, release, err := utils.ResolveContainerAndImageNames(container,
createFlags.distro,
createFlags.image,
release)
image, release, err := utils.ResolveImageName(createFlags.distro, createFlags.image, release)
if err != nil {
return err
}

container, err = utils.ResolveContainerName(container, image, release)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func enter(cmd *cobra.Command, args []string) error {
}
}

container, image, release, err := utils.ResolveContainerAndImageNames(container, enterFlags.distro, "", release)
image, release, err := utils.ResolveImageName(enterFlags.distro, "", release)
if err != nil {
return err
}

container, err = utils.ResolveContainerName(container, image, release)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func rootRun(cmd *cobra.Command, args []string) error {
return nil
}

container, image, release, err := utils.ResolveContainerAndImageNames("", "", "", "")
image, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}

container, err := utils.ResolveContainerName("", image, release)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ func run(cmd *cobra.Command, args []string) error {

command := args

container, image, release, err := utils.ResolveContainerAndImageNames(runFlags.container, runFlags.distro, "", release)
image, release, err := utils.ResolveImageName(runFlags.distro, "", release)
if err != nil {
return err
}

container, err := utils.ResolveContainerName(runFlags.container, image, release)
if err != nil {
return err
}
Expand Down
56 changes: 34 additions & 22 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,15 +672,41 @@ func IsInsideToolboxContainer() bool {
return false
}

// ResolveContainerAndImageNames takes care of standardizing names of containers and images.
// ResolveContainerName standardizes the name of a container
//
// If no image name is specified then the base image will reflect the platform of the host (even the version).
// If no container name is specified then the name of the image will be used.
func ResolveContainerName(container, image, release string) (string, error) {
logrus.Debug("Resolving container name")
logrus.Debugf("Container: '%s'", container)
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)

if container == "" {
var err error
container, err = GetContainerNamePrefixForImage(image)
if err != nil {
return "", err
}

tag := ImageReferenceGetTag(image)
if tag != "" {
container = container + "-" + tag
}
}

logrus.Debug("Resolved container name")
logrus.Debugf("Container: '%s'", container)

return container, nil
}

// ResolveImageName standardizes the name of an image.
//
// If no image name is specified then the base image will reflect the platform of the host (even the version).
//
// If the host system is unknown then the base image will be 'fedora-toolbox' with a default version
func ResolveContainerAndImageNames(container, distro, image, release string) (string, string, string, error) {
logrus.Debug("Resolving container and image names")
logrus.Debugf("Container: '%s'", container)
func ResolveImageName(distro, image, release string) (string, string, error) {
logrus.Debug("Resolving image name")
logrus.Debugf("Distribution: '%s'", distro)
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)
Expand All @@ -690,7 +716,7 @@ func ResolveContainerAndImageNames(container, distro, image, release string) (st
}

if distro != distroDefault && release == "" {
return "", "", "", fmt.Errorf("release not found for non-default distribution %s", distro)
return "", "", fmt.Errorf("release not found for non-default distribution %s", distro)
}

if release == "" {
Expand All @@ -706,25 +732,11 @@ func ResolveContainerAndImageNames(container, distro, image, release string) (st
}
}

if container == "" {
var err error
container, err = GetContainerNamePrefixForImage(image)
if err != nil {
return "", "", "", err
}

tag := ImageReferenceGetTag(image)
if tag != "" {
container = container + "-" + tag
}
}

logrus.Debug("Resolved container and image names")
logrus.Debugf("Container: '%s'", container)
logrus.Debug("Resolved image name")
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)

return container, image, release, nil
return image, release, nil
}

func ShowManual(manual string) error {
Expand Down