Skip to content

Commit

Permalink
feat: extract batched plugin archive
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Feb 4, 2023
1 parent 8f1db0a commit 4283738
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pkg/plugin/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func (d *Discovery) FindPluginsWithBatchResolver(resolverName string, pInfos []*
if err != nil {
return err
}
// TODO
_ = batchDownloadInfo
return nil

return downloadBatchPlugins(missingPlugins, batchDownloadInfo, d.config.ShowProgress)
}
75 changes: 75 additions & 0 deletions pkg/plugin/discovery/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,78 @@ func downloadPlugin(pluginInfo *plugin.Info, downloadInfo *resolver.PluginDownlo
}
return targetFile, nil
}

//gocyclo:ignore
func downloadBatchPlugins(pluginInfos []*plugin.Info, downloadInfo *resolver.BatchPluginDownloadInfo, showProgress bool) error {
req, err := grab.NewRequest(PluginDir, downloadInfo.URL)
if err != nil {
return err
}
if downloadInfo.Checksum != "" {
sum, decErr := hex.DecodeString(downloadInfo.Checksum)
if decErr != nil {
return fmt.Errorf("could not decode checksum: %w", decErr)
}
req.SetChecksum(sha256.New(), sum, true)
}

res := grab.DefaultClient.Do(req)
if showProgress {
showDownloadProgressBar("batched-plugins", res)
}
err = res.Err()
if err != nil {
return err
}
defer os.Remove(res.Filename)

tgzFile, err := os.Open(res.Filename)
if err != nil {
return err
}
defer tgzFile.Close()

gunzip, err := gzip.NewReader(tgzFile)
if err != nil {
return err
}
defer gunzip.Close()

tarReader := tar.NewReader(gunzip)
for {
header, tarErr := tarReader.Next()
if errors.Is(tarErr, io.EOF) {
break
}
if tarErr != nil {
return tarErr
}
if header.Typeflag != tar.TypeReg {
continue
}

outFileName := path.Join(PluginDir, header.Name)
outDirName := path.Dir(outFileName)
if err = os.MkdirAll(outDirName, 0o755); err != nil {
return err
}

outFile, oErr := os.OpenFile(outFileName, os.O_CREATE|os.O_WRONLY, 0o755)
if oErr != nil {
return oErr
}
_, cErr := io.Copy(outFile, tarReader)
_ = outFile.Close()
if cErr != nil {
return cErr
}

for _, pluginInfo := range pluginInfos {
if strings.HasPrefix(path.Join(PluginDir, header.Name), pluginInfo.PluginPath) {
pluginInfo.BinPath = outFileName
}
}

}
return nil
}
2 changes: 1 addition & 1 deletion pkg/plugin/discovery/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PluginDir = ".semrel"
var osArchDir = runtime.GOOS + "_" + runtime.GOARCH

func setAndEnsurePluginPath(pluginInfo *plugin.Info) error {
pluginPath := path.Join(PluginDir, osArchDir, pluginInfo.NormalizedName)
pluginPath := path.Join(PluginDir, osArchDir, pluginInfo.ShortNormalizedName)
if _, err := os.Stat(pluginPath); os.IsNotExist(err) {
err = os.MkdirAll(pluginPath, 0o755)
if err != nil {
Expand Down

0 comments on commit 4283738

Please sign in to comment.