Skip to content

Commit

Permalink
download: move download into its own package so its available for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Aug 22, 2024
1 parent cf3f4ff commit 16d79e9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package outofband
package download

import (
"bytes"
Expand All @@ -24,8 +24,8 @@ var (
ErrFormat = errors.New("bad checksum format")
)

// download fetches the file into dst
func download(ctx context.Context, fileURL, dst string) error {
// FromURLToFile fetches the file into dst
func FromURLToFile(ctx context.Context, fileURL, dst string) error {
// create file
fileHandle, err := os.Create(dst)
if err != nil {
Expand Down Expand Up @@ -65,7 +65,7 @@ func download(ctx context.Context, fileURL, dst string) error {
return err
}

func checksumValidate(filename, checksum string) error {
func ChecksumValidate(filename, checksum string) error {
// no checksum prefix, default to md5sum
if !strings.Contains(checksum, ":") {
return checksumValidateMD5(filename, checksum)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package outofband
package download

import (
"os"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestChecksumValidate(t *testing.T) {

defer os.Remove(binPath)

err = checksumValidate(binPath, tt.checksum)
err = ChecksumValidate(binPath, tt.checksum)
if tt.expectedError != nil {
assert.ErrorIs(t, err, tt.expectedError)
return
Expand Down
6 changes: 3 additions & 3 deletions internal/outofband/action_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/bmc-toolbox/common"
"github.com/hashicorp/go-multierror"
"github.com/metal-toolbox/flasher/internal/device"
"github.com/metal-toolbox/flasher/internal/download"
"github.com/metal-toolbox/flasher/internal/metrics"
"github.com/metal-toolbox/flasher/internal/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -76,7 +77,6 @@ var (
ErrContextCancelled = errors.New("context canceled")
ErrUnexpected = errors.New("unexpected error occurred")
ErrInstalledFirmwareNotEqual = errors.New("installed and expected firmware not equal")
ErrInstalledFirmwareEqual = errors.New("installed and expected firmware are equal, no action necessary")
ErrInstalledVersionUnknown = errors.New("installed version unknown")
ErrComponentNotFound = errors.New("component not identified for firmware install")
ErrRequireHostPoweredOff = errors.New("expected host to be powered off")
Expand Down Expand Up @@ -284,7 +284,7 @@ func (h *handler) downloadFirmware(ctx context.Context) error {
file := filepath.Join(dir, h.firmware.FileName)

// download firmware file
err = download(ctx, h.firmware.URL, file)
err = download.FromURLToFile(ctx, h.firmware.URL, file)
if err != nil {
return err
}
Expand All @@ -301,7 +301,7 @@ func (h *handler) downloadFirmware(ctx context.Context) error {
}

// validate checksum
if err := checksumValidate(file, h.firmware.Checksum); err != nil {
if err := download.ChecksumValidate(file, h.firmware.Checksum); err != nil {
os.RemoveAll(filepath.Dir(file))
return err
}
Expand Down

0 comments on commit 16d79e9

Please sign in to comment.