From 27bea0c91b9c97c468f6ee0d1ee08bdc34b2e3c6 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Sun, 12 Jun 2022 18:46:16 -0400 Subject: [PATCH] add go logr and move scheduler out --- app/app.go | 5 +++-- app/schedule_test.go | 28 ---------------------------- go.mod | 2 ++ go.sum | 13 ++++++++++++- log/log.go | 5 +++++ main.go | 4 ++++ {app => schedule}/schedule.go | 10 ++++++++-- 7 files changed, 34 insertions(+), 33 deletions(-) delete mode 100644 app/schedule_test.go rename {app => schedule}/schedule.go (93%) diff --git a/app/app.go b/app/app.go index 8abc56a..9385087 100644 --- a/app/app.go +++ b/app/app.go @@ -9,6 +9,7 @@ import ( "go.balki.me/tss/log" "go.balki.me/tss/parser" "go.balki.me/tss/proxy" + "go.balki.me/tss/schedule" "go.balki.me/tss/telegram" ) @@ -19,7 +20,7 @@ func Run(configPath string) { log.Panic("failed to parse config", "path", configPath, "err", err) } - scheduler, err := NewScheduler(cfg.LastSuccessPath) + scheduler, err := schedule.NewScheduler(cfg.LastSuccessPath) if err != nil { log.Panic("failed to create scheduler", "path", cfg.LastSuccessPath, "error", err) } @@ -50,7 +51,7 @@ func Run(configPath string) { wg.Wait() } -func ProcessFeed(feed *FeedCfg, scheduler Scheduler, dbDir string, tgram telegram.TelegramSender) { +func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgram telegram.TelegramSender) { sd, err := scheduler.ShouldDownload(feed.Name, feed.Cron) if err != nil { log.Error("shouldDownload failed", "feed", feed.Name, "err", err) diff --git a/app/schedule_test.go b/app/schedule_test.go deleted file mode 100644 index a966046..0000000 --- a/app/schedule_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package app - -import ( - "fmt" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v3" -) - -func TestYamlMap(t *testing.T) { - yesterday := time.Now().Add(-24 * time.Hour) - m := map[string]time.Time{ - "Bala": time.Now(), - "Linus": yesterday, - } - out, err := yaml.Marshal(&m) - if err != nil { - t.Fatalf("got err: %v", err) - } - // assert.Equal(t, nil, fmt.Sprintf("%s", out)) - var m2 map[string]time.Time - err = yaml.Unmarshal(out, &m2) - assert.NoError(t, err) - - assert.Equal(t, "foobar", fmt.Sprint(m2["Bala"])) -} diff --git a/go.mod b/go.mod index 675ddde..b3ccb33 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module go.balki.me/tss go 1.18 require ( + github.com/go-logr/logr v1.2.3 + github.com/go-logr/zapr v1.2.3 github.com/robfig/cron/v3 v3.0.1 github.com/stretchr/testify v1.7.2 go.uber.org/zap v1.21.0 diff --git a/go.sum b/go.sum index 5442cd1..1fa2cfe 100644 --- a/go.sum +++ b/go.sum @@ -3,29 +3,38 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A= +github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -50,6 +59,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -58,6 +68,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/log.go b/log/log.go index 7df8b48..3fc57a1 100644 --- a/log/log.go +++ b/log/log.go @@ -1,10 +1,13 @@ package log import ( + "github.com/go-logr/logr" + "github.com/go-logr/zapr" "go.uber.org/zap" ) var Logger *zap.SugaredLogger +var GLogr logr.Logger var Debug, Info, Warn, Error, Panic func(msg string, keysAndValues ...interface{}) @@ -33,4 +36,6 @@ func init() { Warn = Logger.Warnw Error = Logger.Errorw Panic = Logger.Panicw + + GLogr = zapr.NewLogger(logger) } diff --git a/main.go b/main.go index f474dc6..99163b2 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,13 @@ import ( "flag" "go.balki.me/tss/app" + "go.balki.me/tss/log" + "go.balki.me/tss/schedule" ) func main() { + schedule.SetLogger(log.GLogr) + var configPath string flag.StringVar(&configPath, "config", "./tss.yml", "path to tss.yml config") flag.Parse() diff --git a/app/schedule.go b/schedule/schedule.go similarity index 93% rename from app/schedule.go rename to schedule/schedule.go index b9893aa..baa58e4 100644 --- a/app/schedule.go +++ b/schedule/schedule.go @@ -1,4 +1,4 @@ -package app +package schedule import ( "errors" @@ -7,11 +7,17 @@ import ( "sync" "time" + "github.com/go-logr/logr" "github.com/robfig/cron/v3" - "go.balki.me/tss/log" "gopkg.in/yaml.v3" ) +var log = logr.Discard() + +func SetLogger(log logr.Logger) { + log = log +} + type Scheduler interface { ShouldDownload(name string, scheduleSpec string) (bool, error) Save() error