Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log) no progress when --log-json option #394

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"time"
Expand Down Expand Up @@ -628,7 +629,12 @@ func deleteJvn(tx *gorm.DB) error {
}

func insertJvn(tx *gorm.DB, cves []models.Jvn, batchSize int) error {
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for _, cve := range cves {
if err := tx.Omit("Cpes").Create(&cve).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
Expand Down Expand Up @@ -727,7 +733,12 @@ func deleteNvd(tx *gorm.DB) error {
}

func insertNvd(tx *gorm.DB, cves []models.Nvd, batchSize int) error {
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for _, cve := range cves {
if err := tx.Omit("Cpes").Create(&cve).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
Expand Down Expand Up @@ -782,7 +793,12 @@ func (r *RDBDriver) InsertFortinet(advs []models.Fortinet) (err error) {
}

logger.Infof("Inserting fetched %d CVEs...", len(advs))
bar := pb.StartNew(len(advs))
bar := pb.StartNew(len(advs)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for _, adv := range advs {
if err := tx.Omit("Cpes").Create(&adv).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
Expand Down Expand Up @@ -930,7 +946,12 @@ func deleteMitre(tx *gorm.DB) error {
}

func insertMitre(tx *gorm.DB, cves []models.Mitre, _ int) error {
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for _, cve := range cves {
if err := tx.Create(&cve).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
Expand Down
29 changes: 25 additions & 4 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -443,7 +444,12 @@ func (r *RedisDriver) InsertJvn(years []string) error {
delete(uniqCves, year)

log.Infof("Inserting fetched CVEs(%s)...", year)
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(cves), batchSize) {
pipe := r.conn.Pipeline()
for _, cve := range cves[idx.From:idx.To] {
Expand Down Expand Up @@ -575,7 +581,12 @@ func (r *RedisDriver) InsertNvd(years []string) error {
}

log.Infof("Inserting fetched CVEs(%s)...", year)
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(cves), batchSize) {
pipe := r.conn.Pipeline()
for _, cve := range cves[idx.From:idx.To] {
Expand Down Expand Up @@ -681,7 +692,12 @@ func (r *RedisDriver) InsertFortinet(advs []models.Fortinet) error {
}

log.Infof("Inserting fetched %d CVEs...", len(advs))
bar := pb.StartNew(len(advs))
bar := pb.StartNew(len(advs)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(advs), batchSize) {
pipe := r.conn.Pipeline()
for _, adv := range advs[idx.From:idx.To] {
Expand Down Expand Up @@ -810,7 +826,12 @@ func (r *RedisDriver) InsertMitre(years []string) error {
}

log.Infof("Inserting fetched CVEs(%s)...", year)
bar := pb.StartNew(len(cves))
bar := pb.StartNew(len(cves)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(cves), batchSize) {
pipe := r.conn.Pipeline()
for _, cve := range cves[idx.From:idx.To] {
Expand Down
Loading