Skip to content

Commit 77d2464

Browse files
authored
fix(image build): improve error when no image specified (#70)
1 parent 7b46253 commit 77d2464

File tree

1 file changed

+20
-16
lines changed
  • internal/app/azldev/cmds/image

1 file changed

+20
-16
lines changed

internal/app/azldev/cmds/image/boot.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,27 +423,31 @@ func ResolveImageByName(env *azldev.Env, imageName string) (*projectconfig.Image
423423
return nil, errors.New("no project configuration loaded")
424424
}
425425

426-
if imageName == "" {
427-
return nil, errors.New("image name is required")
428-
}
429-
430-
imageConfig, ok := cfg.Images[imageName]
431-
if !ok {
432-
// List available images in the error message.
433-
availableImages := lo.Keys(cfg.Images)
434-
sort.Strings(availableImages)
426+
var err error
435427

436-
if len(availableImages) == 0 {
437-
return nil, fmt.Errorf("image %#q not found; no images defined in project configuration", imageName)
428+
// If an image name was provided and exists in the configuration, then return the config.
429+
if imageName != "" {
430+
if imageConfig, ok := cfg.Images[imageName]; ok {
431+
return &imageConfig, nil
438432
}
439433

440-
return nil, fmt.Errorf(
441-
"image %#q not found in project configuration; available images: %s",
442-
imageName, strings.Join(availableImages, ", "),
443-
)
434+
err = fmt.Errorf("image %#q not found in project configuration", imageName)
435+
} else {
436+
err = errors.New("image name is required")
444437
}
445438

446-
return &imageConfig, nil
439+
// Something went wrong; list available images so we can offer options in the error message.
440+
availableImages := lo.Keys(cfg.Images)
441+
sort.Strings(availableImages)
442+
443+
if len(availableImages) == 0 {
444+
return nil, fmt.Errorf("%w; no images defined in project configuration", err)
445+
}
446+
447+
return nil, fmt.Errorf(
448+
"%w; available images: %s",
449+
err, strings.Join(availableImages, ", "),
450+
)
447451
}
448452

449453
func checkBootPrerequisites(env *azldev.Env, arch string) error {

0 commit comments

Comments
 (0)