Skip to content

Commit

Permalink
cmd/utils, pkg/utils: Beautify an error message for the image option
Browse files Browse the repository at this point in the history
  • Loading branch information
debarshiray committed Aug 31, 2022
1 parent 8efe6b6 commit 1ae5b69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ func createErrorInvalidImageForContainerName() error {
return errors.New(errMsg)
}

func createErrorInvalidImageWithoutBasename() error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--image'\n")
fmt.Fprintf(&builder, "Images must have basenames.\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

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

func createErrorInvalidRelease(hint string) error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--release'\n")
Expand Down Expand Up @@ -145,6 +155,9 @@ func handleResolveErrorForCLI(err error, containerArg, distro string) error {
} else if errors.Is(err, utils.ErrContainerNameFromImageInvalid) {
err := createErrorInvalidImageForContainerName()
return err
} else if errors.Is(err, utils.ErrImageWithoutBasename) {
err := createErrorInvalidImageWithoutBasename()
return err
} else {
var errParseRelease *utils.ParseReleaseError

Expand Down
4 changes: 3 additions & 1 deletion src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ var (
ErrContainerNameInvalid = errors.New("container name is invalid")

ErrContainerNameFromImageInvalid = errors.New("container name generated from image is invalid")

ErrImageWithoutBasename = errors.New("image does not have a basename")
)

func init() {
Expand Down Expand Up @@ -247,7 +249,7 @@ func GetCgroupsVersion() (int, error) {
func getContainerNamePrefixForImage(image string) (string, error) {
basename := ImageReferenceGetBasename(image)
if basename == "" {
return "", fmt.Errorf("failed to get the basename of image %s", image)
return "", fmt.Errorf("%s: %w", image, ErrImageWithoutBasename)
}

for _, distroObj := range supportedDistros {
Expand Down

0 comments on commit 1ae5b69

Please sign in to comment.