From c84a30cef2837f1fa5144acf14c13a6446a855d5 Mon Sep 17 00:00:00 2001 From: etsai-stripe <88805634+etsai-stripe@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:11:27 -0700 Subject: [PATCH] clean up duplicated plugin versions from archive install (#953) --- pkg/plugins/plugin.go | 9 ++++----- pkg/plugins/utilities.go | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/pkg/plugins/plugin.go b/pkg/plugins/plugin.go index 33462bbd3..80d1d1057 100644 --- a/pkg/plugins/plugin.go +++ b/pkg/plugins/plugin.go @@ -47,11 +47,10 @@ type PluginList struct { // Release is the type that holds release data for a specific build of a plugin type Release struct { - Arch string `toml:"Arch"` - OS string `toml:"OS"` - Version string `toml:"Version"` - Sum string `toml:"Sum"` - Unmanaged bool `toml:"Unmanaged"` + Arch string `toml:"Arch"` + OS string `toml:"OS"` + Version string `toml:"Version"` + Sum string `toml:"Sum"` } // getPluginInterface computes the correct metadata needed for starting the hcplugin client diff --git a/pkg/plugins/utilities.go b/pkg/plugins/utilities.go index f6d339621..538a11480 100644 --- a/pkg/plugins/utilities.go +++ b/pkg/plugins/utilities.go @@ -149,7 +149,10 @@ func AddEntryToPluginManifest(ctx context.Context, config config.IConfig, fs afe entryRelease := entry.Releases[0] foundRelease := false for _, release := range plugin.Releases { - if release.Version == entryRelease.Version && release.Unmanaged == entryRelease.Unmanaged { + if release.Version == entryRelease.Version { + if release.Sum != entryRelease.Sum { + return fmt.Errorf("version '%s' is already installed but checksums do not match", release.Version) + } foundRelease = true break } @@ -343,18 +346,23 @@ func extractAndInstall(ctx context.Context, config config.IConfig, tarReader *ta case tar.TypeDir: continue case tar.TypeReg: - if name == "manifest.toml" { + filename := filepath.Base(name) + if strings.HasPrefix(filename, "._") { + continue + } + + if filename == "manifest.toml" { tomlBytes, _ := io.ReadAll(tarReader) err = toml.Unmarshal(tomlBytes, &manifest) if err != nil { return err } - fmt.Println(color.Green(fmt.Sprintf("✔ extracted manifest '%s'", name))) - } else if strings.Contains(name, "stripe-cli-") { - extractedPluginName = name + fmt.Println(color.Green(fmt.Sprintf("✔ extracted manifest '%s'", filename))) + } else if strings.Contains(filename, "stripe-cli-") { + extractedPluginName = filename pluginData, _ = io.ReadAll(tarReader) - fmt.Println(color.Green(fmt.Sprintf("✔ extracted plugin '%s'", name))) + fmt.Println(color.Green(fmt.Sprintf("✔ extracted plugin '%s'", filename))) } default: @@ -365,7 +373,6 @@ func extractAndInstall(ctx context.Context, config config.IConfig, tarReader *ta // update plugin manifest and config manifest if len(manifest.Plugins) == 1 && len(manifest.Plugins[0].Releases) == 1 && len(pluginData) > 0 { plugin := manifest.Plugins[0] - plugin.Releases[0].Unmanaged = true if extractedPluginName != plugin.Binary { return fmt.Errorf( @@ -379,10 +386,14 @@ func extractAndInstall(ctx context.Context, config config.IConfig, tarReader *ta return err } - err = plugin.verifychecksumAndSavePlugin(pluginData, config, fs, plugin.Releases[0].Version) + version := plugin.Releases[0].Version + err = plugin.verifychecksumAndSavePlugin(pluginData, config, fs, version) if err != nil { return err } + + // clean up other plugin versions + plugin.cleanUpPluginPath(config, fs, version) } else { return fmt.Errorf("missing required manifest.toml or plugin in the archive") }