/
db_list.go
77 lines (63 loc) · 1.91 KB
/
db_list.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
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/nextlinux/govulners/govulners/db"
)
var dbListOutputFormat string
var dbListCmd = &cobra.Command{
Use: "list",
Short: "list all DBs available according to the listing URL",
Args: cobra.ExactArgs(0),
RunE: runDBListCmd,
}
func init() {
dbListCmd.Flags().StringVarP(&dbListOutputFormat, "output", "o", "text", "format to display results (available=[text, raw, json])")
dbCmd.AddCommand(dbListCmd)
}
func runDBListCmd(_ *cobra.Command, _ []string) error {
dbCurator, err := db.NewCurator(appConfig.DB.ToCuratorConfig())
if err != nil {
return err
}
listing, err := dbCurator.ListingFromURL()
if err != nil {
return err
}
supportedSchema := dbCurator.SupportedSchema()
available, exists := listing.Available[supportedSchema]
if len(available) == 0 || !exists {
return stderrPrintLnf("No databases available for the current schema (%d)", supportedSchema)
}
switch dbListOutputFormat {
case "text":
// summarize each listing entry for the current DB schema
for _, l := range available {
fmt.Printf("Built: %s\n", l.Built)
fmt.Printf("URL: %s\n", l.URL)
fmt.Printf("Checksum: %s\n\n", l.Checksum)
}
fmt.Printf("%d databases available for schema %d\n", len(available), supportedSchema)
case "json":
// show entries for the current schema
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(&available); err != nil {
return fmt.Errorf("failed to db listing information: %+v", err)
}
case "raw":
// show the entire listing file
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(&listing); err != nil {
return fmt.Errorf("failed to db listing information: %+v", err)
}
default:
return fmt.Errorf("unsupported output format: %s", dbListOutputFormat)
}
return nil
}