@@ -2,12 +2,16 @@ package commands
2
2
3
3
import (
4
4
"fmt"
5
+ "path/filepath"
6
+ "strings"
5
7
6
8
"github.com/spf13/cobra"
7
9
8
10
"github.com/anchore/clio"
9
11
"github.com/anchore/grype/cmd/grype/cli/options"
10
- "github.com/anchore/grype/grype/db/legacy/distribution"
12
+ legacyDistribution "github.com/anchore/grype/grype/db/legacy/distribution"
13
+ "github.com/anchore/grype/grype/db/v6/distribution"
14
+ "github.com/anchore/grype/grype/db/v6/installation"
11
15
"github.com/anchore/grype/internal"
12
16
)
13
17
@@ -27,14 +31,44 @@ func DBImport(app clio.Application) *cobra.Command {
27
31
}
28
32
29
33
func runDBImport (opts options.Database , dbArchivePath string ) error {
30
- dbCurator , err := distribution .NewCurator (opts .ToCuratorConfig ())
34
+ // TODO: tui update? better logging?
35
+
36
+ // TODO: we will only support v6 after development is complete
37
+ if isV6DB (dbArchivePath ) {
38
+ return importDB (opts , dbArchivePath )
39
+ }
40
+ return legacyDBImport (opts , dbArchivePath )
41
+ }
42
+
43
+ func importDB (opts options.Database , dbArchivePath string ) error {
44
+ client , err := distribution .NewClient (opts .ToClientConfig ())
45
+ if err != nil {
46
+ return fmt .Errorf ("unable to create distribution client: %w" , err )
47
+ }
48
+ c , err := installation .NewCurator (opts .ToCuratorConfig (), client )
49
+ if err != nil {
50
+ return fmt .Errorf ("unable to create curator: %w" , err )
51
+ }
52
+
53
+ if err := c .Import (dbArchivePath ); err != nil {
54
+ return fmt .Errorf ("unable to import vulnerability database: %w" , err )
55
+ }
56
+ return stderrPrintLnf ("Vulnerability database imported" )
57
+ }
58
+
59
+ func legacyDBImport (opts options.Database , dbArchivePath string ) error {
60
+ dbCurator , err := legacyDistribution .NewCurator (opts .ToLegacyCuratorConfig ())
31
61
if err != nil {
32
62
return err
33
63
}
34
64
35
65
if err := dbCurator .ImportFrom (dbArchivePath ); err != nil {
36
- return fmt .Errorf ("unable to import vulnerability database: %+v " , err )
66
+ return fmt .Errorf ("unable to import vulnerability database: %w " , err )
37
67
}
38
68
39
69
return stderrPrintLnf ("Vulnerability database imported" )
40
70
}
71
+
72
+ func isV6DB (path string ) bool {
73
+ return strings .Contains (filepath .Base (path ), "vulnerability-db_v6" )
74
+ }
0 commit comments