From 1bb154de5abb64aaf1ed56ffff5f8fda4e7a520f Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Wed, 16 Sep 2020 14:46:16 -0400 Subject: [PATCH] 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. --- command/cli.go | 4 +++- command/init.go | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/command/cli.go b/command/cli.go index a7c2d36cc..6be178f35 100644 --- a/command/cli.go +++ b/command/cli.go @@ -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 } diff --git a/command/init.go b/command/init.go index 74fa2310c..5519184b6 100644 --- a/command/init.go +++ b/command/init.go @@ -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