github.com/hashicorp/packer@v1.14.3/command/inspect.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package command 5 6 import ( 7 "context" 8 "strings" 9 10 "github.com/hashicorp/packer/packer" 11 "github.com/posener/complete" 12 ) 13 14 type InspectCommand struct { 15 Meta 16 } 17 18 func (c *InspectCommand) Run(args []string) int { 19 ctx := context.Background() 20 21 cfg, ret := c.ParseArgs(args) 22 if ret != 0 { 23 return ret 24 } 25 26 return c.RunContext(ctx, cfg) 27 } 28 29 func (c *InspectCommand) ParseArgs(args []string) (*InspectArgs, int) { 30 var cfg InspectArgs 31 flags := c.Meta.FlagSet("inspect") 32 flags.Usage = func() { c.Ui.Say(c.Help()) } 33 cfg.AddFlagSets(flags) 34 if err := flags.Parse(args); err != nil { 35 return &cfg, 1 36 } 37 38 args = flags.Args() 39 if len(args) == 1 { 40 cfg.Path = args[0] 41 } 42 return &cfg, 0 43 } 44 45 func (c *InspectCommand) RunContext(ctx context.Context, cla *InspectArgs) int { 46 packerStarter, ret := c.GetConfig(&cla.MetaArgs) 47 if ret != 0 { 48 return ret 49 } 50 51 // here we ignore init diags to allow unknown variables to be used 52 _ = packerStarter.Initialize(packer.InitializeOptions{ 53 UseSequential: cla.UseSequential, 54 }) 55 56 return packerStarter.InspectConfig(packer.InspectConfigOptions{ 57 Ui: c.Ui, 58 }) 59 } 60 61 func (*InspectCommand) Help() string { 62 helpText := ` 63 Usage: packer inspect TEMPLATE 64 65 Inspects a template, parsing and outputting the components a template 66 defines. This does not validate the contents of a template (other than 67 basic syntax by necessity). 68 69 Options: 70 71 -machine-readable Machine-readable output 72 -use-sequential-evaluation Fallback to using a sequential approach for local/datasource evaluation. 73 ` 74 75 return strings.TrimSpace(helpText) 76 } 77 78 func (c *InspectCommand) Synopsis() string { 79 return "see components of a template" 80 } 81 82 func (c *InspectCommand) AutocompleteArgs() complete.Predictor { 83 return complete.PredictNothing 84 } 85 86 func (c *InspectCommand) AutocompleteFlags() complete.Flags { 87 return complete.Flags{ 88 "-machine-readable": complete.PredictNothing, 89 } 90 }