packer/command/core_wrapper.go
Lucas Bajolet 61c810e720 command: rework HCP-related error messages
The HCP error messages were sometimes a bit terse in terms of the
information it bubbles up to the user.
This made them useful for people who already knew what to look for, but
newcomers would almost certainly be lost because of the lack of
information in those.

To improve on this situation, we reword most of those error messages in
this commit.
2022-10-21 10:32:45 -04:00

72 lines
2.2 KiB
Go

package command
import (
"fmt"
"github.com/hashicorp/hcl/v2"
packerregistry "github.com/hashicorp/packer/internal/registry"
"github.com/hashicorp/packer/packer"
plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
)
// CoreWrapper wraps a packer.Core in order to have it's Initialize func return
// a diagnostic.
type CoreWrapper struct {
*packer.Core
}
func (c *CoreWrapper) Initialize(_ packer.InitializeOptions) hcl.Diagnostics {
err := c.Core.Initialize()
if err != nil {
return hcl.Diagnostics{
&hcl.Diagnostic{
Detail: err.Error(),
Severity: hcl.DiagError,
},
}
}
return nil
}
func (c *CoreWrapper) PluginRequirements() (plugingetter.Requirements, hcl.Diagnostics) {
return nil, hcl.Diagnostics{
&hcl.Diagnostic{
Summary: "Packer plugins currently only works with HCL2 configuration templates",
Detail: "Please manually install plugins with the plugins command or use a HCL2 configuration that will do that for you.",
Severity: hcl.DiagError,
},
}
}
// ConfiguredArtifactMetadataPublisher returns a configured image bucket that can be used for publishing
// build image artifacts to a configured Packer Registry destination.
func (c *CoreWrapper) ConfiguredArtifactMetadataPublisher() (*packerregistry.Bucket, hcl.Diagnostics) {
bucket := c.Core.GetRegistryBucket()
// If at this point the bucket is nil, it means the HCP Packer registry is not enabled
if bucket == nil {
return nil, hcl.Diagnostics{
&hcl.Diagnostic{
Summary: "Publishing build artifacts to HCP Packer Registry not enabled",
Detail: "No Packer Registry configuration detected; skipping all publishing steps " +
"See publishing to a Packer registry for Packer configuration details",
Severity: hcl.DiagWarning,
},
}
}
err := bucket.Validate()
if err != nil {
return nil, hcl.Diagnostics{
&hcl.Diagnostic{
Summary: "Invalid HCP Packer configuration",
Detail: fmt.Sprintf("Packer could not validate the provided "+
"HCP Packer registry configuration. Check the error message for details "+
"or contact HCP Packer support for further assistance.\nError: %s", err),
Severity: hcl.DiagError,
},
}
}
return bucket, nil
}