2014-10-27 23:31:02 -04:00
|
|
|
package command
|
2013-08-13 12:36:40 -04:00
|
|
|
|
|
|
|
import (
|
2020-06-23 05:58:57 -04:00
|
|
|
"context"
|
2013-08-13 12:36:40 -04:00
|
|
|
"strings"
|
2015-05-26 12:38:09 -04:00
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-10-13 14:42:22 -04:00
|
|
|
"github.com/posener/complete"
|
2013-08-13 12:36:40 -04:00
|
|
|
)
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
type InspectCommand struct {
|
2014-10-27 23:31:02 -04:00
|
|
|
Meta
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 23:31:02 -04:00
|
|
|
func (c *InspectCommand) Run(args []string) int {
|
2020-06-23 05:58:57 -04:00
|
|
|
ctx := context.Background()
|
2013-08-13 12:36:40 -04:00
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
cfg, ret := c.ParseArgs(args)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
return c.RunContext(ctx, cfg)
|
|
|
|
}
|
2013-12-11 16:48:18 -05:00
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
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
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) == 1 {
|
|
|
|
cfg.Path = args[0]
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
2020-06-23 05:58:57 -04:00
|
|
|
return &cfg, 0
|
|
|
|
}
|
2013-08-13 12:36:40 -04:00
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
func (c *InspectCommand) RunContext(ctx context.Context, cla *InspectArgs) int {
|
|
|
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
2020-08-27 05:54:36 -04:00
|
|
|
|
|
|
|
// here we ignore init diags to allow unknown variables to be used
|
2021-01-20 04:37:16 -05:00
|
|
|
_ = packerStarter.Initialize(packer.InitializeOptions{})
|
2020-08-27 05:54:36 -04:00
|
|
|
|
2020-06-23 05:58:57 -04:00
|
|
|
return packerStarter.InspectConfig(packer.InspectConfigOptions{
|
|
|
|
Ui: c.Ui,
|
|
|
|
})
|
2013-08-13 12:36:40 -04:00
|
|
|
}
|
2014-10-27 23:31:02 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
2017-10-13 14:42:22 -04:00
|
|
|
|
|
|
|
func (c *InspectCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InspectCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
|
|
|
"-machine-readable": complete.PredictNothing,
|
|
|
|
}
|
|
|
|
}
|