From 56fab30fa1dc725058cdafaa2925d5309def30ee Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Wed, 10 Apr 2024 17:26:05 -0400 Subject: [PATCH] plugin: factorise calling describe on binaries Given that calling the describe command on plugins and deserialising the output as a plugin description is something done in multiple places in the code, we factorise this operation so we don't need to copy/paste the code around. --- packer/plugin-getter/plugins.go | 22 ++++++++++++++-------- packer/plugin.go | 9 ++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index 80bae5639..752f845fe 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -211,18 +211,12 @@ func (pr Requirement) ListInstallations(opts ListInstallationsOptions) (InstallL } } - descOut, err := exec.Command(path, "describe").Output() + describeInfo, err := GetPluginDescription(path) if err != nil { - log.Printf("couldn't call describe on %q, ignoring", path) + log.Printf("failed to call describe on %q: %s", path, err) continue } - var describeInfo pluginsdk.SetDescription - err = json.Unmarshal(descOut, &describeInfo) - if err != nil { - log.Printf("%q: describe output deserialization error %q, ignoring", path, err) - } - // versionsStr now looks like v1.2.3_x5.1 or amazon_v1.2.3_x5.1 parts := strings.SplitN(versionsStr, "_", 2) pluginVersionStr, protocolVersionStr := parts[0], parts[1] @@ -933,6 +927,18 @@ func (pr *Requirement) InstallLatest(opts InstallOptions) (*Installation, error) return nil, errs } +func GetPluginDescription(pluginPath string) (pluginsdk.SetDescription, error) { + out, err := exec.Command(pluginPath, "describe").Output() + if err != nil { + return pluginsdk.SetDescription{}, err + } + + desc := pluginsdk.SetDescription{} + err = json.Unmarshal(out, &desc) + + return desc, err +} + func init() { var err error // Should never error if both components are set diff --git a/packer/plugin.go b/packer/plugin.go index de85071d7..cda4118f5 100644 --- a/packer/plugin.go +++ b/packer/plugin.go @@ -5,7 +5,6 @@ package packer import ( "crypto/sha256" - "encoding/json" "fmt" "log" "os" @@ -127,13 +126,9 @@ func (c *PluginConfig) Discover() error { // if the "packer-plugin-amazon" binary had an "ebs" builder one could use // the "amazon-ebs" builder. func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error { - out, err := exec.Command(pluginPath, "describe").Output() + desc, err := plugingetter.GetPluginDescription(pluginPath) if err != nil { - return err - } - var desc pluginsdk.SetDescription - if err := json.Unmarshal(out, &desc); err != nil { - return err + return fmt.Errorf("failed to get plugin description from executable %q: %s", pluginPath, err) } pluginPrefix := pluginName + "-"