Skip to content

Commit

Permalink
clean up duplicated plugin versions from archive install (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
etsai-stripe committed Aug 24, 2022
1 parent 7b5b97d commit c84a30c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
9 changes: 4 additions & 5 deletions pkg/plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions pkg/plugins/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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")
}
Expand Down

0 comments on commit c84a30c

Please sign in to comment.