From 0c8a289e950ce8e949988777ab0dc8b74614f9b4 Mon Sep 17 00:00:00 2001 From: sadayuki-matsuno Date: Thu, 4 Jul 2024 16:27:02 +0900 Subject: [PATCH] feat(db) no progress when --log-json option (#147) --- db/rdb.go | 8 +++++++- db/redis.go | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/db/rdb.go b/db/rdb.go index 6ae82f2..c589564 100644 --- a/db/rdb.go +++ b/db/rdb.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "log" "os" "time" @@ -183,7 +184,12 @@ func (r *RDBDriver) InsertMetasploit(records []models.Metasploit) (err error) { } func (r *RDBDriver) deleteAndInsertMetasploit(records []models.Metasploit) (err error) { - bar := pb.StartNew(len(records)) + bar := pb.StartNew(len(records)).SetWriter(func() io.Writer { + if viper.GetBool("log-json") { + return io.Discard + } + return os.Stderr + }()) tx := r.conn.Begin() defer func() { if err != nil { diff --git a/db/redis.go b/db/redis.go index 9616537..6b4b82c 100644 --- a/db/redis.go +++ b/db/redis.go @@ -6,6 +6,8 @@ import ( "encoding/json" "errors" "fmt" + "io" + "os" "strconv" "strings" "time" @@ -202,7 +204,12 @@ func (r *RedisDriver) InsertMetasploit(records []models.Metasploit) (err error) } log15.Info("Inserting Modules having CVEs...") - bar := pb.StartNew(len(records)) + bar := pb.StartNew(len(records)).SetWriter(func() io.Writer { + if viper.GetBool("log-json") { + return io.Discard + } + return os.Stderr + }()) for idx := range chunkSlice(len(records), batchSize) { pipe := r.conn.Pipeline() for _, record := range records[idx.From:idx.To] {