Skip to content

Commit

Permalink
unzip binary
Browse files Browse the repository at this point in the history
  • Loading branch information
abennett committed Nov 25, 2019
1 parent e7b117e commit 1cbd1a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
*.out

hashi-releases
*.zip
*.json
33 changes: 29 additions & 4 deletions releases.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -112,12 +114,35 @@ func (i Index) DownloadBuildForLocal(product, version string) error {
if err = CheckBytes(build.Filename, bts); err != nil {
return err
}
f, err := os.Create(build.Filename)
_, err = ExtractZip(product, "", bts)
return err
}

func ExtractZip(product, parentDir string, bts []byte) (string, error) {
zipReader, err := zip.NewReader(bytes.NewReader(bts), int64(len(bts)))
if err != nil {
return err
return "", err
}
_, err = f.Write(bts)
return err
finalPath := path.Join(parentDir, product)
outFile, err := os.OpenFile(finalPath, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
return "", err
}
var content io.ReadCloser
for _, f := range zipReader.File {
if f.Name == product {
zipFile, err := f.Open()
if err != nil {
return "", err
}
content = zipFile
}
}
_, err = io.Copy(outFile, content)
if err != nil {
return "", nil
}
return finalPath, nil
}

func (b *Build) Download() ([]byte, error) {
Expand Down

0 comments on commit 1cbd1a7

Please sign in to comment.