11package fileutils
22
33import (
4+ "errors"
45 "fmt"
56 "path"
67
@@ -9,10 +10,13 @@ import (
910 "github.com/sirupsen/logrus"
1011)
1112
13+ // ErrSkipped is returned when the downloader did not attempt to download the specified file.
14+ var ErrSkipped = errors .New ("skipped to download" )
15+
1216// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
1317func DownloadFile (dest string , f limayaml.File , decompress bool , description string , expectedArch limayaml.Arch ) (string , error ) {
1418 if f .Arch != expectedArch {
15- return "" , fmt .Errorf ("unsupported arch: %q" , f .Arch )
19+ return "" , fmt .Errorf ("%w: %q: unsupported arch: %q" , ErrSkipped , f . Location , f .Arch )
1620 }
1721 fields := logrus.Fields {"location" : f .Location , "arch" : f .Arch , "digest" : f .Digest }
1822 logrus .WithFields (fields ).Infof ("Attempting to download %s" , description )
@@ -36,3 +40,25 @@ func DownloadFile(dest string, f limayaml.File, decompress bool, description str
3640 }
3741 return res .CachePath , nil
3842}
43+
44+ // Errors compose multiple into a single error.
45+ // Errors filters out ErrSkipped.
46+ func Errors (errs []error ) error {
47+ var finalErr error
48+ for _ , err := range errs {
49+ if errors .Is (err , ErrSkipped ) {
50+ logrus .Debug (err )
51+ } else {
52+ if finalErr == nil {
53+ finalErr = err
54+ } else {
55+ finalErr = fmt .Errorf ("%v, %w" , finalErr , err )
56+ }
57+ }
58+ }
59+ if len (errs ) > 0 && finalErr == nil {
60+ // errs only contains ErrSkipped
61+ finalErr = fmt .Errorf ("%v" , errs )
62+ }
63+ return finalErr
64+ }
0 commit comments