Skip to content

Commit

Permalink
Detect 404 not found error when downloading using curl
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Feb 15, 2024
1 parent 51a07a3 commit f789af1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,29 @@ func (l *Linux) TempDir(h os.Host) (string, error) {
return h.ExecOutput("mktemp -d")
}

var trailingNumberRegex = regexp.MustCompile(`(\d+)$`)

func trailingNumber(s string) (int, bool) {
match := trailingNumberRegex.FindStringSubmatch(s)
if len(match) > 0 {
i, err := strconv.Atoi(match[1])
if err == nil {
return i, true
}
}
return 0, false
}

// DownloadURL performs a download from a URL on the host
func (l *Linux) DownloadURL(h os.Host, url, destination string, opts ...exec.Option) error {
return h.Exec(fmt.Sprintf(`curl -sSLf -o %s %s`, shellescape.Quote(destination), shellescape.Quote(url)), opts...)
err := h.Exec(fmt.Sprintf(`curl -sSLf -o %s %s`, shellescape.Quote(destination), shellescape.Quote(url)), opts...)
if err != nil {
if exitCode, ok := trailingNumber(err.Error()); ok && exitCode == 22 {
return fmt.Errorf("download failed: http 404 - not found: %w", err)
}
return fmt.Errorf("download failed: %w", err)
}
return nil
}

// DownloadK0s performs k0s binary download from github on the host
Expand Down

0 comments on commit f789af1

Please sign in to comment.