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

More robustly handle values passed to --distro and --release #977

Merged
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
16 changes: 11 additions & 5 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,23 @@ func create(cmd *cobra.Command, args []string) error {
}
}

var release string
if createFlags.release != "" {
distro, err := utils.ResolveDistro(createFlags.distro)
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err := createErrorInvalidDistro()
return err
}

release := createFlags.release
if release != "" {
var err error
release, err = utils.ParseRelease(createFlags.distro, createFlags.release)
release, err = utils.ParseRelease(distro, release)
if err != nil {
err := createErrorInvalidRelease()
err := createErrorInvalidRelease(distro)
return err
}
}

image, release, err := utils.ResolveImageName(createFlags.distro, createFlags.image, release)
image, release, err := utils.ResolveImageName(distro, createFlags.image, release)
if err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions src/cmd/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,25 @@ func enter(cmd *cobra.Command, args []string) error {
}
}

var release string
if enterFlags.release != "" {
distro, err := utils.ResolveDistro(enterFlags.distro)
if err != nil {
err := createErrorInvalidDistro()
return err
}

release := enterFlags.release
if release != "" {
defaultContainer = false

var err error
release, err = utils.ParseRelease(enterFlags.distro, enterFlags.release)
release, err = utils.ParseRelease(distro, release)
if err != nil {
err := createErrorInvalidRelease()
err := createErrorInvalidRelease(distro)
return err
}
}

image, release, err := utils.ResolveImageName(enterFlags.distro, "", release)
image, release, err := utils.ResolveImageName(distro, "", release)
if err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,20 @@ func run(cmd *cobra.Command, args []string) error {
}
}

var release string
if runFlags.release != "" {
distro, err := utils.ResolveDistro(runFlags.distro)
if err != nil {
err := createErrorInvalidDistro()
return err
}

release := runFlags.release
if release != "" {
defaultContainer = false

var err error
release, err = utils.ParseRelease(runFlags.distro, runFlags.release)
release, err = utils.ParseRelease(distro, release)
if err != nil {
err := createErrorInvalidRelease()
err := createErrorInvalidRelease(distro)
return err
}
}
Expand All @@ -125,7 +131,7 @@ func run(cmd *cobra.Command, args []string) error {

command := args

image, release, err := utils.ResolveImageName(runFlags.distro, "", release)
image, release, err := utils.ResolveImageName(distro, "", release)
if err != nil {
return err
}
Expand Down
15 changes: 14 additions & 1 deletion src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os/exec"
"strings"
"syscall"

"github.com/containers/toolbox/pkg/utils"
)

// askForConfirmation prints prompt to stdout and waits for response from the
Expand Down Expand Up @@ -69,9 +71,20 @@ func createErrorContainerNotFound(container string) error {
return errors.New(errMsg)
}

func createErrorInvalidRelease() error {
func createErrorInvalidDistro() error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--distro'\n")
fmt.Fprintf(&builder, "Supported values are: %s\n", strings.Join(utils.GetSupportedDistros(), " "))
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
return errors.New(errMsg)
}

func createErrorInvalidRelease(distro string) error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--release'\n")
fmt.Fprintf(&builder, "Supported values for distribution %s are in format: %s\n", distro, utils.GetReleaseFormat(distro))
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
Expand Down
88 changes: 76 additions & 12 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Distro struct {
ContainerNamePrefix string
ImageBasename string
ParseRelease ParseReleaseFunc
ReleaseFormat string
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
Registry string
Repository string
RepositoryNeedsRelease bool
Expand All @@ -60,6 +61,10 @@ const (
ContainerNameRegexp = "[a-zA-Z0-9][a-zA-Z0-9_.-]*"
)

var (
ErrUnsupportedDistro = errors.New("linux distribution is not supported")
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
)

var (
containerNamePrefixDefault = "fedora-toolbox"

Expand Down Expand Up @@ -98,6 +103,7 @@ var (
"fedora-toolbox",
"fedora-toolbox",
parseReleaseFedora,
"<release>/f<release>",
"registry.fedoraproject.org",
"",
false,
Expand All @@ -106,6 +112,7 @@ var (
"rhel-toolbox",
"toolbox",
parseReleaseRHEL,
"<major.minor>",
"registry.access.redhat.com",
"ubi8",
false,
Expand Down Expand Up @@ -242,8 +249,8 @@ func GetContainerNamePrefixForImage(image string) (string, error) {
}

func getDefaultImageForDistro(distro, release string) string {
if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distro = "fedora"
if !IsDistroSupported(distro) {
distro = distroDefault
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
}

distroObj, supportedDistro := supportedDistros[distro]
Expand Down Expand Up @@ -407,6 +414,20 @@ func GetMountOptions(target string) (string, error) {
return mountOptions, nil
}

// GetReleaseFormat returns the format string signifying supported release
// version formats.
//
// distro should be value found under ID in os-release.
//
// If distro is unsupported an empty string is returned.
func GetReleaseFormat(distro string) string {
if !IsDistroSupported(distro) {
return ""
}

return supportedDistros[distro].ReleaseFormat
}

func GetRuntimeDirectory(targetUser *user.User) (string, error) {
gid, err := strconv.Atoi(targetUser.Gid)
if err != nil {
Expand Down Expand Up @@ -446,6 +467,15 @@ func GetRuntimeDirectory(targetUser *user.User) (string, error) {
return toolboxRuntimeDirectory, nil
}

// GetSupportedDistros returns a list of supported distributions
func GetSupportedDistros() []string {
var distros []string
for d := range supportedDistros {
distros = append(distros, d)
}
return distros
}

// HumanDuration accepts a Unix time value and converts it into a human readable
// string.
//
Expand All @@ -454,6 +484,14 @@ func HumanDuration(duration int64) string {
return units.HumanDuration(time.Since(time.Unix(duration, 0))) + " ago"
}

// IsDistroSupported signifies if a distribution has a toolbx image for it.
//
// distro should be value found under ID in os-release.
func IsDistroSupported(distro string) bool {
_, ok := supportedDistros[distro]
return ok
}

// ImageReferenceCanBeID checks if 'image' might be the ID of an image
func ImageReferenceCanBeID(image string) bool {
matched, err := regexp.MatchString("^[a-f0-9]{6,64}$", image)
Expand Down Expand Up @@ -598,16 +636,18 @@ func ShortID(id string) string {
return id
}

// ParseRelease assesses if the requested version of a distribution is in
// the correct format.
//
// If distro is an empty string, a default value (value under the
// 'general.distro' key in a config file or 'fedora') is assumed.
func ParseRelease(distro, release string) (string, error) {
if distro == "" {
distro = distroDefault
if viper.IsSet("general.distro") {
distro = viper.GetString("general.distro")
}
distro, _ = ResolveDistro(distro)
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
}

if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distro = "fedora"
if !IsDistroSupported(distro) {
distro = distroDefault
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
}

distroObj, supportedDistro := supportedDistros[distro]
Expand Down Expand Up @@ -712,6 +752,33 @@ func ResolveContainerName(container, image, release string) (string, error) {
return container, nil
}

// ResolveDistro assess if the requested distribution is supported and provides
// a default value if none is requested.
//
// If distro is empty, and the "general.distro" key in a config file is set,
// the value is read from the config file. If the key is not set, the default
// value ('fedora') is used instead.
func ResolveDistro(distro string) (string, error) {
logrus.Debug("Resolving distribution")
logrus.Debugf("Distribution: %s", distro)

if distro == "" {
distro = distroDefault
if viper.IsSet("general.distro") {
distro = viper.GetString("general.distro")
}
}

if !IsDistroSupported(distro) {
return "", ErrUnsupportedDistro
}

logrus.Debug("Resolved distribution")
logrus.Debugf("Distribution: %s", distro)

return distro, 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).
Expand All @@ -726,10 +793,7 @@ func ResolveImageName(distroCLI, imageCLI, releaseCLI string) (string, string, e
distro, image, release := distroCLI, imageCLI, releaseCLI

if distroCLI == "" {
distro = distroDefault
if viper.IsSet("general.distro") {
distro = viper.GetString("general.distro")
}
distro, _ = ResolveDistro(distroCLI)
}

if distro != distroDefault && releaseCLI == "" && !viper.IsSet("general.release") {
Expand Down
Loading