Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/aws/smithy-go v1.27.4
github.com/briandowns/spinner v1.23.2
github.com/dchest/validator v0.0.0-20191217151620-8e45250f2371
github.com/dustin/go-humanize v1.0.1
github.com/golang-jwt/jwt/v4 v4.5.2
github.com/golang/glog v1.2.5
github.com/google/go-cmp v0.7.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/validator v0.0.0-20191217151620-8e45250f2371 h1:BuLreR1acrosGsW+njS+RxyPgL06rYTkasZA2NAogEo=
github.com/dchest/validator v0.0.0-20191217151620-8e45250f2371/go.mod h1:ZfpgrLR1i3mQWz5fIRfkyMIh9zLOy3MwTc7hUBVPlww=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.7.0 h1:bnQc8+GMnidJZA8zc6lLEAb4xNrIqHwO+9TzqvtQZPo=
github.com/dvsekhvalnov/jose2go v1.7.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
Expand Down
38 changes: 30 additions & 8 deletions pkg/helper/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package helper
import (
"fmt"
"io"
"math"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"

humanize "github.com/dustin/go-humanize"
)

// download will download a url to a local file. It's efficient because it will
Expand Down Expand Up @@ -50,13 +49,21 @@ func Download(url string, filename string) error {
cleanupTempFile()
switch resp.StatusCode {
case http.StatusNotFound:
return fmt.Errorf("download failed: file not found (HTTP %d). The requested file may not exist or the URL may be incorrect. URL: %s", resp.StatusCode, url)
return fmt.Errorf("download failed: file not found (HTTP %d). "+
"The requested file may not exist or the URL may be incorrect. URL: %s",
resp.StatusCode, url)
case http.StatusForbidden:
return fmt.Errorf("download failed: access forbidden (HTTP %d). You may not have permission to access this file. URL: %s", resp.StatusCode, url)
return fmt.Errorf("download failed: access forbidden (HTTP %d). "+
"You may not have permission to access this file. URL: %s",
resp.StatusCode, url)
case http.StatusUnauthorized:
return fmt.Errorf("download failed: authentication required (HTTP %d). Please check your credentials. URL: %s", resp.StatusCode, url)
return fmt.Errorf("download failed: authentication required (HTTP %d). "+
"Please check your credentials. URL: %s",
resp.StatusCode, url)
case http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable:
return fmt.Errorf("download failed: server error (HTTP %d). The server may be temporarily unavailable. Please try again later. URL: %s", resp.StatusCode, url)
return fmt.Errorf("download failed: server error (HTTP %d). "+
"The server may be temporarily unavailable. Please try again later. URL: %s",
resp.StatusCode, url)
default:
return fmt.Errorf("download failed: HTTP %d %s. URL: %s", resp.StatusCode, resp.Status, url)
}
Expand Down Expand Up @@ -103,7 +110,8 @@ func formatDownloadError(err error, url string) error {

// Timeout errors
if strings.Contains(errStr, "timeout") || strings.Contains(errStr, "i/o timeout") {
return fmt.Errorf("connection timeout while downloading from %s\nPlease check your internet connection and try again", url)
return fmt.Errorf("connection timeout while downloading from %s\n"+
"Please check your internet connection and try again", url)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

// Network unreachable
Expand Down Expand Up @@ -154,5 +162,19 @@ func (wc WriteCounter) PrintProgress() {

// Return again and print current status of download
// We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB)
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
fmt.Printf("\rDownloading... %s complete", readableBytes(wc.Total))
}

func readableBytes(b uint64) string {
if b < 10 {
return fmt.Sprintf("%d B", b)
}
const base = 1000.0
e := math.Floor(math.Log(float64(b)) / math.Log(base))
suffix := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}[int(e)]
val := math.Floor(float64(b)/math.Pow(base, e)*10+0.5) / 10
if val < 10 {
return fmt.Sprintf("%.1f %s", val, suffix)
}
return fmt.Sprintf("%.0f %s", val, suffix)
}
33 changes: 33 additions & 0 deletions pkg/helper/download/readable_bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package helper

import (
"math"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("readableBytes", func() {
DescribeTable("formats byte values",
func(input uint64, expected string) {
Expect(readableBytes(input)).To(Equal(expected))
},
Entry("zero bytes", uint64(0), "0 B"),
Entry("9 bytes", uint64(9), "9 B"),
Entry("10 bytes", uint64(10), "10 B"),
Entry("999 bytes", uint64(999), "999 B"),
Entry("1000 bytes (1 kB boundary)", uint64(1000), "1.0 kB"),
Entry("1250 bytes rounds up to 1.3 kB", uint64(1250), "1.3 kB"),
Entry("9999 bytes", uint64(9999), "10 kB"),
Entry("10 kB", uint64(10000), "10 kB"),
Entry("1 MB boundary", uint64(1000000), "1.0 MB"),
Entry("9999999 bytes", uint64(9999999), "10 MB"),
Entry("10 MB", uint64(10000000), "10 MB"),
Entry("1 GB boundary", uint64(1000000000), "1.0 GB"),
Entry("10 GB", uint64(10000000000), "10 GB"),
Entry("1 TB boundary", uint64(1000000000000), "1.0 TB"),
Entry("1 PB boundary", uint64(1000000000000000), "1.0 PB"),
Entry("1 EB boundary", uint64(1000000000000000000), "1.0 EB"),
Entry("max uint64", uint64(math.MaxUint64), "18 EB"),
)
})
21 changes: 0 additions & 21 deletions vendor/github.com/dustin/go-humanize/.travis.yml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/dustin/go-humanize/LICENSE

This file was deleted.

124 changes: 0 additions & 124 deletions vendor/github.com/dustin/go-humanize/README.markdown

This file was deleted.

31 changes: 0 additions & 31 deletions vendor/github.com/dustin/go-humanize/big.go

This file was deleted.

Loading