-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdocker.go
44 lines (37 loc) · 966 Bytes
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package image
import (
"bufio"
"compress/gzip"
"io"
"io/ioutil"
"os"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)
func tryDockerArchive(fileName string) (v1.Image, error) {
img, err := tarball.Image(fileOpener(fileName), nil)
if err != nil {
return nil, xerrors.Errorf("unable to open %s as a Docker image: %w", fileName, err)
}
return img, nil
}
func fileOpener(fileName string) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, xerrors.Errorf("unable to open the file: %w", err)
}
var r io.Reader
br := bufio.NewReader(f)
r = br
if utils.IsGzip(br) {
r, err = gzip.NewReader(br)
if err != nil {
return nil, xerrors.Errorf("failed to open gzip: %w", err)
}
}
return ioutil.NopCloser(r), nil
}
}