From f955afc850693247dccb70bf470a3c88b6109b94 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 18 Mar 2022 00:38:35 -0400 Subject: cmd: refactor commands --- cmd/cmd.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/cvss.go | 18 +---------------- cmd/search.go | 51 ++---------------------------------------------- cmd/update.go | 10 ---------- 4 files changed, 65 insertions(+), 76 deletions(-) (limited to 'cmd') diff --git a/cmd/cmd.go b/cmd/cmd.go index faf2b75..4b09d18 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,2 +1,64 @@ // Command-line interface package cmd + +import ( + "encoding/json" + "github.com/pablotron/cvez/datadir" + "github.com/pablotron/cvez/dbstore" + "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 +} + +// 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) +} diff --git a/cmd/cvss.go b/cmd/cvss.go index d704811..0076ccd 100644 --- a/cmd/cvss.go +++ b/cmd/cvss.go @@ -1,13 +1,8 @@ package cmd import ( - "encoding/json" "github.com/pablotron/cvez/cvss" "github.com/spf13/cobra" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "os" - "time" ) var cvssCmd = &cobra.Command{ @@ -16,18 +11,7 @@ var cvssCmd = &cobra.Command{ Long: `Common Vulnerability Scoring System (CVSS) vector calculator.`, Run: func(cmd *cobra.Command, args []string) { - // set global logging options - zerolog.TimeFieldFormat = time.RFC3339 - zerolog.SetGlobalLevel(zerolog.InfoLevel) - // parse args, write result - e := json.NewEncoder(os.Stdout) - if err := e.Encode(cvss.Calc(args)); err != nil { - log.Error().Err(err).Msg("Encode") - } + jsonEncode(cvss.Calc(args)) }, } - -func init() { - rootCmd.AddCommand(cvssCmd) -} 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) -} diff --git a/cmd/update.go b/cmd/update.go index 0714abe..7047fd9 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -4,9 +4,7 @@ import ( // "encoding/json" "github.com/pablotron/cvez/nvdmirror" "github.com/spf13/cobra" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "time" ) var updateCmd = &cobra.Command{ @@ -16,10 +14,6 @@ var updateCmd = &cobra.Command{ Long: `Update local NVD mirror.`, Run: func(cmd *cobra.Command, args []string) { - // set global logging options - zerolog.TimeFieldFormat = time.RFC3339 - zerolog.SetGlobalLevel(zerolog.InfoLevel) - // create cache cache, err := nvdmirror.NewJsonCache("stuff/cache.json.gz") if err != nil { @@ -38,7 +32,3 @@ var updateCmd = &cobra.Command{ nvdmirror.Sync(config, &cache, "stuff") }, } - -func init() { - rootCmd.AddCommand(updateCmd) -} -- cgit v1.2.3