diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-03-18 00:38:35 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-03-18 00:38:35 -0400 |
commit | f955afc850693247dccb70bf470a3c88b6109b94 (patch) | |
tree | 4751d10df794325917fa0188874d15ded17b8afe /cmd/search.go | |
parent | 43be1706be867bc81bb8ac0b77961d63196669af (diff) | |
download | cvez-f955afc850693247dccb70bf470a3c88b6109b94.tar.bz2 cvez-f955afc850693247dccb70bf470a3c88b6109b94.zip |
cmd: refactor commands
Diffstat (limited to 'cmd/search.go')
-rw-r--r-- | cmd/search.go | 51 |
1 files changed, 2 insertions, 49 deletions
diff --git a/cmd/search.go b/cmd/search.go index ba831df..fd1b011 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -2,47 +2,11 @@ package cmd import ( "context" - "encoding/json" - "github.com/pablotron/cvez/datadir" - "github.com/pablotron/cvez/dbstore" "github.com/spf13/cobra" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" "os" - "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 -} - var searchCmd = &cobra.Command{ Use: "search", Args: cobra.MinimumNArgs(1), @@ -52,10 +16,6 @@ var searchCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { ctx := context.Background() - // set global logging options - zerolog.TimeFieldFormat = time.RFC3339 - zerolog.SetGlobalLevel(zerolog.InfoLevel) - // search for CVEs rows, err := getDb().CveSearch(ctx, getSearchQuery(args)) if err != nil { @@ -63,14 +23,7 @@ var searchCmd = &cobra.Command{ os.Exit(-1) } - // search for CVEs, write result - e := json.NewEncoder(os.Stdout) - if err := e.Encode(rows); err != nil { - log.Error().Err(err).Msg("Encode") - } + // write rows to stdout + jsonEncode(rows) }, } - -func init() { - rootCmd.AddCommand(searchCmd) -} |