From 4be7e384748de9b7d17e17089239c9675511cf73 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 26 Jul 2022 09:30:30 -0400 Subject: [PATCH] command: move GetConfig methods to meta Since the GetConfig methods are common to all the commands of Packer that rely on a config to work, we move them from build.go to meta.go in order to make the intent clearer. --- command/build.go | 67 ----------------------------------------------- command/meta.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/command/build.go b/command/build.go index b90a176fd..674b08259 100644 --- a/command/build.go +++ b/command/build.go @@ -12,12 +12,9 @@ import ( "time" "github.com/hashicorp/hcl/v2" - "github.com/hashicorp/hcl/v2/hclparse" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" - "github.com/hashicorp/packer-plugin-sdk/template" "github.com/hashicorp/packer/hcl2template" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/version" "golang.org/x/sync/semaphore" "github.com/hako/durafmt" @@ -62,17 +59,6 @@ func (c *BuildCommand) ParseArgs(args []string) (*BuildArgs, int) { return &cfg, 0 } -func (m *Meta) GetConfigFromHCL(cla *MetaArgs) (*hcl2template.PackerConfig, int) { - parser := &hcl2template.Parser{ - CorePackerVersion: version.SemVer, - CorePackerVersionString: version.FormattedVersion(), - Parser: hclparse.NewParser(), - PluginConfig: m.CoreConfig.Components.PluginConfig, - } - cfg, diags := parser.Parse(cla.Path, cla.VarFiles, cla.Vars) - return cfg, writeDiags(m.Ui, parser.Files(), diags) -} - func writeDiags(ui packersdk.Ui, files map[string]*hcl.File, diags hcl.Diagnostics) int { // write HCL errors/diagnostics if any. b := bytes.NewBuffer(nil) @@ -91,59 +77,6 @@ func writeDiags(ui packersdk.Ui, files map[string]*hcl.File, diags hcl.Diagnosti return 0 } -func (m *Meta) GetConfig(cla *MetaArgs) (packer.Handler, int) { - cfgType, err := cla.GetConfigType() - if err != nil { - m.Ui.Error(fmt.Sprintf("%q: %s", cla.Path, err)) - return nil, 1 - } - - switch cfgType { - case ConfigTypeHCL2: - // TODO(azr): allow to pass a slice of files here. - return m.GetConfigFromHCL(cla) - default: - // TODO: uncomment once we've polished HCL a bit more. - // c.Ui.Say(`Legacy JSON Configuration Will Be Used. - // The template will be parsed in the legacy configuration style. This style - // will continue to work but users are encouraged to move to the new style. - // See: https://packer.io/guides/hcl - // `) - return m.GetConfigFromJSON(cla) - } -} - -func (m *Meta) GetConfigFromJSON(cla *MetaArgs) (packer.Handler, int) { - // Parse the template - var tpl *template.Template - var err error - if cla.Path == "" { - // here cla validation passed so this means we want a default builder - // and we probably are in the console command - tpl, err = template.Parse(TiniestBuilder) - } else { - tpl, err = template.ParseFile(cla.Path) - } - - if err != nil { - m.Ui.Error(fmt.Sprintf("Failed to parse file as legacy JSON template: "+ - "if you are using an HCL template, check your file extensions; they "+ - "should be either *.pkr.hcl or *.pkr.json; see the docs for more "+ - "details: https://www.packer.io/docs/templates/hcl_templates. \n"+ - "Original error: %s", err)) - return nil, 1 - } - - // Get the core - core, err := m.Core(tpl, cla) - ret := 0 - if err != nil { - m.Ui.Error(err.Error()) - ret = 1 - } - return &CoreWrapper{core}, ret -} - func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int { packerStarter, ret := c.GetConfig(&cla.MetaArgs) if ret != 0 { diff --git a/command/meta.go b/command/meta.go index 53b5dabd2..bf5a6d662 100644 --- a/command/meta.go +++ b/command/meta.go @@ -3,14 +3,18 @@ package command import ( "bufio" "flag" + "fmt" "io" "os" + "github.com/hashicorp/hcl/v2/hclparse" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/template" kvflag "github.com/hashicorp/packer/command/flag-kv" + "github.com/hashicorp/packer/hcl2template" "github.com/hashicorp/packer/helper/wrappedstreams" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/version" ) // FlagSetFlags is an enum to define what flags are present in the @@ -103,3 +107,67 @@ func (m *Meta) StdinPiped() bool { return fi.Mode()&os.ModeNamedPipe != 0 } + +func (m *Meta) GetConfig(cla *MetaArgs) (packer.Handler, int) { + cfgType, err := cla.GetConfigType() + if err != nil { + m.Ui.Error(fmt.Sprintf("%q: %s", cla.Path, err)) + return nil, 1 + } + + switch cfgType { + case ConfigTypeHCL2: + // TODO(azr): allow to pass a slice of files here. + return m.GetConfigFromHCL(cla) + default: + // TODO: uncomment once we've polished HCL a bit more. + // c.Ui.Say(`Legacy JSON Configuration Will Be Used. + // The template will be parsed in the legacy configuration style. This style + // will continue to work but users are encouraged to move to the new style. + // See: https://packer.io/guides/hcl + // `) + return m.GetConfigFromJSON(cla) + } +} + +func (m *Meta) GetConfigFromHCL(cla *MetaArgs) (*hcl2template.PackerConfig, int) { + parser := &hcl2template.Parser{ + CorePackerVersion: version.SemVer, + CorePackerVersionString: version.FormattedVersion(), + Parser: hclparse.NewParser(), + PluginConfig: m.CoreConfig.Components.PluginConfig, + } + cfg, diags := parser.Parse(cla.Path, cla.VarFiles, cla.Vars) + return cfg, writeDiags(m.Ui, parser.Files(), diags) +} + +func (m *Meta) GetConfigFromJSON(cla *MetaArgs) (packer.Handler, int) { + // Parse the template + var tpl *template.Template + var err error + if cla.Path == "" { + // here cla validation passed so this means we want a default builder + // and we probably are in the console command + tpl, err = template.Parse(TiniestBuilder) + } else { + tpl, err = template.ParseFile(cla.Path) + } + + if err != nil { + m.Ui.Error(fmt.Sprintf("Failed to parse file as legacy JSON template: "+ + "if you are using an HCL template, check your file extensions; they "+ + "should be either *.pkr.hcl or *.pkr.json; see the docs for more "+ + "details: https://www.packer.io/docs/templates/hcl_templates. \n"+ + "Original error: %s", err)) + return nil, 1 + } + + // Get the core + core, err := m.Core(tpl, cla) + ret := 0 + if err != nil { + m.Ui.Error(err.Error()) + ret = 1 + } + return &CoreWrapper{core}, ret +}