diff --git a/commands/build.go b/commands/build.go index b6ef6ce..235892d 100644 --- a/commands/build.go +++ b/commands/build.go @@ -4,6 +4,7 @@ import ( "log" "path" + "github.com/bravetools/bravetools/platform" "github.com/spf13/cobra" ) @@ -40,7 +41,11 @@ func build(cmd *cobra.Command, args []string) { } err = host.BuildImage(bravefile) - if err != nil { + switch errType := err.(type) { + case nil: + case *platform.ImageExistsError: + log.Fatalf("image %q already exists - if you want to rebuild it, first delete the existing image with: `brave remove -i [IMAGE]`", errType.Name) + default: log.Fatal(err) } } diff --git a/platform/helpers.go b/platform/helpers.go index 0023241..2cf4088 100644 --- a/platform/helpers.go +++ b/platform/helpers.go @@ -462,3 +462,9 @@ func checkUnits(unitName string, bh *BraveHost) error { return nil } + +func imageExists(imageNameAndVersion string) bool { + homeDir, _ := os.UserHomeDir() + image := path.Join(homeDir, shared.ImageStore, imageNameAndVersion+".tar.gz") + return shared.FileExists(image) +} diff --git a/platform/host_api.go b/platform/host_api.go index 96769c0..94b531d 100644 --- a/platform/host_api.go +++ b/platform/host_api.go @@ -530,6 +530,14 @@ func (bh *BraveHost) DeleteUnit(name string) error { return nil } +type ImageExistsError struct { + Name string +} + +func (e *ImageExistsError) Error() string { + return fmt.Sprintf("image %q already exists", e.Name) +} + // BuildImage creates an image based on Bravefile func (bh *BraveHost) BuildImage(bravefile *shared.Bravefile) error { if strings.ContainsAny(bravefile.PlatformService.Name, "/_. !@£$%^&*(){}:;`~,?") { @@ -544,6 +552,9 @@ func (bh *BraveHost) BuildImage(bravefile *shared.Bravefile) error { if err != nil { return err } + if imageExists(bravefile.PlatformService.Image) { + return &ImageExistsError{Name: bravefile.PlatformService.Image} + } // Intercept SIGINT, propagate cancel and cleanup artefacts var imageFingerprint string @@ -797,6 +808,9 @@ func (bh *BraveHost) InitUnit(backend Backend, unitParams *shared.Bravefile) (er if err != nil { return err } + if !imageExists(unitParams.PlatformService.Image) { + return fmt.Errorf("image %q does not exist", unitParams.PlatformService.Image) + } var fingerprint string