Skip to content

Commit

Permalink
pkg/utils: Separate container & image name resolution
Browse files Browse the repository at this point in the history
The ResolveContainerAndImageNames() function does too much work. It
makes more sense to have two functions: one for resolving the image
name and another for resolving the container name.

containers#828
containers#838
  • Loading branch information
HarryMichal authored and debarshiray committed Jul 13, 2021
1 parent d03a5fe commit 188908b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
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

0 comments on commit 188908b

Please sign in to comment.