Add support for reading configs
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.
This commit is contained in:
+3
-1
@@ -146,12 +146,14 @@ type HCL2UpgradeArgs struct {
|
||||
func (ia *InitArgs) AddFlagSets(flags *flag.FlagSet) {
|
||||
flags.StringVar(&ia.PluginDir, "plugin-dir", "", "")
|
||||
flags.BoolVar(&ia.GetPlugins, "get-plugins", true, "")
|
||||
flags.BoolVar(&ia.Upgrade, "upgrade", false, "")
|
||||
ia.MetaArgs.AddFlagSets(flags)
|
||||
}
|
||||
|
||||
//InitArgs respresents a parsed cli line for packer init
|
||||
type InitArgs struct {
|
||||
MetaArgs
|
||||
PluginDir string
|
||||
GetPlugins bool
|
||||
PluginDir string
|
||||
Upgrade bool
|
||||
}
|
||||
|
||||
+23
-3
@@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/hashicorp/packer/version"
|
||||
)
|
||||
@@ -33,17 +34,35 @@ func (c *InitCommand) ParseArgs(args []string) (*InitArgs, int) {
|
||||
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 {
|
||||
fmt.Printf(`%s
|
||||
|
||||
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
|
||||
}
|
||||
@@ -64,8 +83,9 @@ Usage: packer init [options]
|
||||
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.
|
||||
-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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user