packer-cn/command/inspect.go
Adrien Delorme 44616d3bff
refactor initialization out from packer configs + tests (#9627)
The initialization of packer core in JSON also validates that `null` variables were set, except in the case of `packer validate --syntax-only` , but after the refactor to allow to have all commands work with HCL2 and JSON this subtlety was lost.

This refactors the initialisation of the core in order to allow to have `packer validate --syntax-only` not error in case a variable is not set. Since these calls are refactored this works for HCL2 too.

fix #9478
2020-07-24 10:58:03 +02:00

86 lines
1.7 KiB
Go

package command
import (
"context"
"strings"
"github.com/hashicorp/packer/packer"
"github.com/posener/complete"
)
type InspectCommand struct {
Meta
}
func (c *InspectCommand) Run(args []string) int {
ctx := context.Background()
cfg, ret := c.ParseArgs(args)
if ret != 0 {
return ret
}
return c.RunContext(ctx, cfg)
}
func (c *InspectCommand) ParseArgs(args []string) (*InspectArgs, int) {
var cfg InspectArgs
flags := c.Meta.FlagSet("inspect", FlagSetVars)
flags.Usage = func() { c.Ui.Say(c.Help()) }
cfg.AddFlagSets(flags)
if err := flags.Parse(args); err != nil {
return &cfg, 1
}
args = flags.Args()
if len(args) == 1 {
cfg.Path = args[0]
}
return &cfg, 0
}
func (c *InspectCommand) RunContext(ctx context.Context, cla *InspectArgs) int {
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
if ret != 0 {
return ret
}
diags := packerStarter.Initialize()
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
}
return packerStarter.InspectConfig(packer.InspectConfigOptions{
Ui: c.Ui,
})
}
func (*InspectCommand) Help() string {
helpText := `
Usage: packer inspect TEMPLATE
Inspects a template, parsing and outputting the components a template
defines. This does not validate the contents of a template (other than
basic syntax by necessity).
Options:
-machine-readable Machine-readable output
`
return strings.TrimSpace(helpText)
}
func (c *InspectCommand) Synopsis() string {
return "see components of a template"
}
func (c *InspectCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *InspectCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
"-machine-readable": complete.PredictNothing,
}
}