Files

157 lines
4.2 KiB
Go
Raw Permalink Normal View History

// Copyright IBM Corp. 2024, 2025
2023-08-10 15:53:29 -07:00
// SPDX-License-Identifier: BUSL-1.1
package command
2013-06-13 10:03:44 -07:00
import (
"context"
"log"
2013-06-13 10:03:44 -07:00
"strings"
2015-05-25 17:29:10 -07:00
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
"github.com/posener/complete"
2013-06-13 10:03:44 -07:00
)
type ValidateCommand struct {
Meta
2013-06-13 10:03:44 -07:00
}
func (c *ValidateCommand) Run(args []string) int {
ctx, cleanup := handleTermInterrupt(c.Ui)
defer cleanup()
cfg, ret := c.ParseArgs(args)
if ret != 0 {
return ret
}
return c.RunContext(ctx, cfg)
}
func (c *ValidateCommand) ParseArgs(args []string) (*ValidateArgs, int) {
var cfg ValidateArgs
2023-10-06 10:48:31 -04:00
flags := c.Meta.FlagSet("validate")
2015-05-25 17:29:10 -07:00
flags.Usage = func() { c.Ui.Say(c.Help()) }
cfg.AddFlagSets(flags)
2015-05-25 17:29:10 -07:00
if err := flags.Parse(args); err != nil {
return &cfg, 1
}
2015-05-25 17:29:10 -07:00
args = flags.Args()
2013-06-13 10:03:44 -07:00
if len(args) != 1 {
2015-05-25 17:29:10 -07:00
flags.Usage()
return &cfg, 1
2013-06-13 10:03:44 -07:00
}
cfg.Path = args[0]
return &cfg, 0
}
2013-06-13 10:03:44 -07:00
func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int {
// Set the release only flag if specified as argument
//
// This deactivates the capacity for Packer to load development binaries.
c.CoreConfig.Components.PluginConfig.ReleasesOnly = cla.ReleaseOnly
// By default we want to inform users of undeclared variables when validating but not during build time.
cla.MetaArgs.WarnOnUndeclaredVar = true
if cla.NoWarnUndeclaredVar {
cla.MetaArgs.WarnOnUndeclaredVar = false
}
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
if ret != 0 {
2013-06-13 10:03:44 -07:00
return 1
}
2015-05-25 17:29:10 -07:00
// If we're only checking syntax, then we're done already
if cla.SyntaxOnly {
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
2013-06-13 10:03:44 -07:00
return 0
}
2023-07-11 15:41:37 -04:00
diags := packerStarter.DetectPluginBinaries()
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
}
if packer.PackerUseProto {
log.Printf("[TRACE] Using protobuf for communication with plugins")
}
2023-07-11 15:41:37 -04:00
diags = packerStarter.Initialize(packer.InitializeOptions{
SkipDatasourcesExecution: !cla.EvaluateDatasources,
UseSequential: cla.UseSequential,
2021-01-20 10:37:16 +01:00
})
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
}
_, diags = packerStarter.GetBuilds(packer.GetBuildsOptions{
Only: cla.Only,
Except: cla.Except,
})
2015-05-25 17:29:10 -07:00
fixerDiags := packerStarter.FixConfig(packer.FixConfigOptions{
Mode: packer.Diff,
})
diags = append(diags, fixerDiags...)
2013-06-13 10:03:44 -07:00
ret = writeDiags(c.Ui, nil, diags)
if ret == 0 {
c.Ui.Say("The configuration is valid.")
}
return ret
2013-06-13 10:03:44 -07:00
}
func (*ValidateCommand) Help() string {
helpText := `
Usage: packer validate [options] TEMPLATE
Checks the template is valid by parsing the template and also
checking the configuration with the various builders, provisioners, etc.
If it is not valid, the errors will be shown and the command will exit
with a non-zero exit status. If it is valid, it will exit with a zero
exit status.
Options:
-syntax-only Only check syntax. Do not verify config of the template.
-except=foo,bar,baz Validate all builds other than these.
-only=foo,bar,baz Validate only these builds.
-machine-readable Produce machine-readable output.
-var 'key=value' Variable for templates, can be used multiple times.
-var-file=path JSON or HCL2 file containing user variables, can be used multiple times.
-no-warn-undeclared-var Disable warnings for user variable files containing undeclared variables.
-evaluate-datasources Evaluate data sources during validation (HCL2 only, may incur costs); Defaults to false.
-ignore-prerelease-plugins Disable the loading of prerelease plugin binaries (x.y.z-dev).
-use-sequential-evaluation Fallback to using a sequential approach for local/datasource evaluation.
`
return strings.TrimSpace(helpText)
}
func (*ValidateCommand) Synopsis() string {
2013-06-13 10:03:44 -07:00
return "check that a template is valid"
}
func (*ValidateCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (*ValidateCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
"-syntax-only": complete.PredictNothing,
"-except": complete.PredictNothing,
"-only": complete.PredictNothing,
"-var": complete.PredictNothing,
"-machine-readable": complete.PredictNothing,
"-var-file": complete.PredictNothing,
}
}