-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
import.go
144 lines (131 loc) · 2.95 KB
/
import.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/klauspost/compress/zstd"
"github.com/quay/claircore/libvuln"
"github.com/urfave/cli/v2"
"github.com/quay/clair/v4/internal/httputil"
)
// ImportCmd is the "import-updaters" subcommand.
var ImportCmd = &cli.Command{
Name: "import-updaters",
Action: importAction,
Usage: "import updates",
ArgsUsage: "input[.gz|.zst]|-",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "gzip",
Aliases: []string{"g"},
Usage: "Decompress input with gzip.",
},
&cli.BoolFlag{
Name: "zstd",
Aliases: []string{"z"},
Usage: "Decompress input with zstd.",
},
},
Description: `Import updates from a file or HTTP URI.
If the supplied file name ends with ".gz" or ".zst" and neither the "z"
or "g" flag have been supplied, input will be decompressed with gzip or
zstd compression, respectively.
A configuration file is needed to run this command, see 'clairctl help'
for how to specify one.`,
}
func importAction(c *cli.Context) error {
ctx := c.Context
// Read and process the config file.
cfg, err := loadConfig(c.String("config"))
if err != nil {
return err
}
cl, err := httputil.NewClient(ctx, false)
if err != nil {
return err
}
// Setup the input file.
args := c.Args()
if args.Len() != 1 {
return errors.New("need one argument")
}
inName := args.First()
switch {
case c.IsSet("zstd") || c.IsSet("gzip"):
break
case strings.HasSuffix(inName, ".zst"):
c.Set("zstd", "true")
case strings.HasSuffix(inName, ".gz"):
c.Set("gzip", "true")
}
in, err := openInput(ctx, cl, inName)
if err != nil {
return err
}
defer in.Close()
switch {
case c.Bool("zstd"):
dec, err := zstd.NewReader(in)
if err != nil {
return err
}
defer dec.Close()
in = io.NopCloser(dec)
case c.Bool("gzip"):
dec, err := gzip.NewReader(in)
if err != nil {
return err
}
defer func() {
if err := dec.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
in = dec
}
pool, err := pgxpool.Connect(ctx, cfg.Matcher.ConnString)
if err != nil {
return err
}
defer pool.Close()
if err := libvuln.OfflineImport(ctx, pool, in); err != nil {
return err
}
return nil
}
func openInput(ctx context.Context, c *http.Client, n string) (io.ReadCloser, error) {
if n == "-" {
return os.Stdin, nil
}
f, ferr := os.Open(n)
if ferr == nil {
return f, nil
}
u, uerr := url.Parse(n)
if uerr == nil {
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
res, err := c.Do(req)
if err != nil {
if res != nil {
res.Body.Close()
}
return nil, err
}
if res.StatusCode != http.StatusOK {
res.Body.Close()
return nil, fmt.Errorf("unexpected response: %d %s", res.StatusCode, res.Status)
}
return res.Body, nil
}
return nil, fmt.Errorf("error opening input:\n\t%v\n\t%v", ferr, uerr)
}