Skip to content

Commit

Permalink
wip downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
arbll committed Dec 18, 2023
1 parent 5c77529 commit 122addd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/updater/packaging/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
"io"
"net/http"
"os"
"path/filepath"

"github.com/mholt/archiver/v3"
)

const (
agentArchiveFileName = "agent.tar.gz"
)

// Downloader is the downloader used by the updater to download packages.
type Downloader struct {
client *http.Client
Expand Down Expand Up @@ -40,12 +45,23 @@ func (d *Downloader) Download(ctx context.Context, url string, expectedSHA256 []
defer resp.Body.Close()
hashWriter := sha256.New()
reader := io.TeeReader(resp.Body, hashWriter)
archivePath := filepath.Join(tmpDir, agentArchiveFileName)
archiveFile, err := os.Create(archivePath)
if err != nil {
return fmt.Errorf("could not create archive file: %w", err)
}
defer archiveFile.Close()
_, err = io.Copy(archiveFile, reader)
if err != nil {
return fmt.Errorf("could not write archive file: %w", err)
}
sha256 := hashWriter.Sum(nil)
if !bytes.Equal(expectedSHA256, sha256) {
return fmt.Errorf("invalid hash for %s: expected %x, got %x", url, expectedSHA256, sha256)
}
archive := archiver.NewTarGz()
archive.Open(reader, 0)
archive.Walk(archive string, walkFn archiver.WalkFunc)
return "", nil
err = archiver.Extract(archivePath, "/", destinationPath)
if err != nil {
return fmt.Errorf("could not extract archive: %w", err)
}
return nil
}
1 change: 1 addition & 0 deletions pkg/updater/packaging/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package packaging

0 comments on commit 122addd

Please sign in to comment.