From 514c18ffa297b5965215f1cc90b5bb0fa196bf81 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 17 Dec 2021 04:40:07 +0000 Subject: [PATCH] Updates (#4) Resolves #1 Resolves #3 Reviewed-on: https://git.jojodev.com/jolheiser/lurk/pulls/4 Co-authored-by: jolheiser Co-committed-by: jolheiser --- .drone.yml | 42 -------------- .gitignore | 4 +- .woodpecker.yml | 59 +++++++++++++++++++ Makefile | 15 ----- README.md | 6 +- config/config.go | 27 ++++++++- lurk.sample.toml => config/lurk.toml | 3 + go.mod | 3 +- go.sum | 44 +++++++++++--- handler/reddit.go | 24 ++++++-- handler/twitter.go | 18 +++--- main.go | 87 ++++++++++++++++------------ tools.go | 8 +++ 13 files changed, 216 insertions(+), 124 deletions(-) delete mode 100644 .drone.yml create mode 100644 .woodpecker.yml delete mode 100644 Makefile rename lurk.sample.toml => config/lurk.toml (94%) create mode 100644 tools.go diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index c2ba578..0000000 --- a/.drone.yml +++ /dev/null @@ -1,42 +0,0 @@ ---- -kind: pipeline -name: compliance -trigger: - event: - - pull_request -steps: - - name: build - pull: always - image: golang:1.16 - commands: - - make test - - make build - - name: check - pull: always - image: golang:1.16 - commands: - - make vet - ---- -kind: pipeline -name: release -trigger: - event: - - push - branch: - - master -steps: - - name: build - pull: always - image: golang:1.16 - commands: - - make build - - name: gitea-release - pull: always - image: jolheiser/drone-gitea-main:latest - settings: - token: - from_secret: gitea_token - base: https://gitea.com - files: - - "lurk" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 515fddb..192f1c1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,4 @@ .idea/ # Binaries -/lurk* - -!lurk.sample.toml \ No newline at end of file +/lurk* \ No newline at end of file diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..7414948 --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,59 @@ +clone: + git: + image: woodpeckerci/plugin-git:next + +pipeline: + compliance: + image: golang:1.17 + commands: + - go test -race ./... + - go vet ./... + - go run github.com/rs/zerolog/cmd/lint go.jolheiser.com/lurk + - go build + when: + event: pull_request + + build: + image: golang:1.17 + commands: + - GOOS="windows" go build + - GOOS="linux" go build + when: + event: [ push, tag ] + branch: main + + release-main: + image: jolheiser/drone-gitea-main:latest + secrets: + - source: gitea_token + target: plugin_token + base: https://git.jojodev.com + files: + - "lurk" + - "lurk.exe" + when: + event: push + branch: main + + release-tag: + image: plugins/gitea-release:1 + secrets: + - source: gitea_token + target: plugin_api_key + base_url: https://git.jojodev.com + files: + - "lurk" + - "lurk.exe" + when: + event: tag + tag: v* + + prune: + image: jolheiser/drone-gitea-prune + secrets: + - source: gitea_token + target: plugin_token + base: https://git.jojodev.com + when: + event: tag + tag: v* diff --git a/Makefile b/Makefile deleted file mode 100644 index 0b355d9..0000000 --- a/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -.PHONY: build -build: - go build - -.PHONY: fmt -fmt: - go fmt ./... - -.PHONY: test -test: - go test -race ./... - -.PHONY: vet -vet: - go vet ./... \ No newline at end of file diff --git a/README.md b/README.md index 23446c3..99132ac 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,8 @@ A lurker used to notify a Discord channel via webhook whenever a matching submission is made. -See the [example config](lurk.sample.toml). \ No newline at end of file +See the [example config](config/lurk.toml). + +## License + +[MIT](LICENSE) \ No newline at end of file diff --git a/config/config.go b/config/config.go index 3187291..d875af9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,14 +1,37 @@ package config import ( + _ "embed" + "fmt" + "os" "regexp" "strings" + "time" "github.com/pelletier/go-toml" - "go.jolheiser.com/beaver" + "github.com/rs/zerolog/log" ) +//go:embed lurk.toml +var defaultConfig []byte + +func Init(configPath string) error { + if _, err := os.Lstat(configPath); err == nil { + return fmt.Errorf("config exists at %q", configPath) + } + + fi, err := os.Create(configPath) + if err != nil { + return err + } + defer fi.Close() + + _, err = fi.Write(defaultConfig) + return err +} + type Config struct { + Backoff time.Duration `toml:"backoff"` Reddit RedditConfig `toml:"reddit"` Twitter TwitterConfig `toml:"twitter"` } @@ -66,6 +89,6 @@ func Load(configPath string) (*Config, error) { } cfg.loadReddit() - beaver.Debug(cfg) + log.Debug().Msgf("%#v", cfg) return &cfg, nil } diff --git a/lurk.sample.toml b/config/lurk.toml similarity index 94% rename from lurk.sample.toml rename to config/lurk.toml index 55f86d1..0494fe0 100644 --- a/lurk.sample.toml +++ b/config/lurk.toml @@ -1,3 +1,6 @@ +# Backoff before lurkers restart after dying +backoff = "5m" + # Reddit information [reddit] # Reddit Agent Info diff --git a/go.mod b/go.mod index b4fb766..6876545 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,8 @@ require ( github.com/dghubble/go-twitter v0.0.0-20200725221434-4bc8ad7ad1b4 github.com/dghubble/oauth1 v0.6.0 github.com/pelletier/go-toml v1.8.1 + github.com/peterbourgon/ff/v3 v3.1.2 // indirect + github.com/rs/zerolog v1.26.0 github.com/turnage/graw v0.0.0-20200404033202-65715eea1cd0 - go.jolheiser.com/beaver v1.0.2 go.jolheiser.com/disco v0.0.3 ) diff --git a/go.sum b/go.sum index f5ff7b0..09db1ae 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY= github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 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= @@ -10,6 +12,7 @@ github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0 github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk= github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU= github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -19,10 +22,17 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= +github.com/peterbourgon/ff/v3 v3.1.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM= +github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE= +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/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE= +github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -30,23 +40,43 @@ github.com/turnage/graw v0.0.0-20200404033202-65715eea1cd0 h1:ss0FREpyYvw5V9t5XW github.com/turnage/graw v0.0.0-20200404033202-65715eea1cd0/go.mod h1:aAkq4I/q1izZSSwHvzhDn9NA+eGxgTSuibwP3MZRlQY= github.com/turnage/redditproto v0.0.0-20151223012412-afedf1b6eddb h1:qR56NGRvs2hTUbkn6QF8bEJzxPIoMw3Np3UigBeJO5A= github.com/turnage/redditproto v0.0.0-20151223012412-afedf1b6eddb/go.mod h1:GyqJdEoZSNoxKDb7Z2Lu/bX63jtFukwpaTP9ZIS5Ei0= -go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk= -go.jolheiser.com/beaver v1.0.2/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= -go.jolheiser.com/disco v0.0.2 h1:UGYNqO7NQSBGB/OoS9WE5o/jYvmx1G0Bq3qQRM42Bkw= -go.jolheiser.com/disco v0.0.2/go.mod h1:tY3HkJmMrzXH/bPgDWKHn1DUzDxkemD80OHLgHSA5uQ= +github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.jolheiser.com/disco v0.0.3 h1:Itt+RazbK2OpREDArE5IN2TA+h4wgTh9HVtKa4N/U+Q= go.jolheiser.com/disco v0.0.3/go.mod h1:J1U0AyytqMRkcuoQCbfWTH7tM7Dfnmtn453lVERJTXI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 h1:pE8b58s1HRDMi8RDc79m0HISf9D4TzseP40cEA6IGfs= golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/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-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ= +golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/handler/reddit.go b/handler/reddit.go index ce5a056..2371905 100644 --- a/handler/reddit.go +++ b/handler/reddit.go @@ -3,6 +3,8 @@ package handler import ( "context" "fmt" + "github.com/rs/zerolog/log" + "github.com/turnage/graw" "net/http" "regexp" "strings" @@ -11,21 +13,31 @@ import ( "go.jolheiser.com/lurk/config" "github.com/turnage/graw/reddit" - "go.jolheiser.com/beaver" "go.jolheiser.com/disco" ) var httpClient = &http.Client{Timeout: time.Minute} type Reddit struct { + Bot reddit.Bot Config *config.Config } +func (r *Reddit) Run() error { + _, wait, err := graw.Run(r, r.Bot, graw.Config{ + Subreddits: r.Config.Reddit.SubRedditNames(), + }) + if err != nil { + return err + } + return wait() +} + func (r *Reddit) Post(p *reddit.Post) error { sub := r.Config.Reddit.Map[strings.ToLower(p.Subreddit)] if err := checkPost(r.Config, p); err != nil { - beaver.Debugf("%s: %v", p.Subreddit, err) + log.Debug().Err(err).Msg(p.Subreddit) return nil } @@ -57,24 +69,24 @@ func (r *Reddit) Post(p *reddit.Post) error { } if sub.Webhook == "" { - beaver.Errorf("no webhook for %s", p.Subreddit) + log.Error().Msgf("no webhook for %s", p.Subreddit) return nil } req, err := e.Request(context.Background(), sub.Webhook) if err != nil { - beaver.Error(err) + log.Err(err).Msg("") return nil } resp, err := httpClient.Do(req) if err != nil { - beaver.Error(err) + log.Err(err).Msg("") return nil } if resp.StatusCode != http.StatusNoContent { - beaver.Error(resp.Status) + log.Error().Msgf(resp.Status) return nil } diff --git a/handler/twitter.go b/handler/twitter.go index 6270d91..6c320c6 100644 --- a/handler/twitter.go +++ b/handler/twitter.go @@ -3,11 +3,11 @@ package handler import ( "context" "fmt" + "github.com/rs/zerolog/log" "go.jolheiser.com/lurk/config" "github.com/dghubble/go-twitter/twitter" - "go.jolheiser.com/beaver" "go.jolheiser.com/disco" ) @@ -17,25 +17,25 @@ type Twitter struct { } func (t *Twitter) Run() { - beaver.Debugf("setting up stream for %v", t.Filter) + log.Debug().Msgf("setting up stream for %v", t.Filter) demux := twitter.NewSwitchDemux() demux.Tweet = t.Tweet - beaver.Debugf("streaming %v", t.Filter) + log.Debug().Msgf("streaming %v", t.Filter) demux.HandleChan(t.Stream.Messages) - beaver.Debugf("disconnected from stream: %v", t.Filter) + log.Debug().Msgf("disconnected from stream: %v", t.Filter) } func (t *Twitter) Tweet(tweet *twitter.Tweet) { - beaver.Debugf("new tweet for %v", t.Filter) + log.Debug().Msgf("new tweet for %v", t.Filter) if t.Filter.FollowStrict { if tweet.InReplyToStatusIDStr != "" { - beaver.Debug("tweet is a reply") + log.Debug().Msg("tweet is a reply") return } if tweet.RetweetedStatus != nil { - beaver.Debug("tweet is a retweet") + log.Debug().Msg("tweet is a retweet") return } var match bool @@ -46,7 +46,7 @@ func (t *Twitter) Tweet(tweet *twitter.Tweet) { } } if !match { - beaver.Debug("tweet did not match any follow IDs") + log.Debug().Msg("tweet did not match any follow IDs") return } } @@ -57,6 +57,6 @@ func (t *Twitter) Tweet(tweet *twitter.Tweet) { Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID), } if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil { - beaver.Error(err) + log.Err(err).Msg("") } } diff --git a/main.go b/main.go index 9255b47..482427f 100644 --- a/main.go +++ b/main.go @@ -2,44 +2,50 @@ package main import ( "flag" + "github.com/peterbourgon/ff/v3" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "os" "os/signal" + "strings" "syscall" + "time" "go.jolheiser.com/lurk/config" "go.jolheiser.com/lurk/handler" "github.com/dghubble/go-twitter/twitter" "github.com/dghubble/oauth1" - "github.com/turnage/graw" "github.com/turnage/graw/reddit" - "go.jolheiser.com/beaver" -) - -var ( - configPath string - debug bool ) func main() { - flag.StringVar(&configPath, "config", "lurk.toml", "Path to lurk's config file") - flag.BoolVar(&debug, "debug", false, "Turn on debug mode") - flag.Parse() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + zerolog.SetGlobalLevel(zerolog.InfoLevel) - beaver.Console.Format = beaver.FormatOptions{ - TimePrefix: true, - StackPrefix: true, - StackLimit: 15, - LevelPrefix: true, - LevelColor: true, - } - if debug { - beaver.Console.Level = beaver.DEBUG + fs := flag.NewFlagSet("lurk", flag.ExitOnError) + configFlag := fs.String("config", "lurk.toml", "path to config file") + debugFlag := fs.Bool("debug", false, "turn on debug mode") + if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("LURK")); err != nil { + log.Fatal().Err(err).Msg("") } - cfg, err := config.Load(configPath) + if *debugFlag { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + if len(os.Args) > 1 && strings.EqualFold(os.Args[1], "init") { + if err := config.Init(*configFlag); err != nil { + log.Err(err).Msg("") + return + } + log.Info().Msgf("created %q", *configFlag) + return + } + + cfg, err := config.Load(*configFlag) if err != nil { - beaver.Fatal(err) + log.Fatal().Err(err).Msg("") } // Reddit @@ -48,8 +54,8 @@ func main() { // Twitter go lurkTwitter(cfg) - beaver.Info("Lurk is ready to start lurking!") - ch := make(chan os.Signal) + log.Info().Msg("Lurk is ready to start lurking!") + ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) <-ch } @@ -65,20 +71,20 @@ func lurkReddit(cfg *config.Config) { }, }) if err != nil { - beaver.Fatal(err) - } - - _, wait, err := graw.Run(&handler.Reddit{ - Config: cfg, - }, bot, graw.Config{ - Subreddits: cfg.Reddit.SubRedditNames(), - }) - if err != nil { - beaver.Errorf("could not run reddit bot: %v", err) + log.Fatal().Err(err).Msg("") return } - if err := wait(); err != nil { - beaver.Fatal(err) + + lurker := &handler.Reddit{ + Bot: bot, + Config: cfg, + } + + for { + if err := lurker.Run(); err != nil { + log.Err(err).Msg("") + } + <-time.After(cfg.Backoff) } } @@ -94,7 +100,7 @@ func lurkTwitter(cfg *config.Config) { Count: 1, }) if err != nil { - beaver.Fatal(err) + log.Fatal().Err(err).Msg("") } for _, filter := range cfg.Twitter.Filters { @@ -105,7 +111,7 @@ func lurkTwitter(cfg *config.Config) { Track: filter.Tracks, }) if err != nil { - beaver.Fatal(err) + log.Fatal().Err(err).Msg("") } lurker := &handler.Twitter{ @@ -113,6 +119,11 @@ func lurkTwitter(cfg *config.Config) { Stream: stream, } - go lurker.Run() + go func() { + for { + lurker.Run() + <-time.After(cfg.Backoff) + } + }() } } diff --git a/tools.go b/tools.go new file mode 100644 index 0000000..048b2fa --- /dev/null +++ b/tools.go @@ -0,0 +1,8 @@ +//go:build tools +// +build tools + +package main + +import ( + _ "github.com/rs/zerolog/cmd/lint" +)