aboutsummaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: 1619a71c764e725463761bccb5a378cbd5cacb73 (plain)
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
// Command-line interface
package cmd

import (
  "encoding/json"
  "github.com/pablotron/cvez/datadir"
  "github.com/pablotron/cvez/dbstore"
  "github.com/pablotron/cvez/nvdmirror"
  "github.com/rs/zerolog"
  "github.com/rs/zerolog/log"
  "os"
  "path/filepath"
  "strings"
  "time"
)

// Build query string.
func getSearchQuery(args []string) string {
  r := make([]string, len(args))

  for i, v := range(args) {
    r[i] = "\"" + strings.ReplaceAll(strings.ToLower(v), "\"", "\"\"") + "\""
  }

  return strings.Join(r, " AND ")
}

// Get database store.
func getDb() dbstore.DbStore {
  // build database path
  dbPath, err := datadir.Join("cvez.db")
  if err != nil {
    log.Error().Err(err).Msg("Join")
    os.Exit(-1)
  }

  // open database
  db, err := dbstore.Open(dbPath)
  if err != nil {
    log.Error().Err(err).Msg("Open")
    os.Exit(-1)
  }

  return db
}

// Get cache and cache directory path.
func getCache() (nvdmirror.JsonCache, string) {
  // get path to cache directory
  dir, err := datadir.CacheDir()
  if err != nil {
    log.Error().Err(err).Msg("CacheDir")
    os.Exit(-1)
  }

  // build cache file path
  cachePath := filepath.Join(dir, "cache.json.gz")

  // create cache
  cache, err := nvdmirror.NewJsonCache(cachePath)
  if err != nil {
    log.Error().Err(err).Msg("NewJsonCache")
    os.Exit(-1)
  }

  // return cache
  return cache, dir
}

// JSON encode data and write it to standard output
func jsonEncode(data interface{}) {
  // search for CVEs, write result
  e := json.NewEncoder(os.Stdout)
  if err := e.Encode(data); err != nil {
    log.Error().Err(err).Msg("Encode")
    os.Exit(-1)
  }
}

func init() {
  // set global logging options
  zerolog.TimeFieldFormat = time.RFC3339
  zerolog.SetGlobalLevel(zerolog.InfoLevel)

  rootCmd.AddCommand(searchCmd)
  rootCmd.AddCommand(cisaSearchCmd)
  rootCmd.AddCommand(cvssCmd)
  rootCmd.AddCommand(updateCmd)
}