mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-21 15:31:41 -04:00
This is the first step in attempting to read a Packer configuration file via the packer init command. At the present moment it will try to read one or more configuration templates from a given directory or path. Once parsed it will error if parsing fails or exist successfully if it is able to parse the file. Looking at how the code is structured there will need to be changes made to the following places: - When no configuration file is found Packer will display an error. That error should be bubbled up a bit so that the caller command can determine if it should be displayed or not. For packer init no configuration is not an error. Maybe it should be? - After a configuration has been parsed there needs to be a single way to determine a list of plugins associated with the configuration. HCL and JSON configs have fields for this data but some is exported and some is unexported. Adapting the packerHandler interface may be an option here. More investigation needed.
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/hashicorp/packer/version"
|
|
)
|
|
|
|
// InitCommand initializes a Packer working directory.
|
|
type InitCommand struct {
|
|
Meta
|
|
}
|
|
|
|
func (c *InitCommand) Run(args []string) int {
|
|
ctx, cleanup := handleTermInterrupt(c.Ui)
|
|
defer cleanup()
|
|
|
|
cla, ret := c.ParseArgs(args)
|
|
if ret != 0 {
|
|
return ret
|
|
}
|
|
|
|
return c.RunContext(ctx, cla)
|
|
}
|
|
|
|
func (c *InitCommand) ParseArgs(args []string) (*InitArgs, int) {
|
|
var cfg InitArgs
|
|
flags := c.Meta.FlagSet("init", 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) > 0 {
|
|
cfg.Path = args[0]
|
|
}
|
|
|
|
// Init command should treat an empty invocation as if it was run
|
|
// against the current working directory.
|
|
if cfg.Path == "" {
|
|
cfg.Path, _ = os.Getwd()
|
|
}
|
|
|
|
return &cfg, 0
|
|
}
|
|
|
|
func (c *InitCommand) RunContext(ctx context.Context, cla *InitArgs) int {
|
|
|
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
|
if ret != 0 {
|
|
fmt.Printf(`%s
|
|
|
|
Packer initialized with no template!
|
|
|
|
The directory has no Packer templates. You may begin working
|
|
with Packer immediately by creating a Packer template.
|
|
`, version.FormattedVersion())
|
|
return ret
|
|
}
|
|
|
|
_ = packerStarter.Initialize()
|
|
|
|
return 0
|
|
}
|
|
|
|
func (c *InitCommand) Help() string {
|
|
helpText := `
|
|
Usage: packer init [options]
|
|
|
|
Initialize a new or existing Packer working directory by downloading
|
|
builders, provisioners, and post-processors defined in the template.
|
|
|
|
This is the first command that should be executed when working with a new
|
|
or existing template. Running this command in an empty directory will
|
|
will perform no operation, and will need to be executed once a template
|
|
has been created to initialize the working directory.
|
|
|
|
It is safe to run init multiple times on a template to update the builders,
|
|
provisioners, post-processors with changes in the template configuration file.
|
|
|
|
Options:
|
|
-get-plugins=false Skips plugin installation.
|
|
-plugin-dir=PATH Skips plugin installation and loads plugins only from the specified directory.
|
|
-upgrade=true Updates installed plugins to the latest available version.
|
|
`
|
|
return helpText
|
|
|
|
}
|
|
|
|
func (c *InitCommand) Synopsis() string {
|
|
return "Initializes a Packer working directory"
|
|
}
|