diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..17297b3 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,25 @@ +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + ldflags: + - "-s -w -X main.Version={{.Version}}" +archives: + - replacements: + 386: i386 + amd64: x86_64 + format_overrides: + - goos: windows + format: zip +checksum: + name_template: 'checksums.txt' +release: + gitea: + owner: jolheiser + name: lurk +gitea_urls: + api: https://git.jojodev.com/api/v1/ + download: https://git.jojodev.com diff --git a/.woodpecker.yml b/.woodpecker.yml deleted file mode 100644 index 7414948..0000000 --- a/.woodpecker.yml +++ /dev/null @@ -1,59 +0,0 @@ -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/.woodpecker/goreleaser.yml b/.woodpecker/goreleaser.yml new file mode 100644 index 0000000..29a760b --- /dev/null +++ b/.woodpecker/goreleaser.yml @@ -0,0 +1,39 @@ +clone: + git: + image: woodpeckerci/plugin-git + settings: + tags: true + +pipeline: + compliance: + image: golang:1.18 + commands: + - go test -race ./... + - go vet ./... + - go run github.com/rs/zerolog/cmd/lint@latest go.jolheiser.com/lurk + when: + event: pull_request + + build: + image: goreleaser/goreleaser + commands: + - goreleaser build --snapshot + when: + event: pull_request + + release: + image: goreleaser/goreleaser + commands: + - goreleaser release + secrets: [ gitea_token ] + when: + event: tag + + prune: + image: jolheiser/drone-gitea-prune + settings: + base: https://git.jojodev.com + token: + from_secret: gitea_token + when: + event: tag diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..a8add13 --- /dev/null +++ b/config.yaml @@ -0,0 +1,55 @@ +# Backoff before lurkers restart after dying +backoff: "5m" + +# Reddit information +reddit: + enabled: false + + # Reddit Agent Info + app_name: "Lurk" + version: "0.1" + + # https://www.reddit.com/prefs/apps + client_id: "" + client_secret: "" + + # Reddit credentials + username: "" + password: "" + + # A list of subreddites to monitor + # allow/blocklist can be omitted in real config if empty + subs: + - name: "" + icon_url: "" + flair_allowlist: [] + flair_blocklist: [] + title_allowlist: [] + title_blocklist: [] + title_limit: 0 + body_allowlist: [] + body_blocklist: [] + body_limit: 0 + webhook: "" + +# Twitter information +twitter: + enabled: false + + # Auth + consumer_key: "" + consumer_secret: "" + access_token: "" + access_secret: "" + + # A list of filters to watch for + # Empty fields can be omitted in a real config + filters: + # follows must use a Twitter user's ID + # https://tweeterid.com/ + - follows: [] + # strict mode means only original tweets will be ingested + follow_strict: false + locations: [] + tracks: [] + webhook: "" diff --git a/config/config.go b/config/config.go index d875af9..bd87fcb 100644 --- a/config/config.go +++ b/config/config.go @@ -8,11 +8,11 @@ import ( "strings" "time" - "github.com/pelletier/go-toml" "github.com/rs/zerolog/log" + "gopkg.in/yaml.v3" ) -//go:embed lurk.toml +//go:embed config.yaml var defaultConfig []byte func Init(configPath string) error { @@ -31,12 +31,12 @@ func Init(configPath string) error { } type Config struct { - Backoff time.Duration `toml:"backoff"` - Reddit RedditConfig `toml:"reddit"` - Twitter TwitterConfig `toml:"twitter"` + Backoff time.Duration `yaml:"backoff"` + Reddit RedditConfig `yaml:"reddit"` + Twitter TwitterConfig `yaml:"twitter"` } -func (c *Config) loadReddit() { +func (c *Config) initReddit() { for _, sub := range c.Reddit.SubReddits { c.Reddit.Map[strings.ToLower(sub.Name)] = sub if sub.TitleLimit == 0 || sub.TitleLimit > 253 { @@ -79,16 +79,16 @@ func Load(configPath string) (*Config, error) { }, } - tree, err := toml.LoadFile(configPath) + fi, err := os.Open(configPath) if err != nil { - return nil, err + return nil, fmt.Errorf("could not open config: %v", err) } - if err := tree.Unmarshal(&cfg); err != nil { - return nil, err + if err := yaml.NewDecoder(fi).Decode(&cfg); err != nil { + return nil, fmt.Errorf("could not decode config: %v", err) } - cfg.loadReddit() + cfg.initReddit() log.Debug().Msgf("%#v", cfg) return &cfg, nil } diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..a8add13 --- /dev/null +++ b/config/config.yaml @@ -0,0 +1,55 @@ +# Backoff before lurkers restart after dying +backoff: "5m" + +# Reddit information +reddit: + enabled: false + + # Reddit Agent Info + app_name: "Lurk" + version: "0.1" + + # https://www.reddit.com/prefs/apps + client_id: "" + client_secret: "" + + # Reddit credentials + username: "" + password: "" + + # A list of subreddites to monitor + # allow/blocklist can be omitted in real config if empty + subs: + - name: "" + icon_url: "" + flair_allowlist: [] + flair_blocklist: [] + title_allowlist: [] + title_blocklist: [] + title_limit: 0 + body_allowlist: [] + body_blocklist: [] + body_limit: 0 + webhook: "" + +# Twitter information +twitter: + enabled: false + + # Auth + consumer_key: "" + consumer_secret: "" + access_token: "" + access_secret: "" + + # A list of filters to watch for + # Empty fields can be omitted in a real config + filters: + # follows must use a Twitter user's ID + # https://tweeterid.com/ + - follows: [] + # strict mode means only original tweets will be ingested + follow_strict: false + locations: [] + tracks: [] + webhook: "" diff --git a/config/lurk.toml b/config/lurk.toml deleted file mode 100644 index 0494fe0..0000000 --- a/config/lurk.toml +++ /dev/null @@ -1,51 +0,0 @@ -# Backoff before lurkers restart after dying -backoff = "5m" - -# Reddit information -[reddit] - # Reddit Agent Info - app_name = "Lurk" - version = "0.1" - - # https://www.reddit.com/prefs/apps - client_id = "" - client_secret = "" - - # Reddit credentials - username = "" - password = "" - - # A list of subreddites to monitor - # allow/blocklist can be omitted in real config if empty - [[reddit.sub]] - name = "" - icon_url = "" - flair_allowlist = [] - flair_blocklist = [] - title_allowlist = [] - title_blocklist = [] - title_limit = 0 - body_allowlist = [] - body_blocklist = [] - body_limit = 0 - webhook = "" - -# Twitter information -[twitter] - # Auth - consumer_key = "" - consumer_secret = "" - access_token = "" - access_secret = "" - - # A list of filters to watch for - # Empty fields can be omitted in a real config - [[twitter.filter]] - # follows must use a Twitter user's ID - # https://tweeterid.com/ - follows = [] - # strict mode means only original tweets will be ingested - follow_strict = false - locations = [] - tracks = [] - webhook = "" diff --git a/config/reddit.go b/config/reddit.go index e335eb1..c0296d0 100644 --- a/config/reddit.go +++ b/config/reddit.go @@ -6,38 +6,39 @@ import ( ) type RedditConfig struct { - SubReddits []*SubReddit `toml:"sub"` - Map map[string]*SubReddit `toml:"-"` + Enabled bool `yaml:"enabled"` + SubReddits []*SubReddit `yaml:"subs"` + Map map[string]*SubReddit `yaml:"-"` // Agent file - AppName string `toml:"app_name"` - Version string `toml:"version"` + AppName string `yaml:"app_name"` + Version string `yaml:"version"` - ClientID string `toml:"client_id"` - ClientSecret string `toml:"client_secret"` + ClientID string `yaml:"client_id"` + ClientSecret string `yaml:"client_secret"` - Username string `toml:"username"` - Password string `toml:"password"` + Username string `yaml:"username"` + Password string `yaml:"password"` } type SubReddit struct { - Name string `toml:"name"` - IconURL string `toml:"icon_url"` - FlairAllowlist []string `toml:"flair_allowlist"` - FlairAllowlistRe []*regexp.Regexp `toml:"-"` - FlairBlocklist []string `toml:"flair_blocklist"` - FlairBlocklistRe []*regexp.Regexp `toml:"-"` - TitleAllowlist []string `toml:"title_allowlist"` - TitleAllowlistRe []*regexp.Regexp `toml:"-"` - TitleBlocklist []string `toml:"title_blocklist"` - TitleBlocklistRe []*regexp.Regexp `toml:"-"` - TitleLimit int `toml:"title_limit"` - BodyAllowlist []string `toml:"body_allowlist"` - BodyAllowlistRe []*regexp.Regexp `toml:"-"` - BodyBlocklist []string `toml:"body_blocklist"` - BodyBlocklistRe []*regexp.Regexp `toml:"-"` - BodyLimit int `toml:"body_limit"` - Webhook string `toml:"webhook"` + Name string `yaml:"name"` + IconURL string `yaml:"icon_url"` + FlairAllowlist []string `yaml:"flair_allowlist"` + FlairAllowlistRe []*regexp.Regexp `yaml:"-"` + FlairBlocklist []string `yaml:"flair_blocklist"` + FlairBlocklistRe []*regexp.Regexp `yaml:"-"` + TitleAllowlist []string `yaml:"title_allowlist"` + TitleAllowlistRe []*regexp.Regexp `yaml:"-"` + TitleBlocklist []string `yaml:"title_blocklist"` + TitleBlocklistRe []*regexp.Regexp `yaml:"-"` + TitleLimit int `yaml:"title_limit"` + BodyAllowlist []string `yaml:"body_allowlist"` + BodyAllowlistRe []*regexp.Regexp `yaml:"-"` + BodyBlocklist []string `yaml:"body_blocklist"` + BodyBlocklistRe []*regexp.Regexp `yaml:"-"` + BodyLimit int `yaml:"body_limit"` + Webhook string `yaml:"webhook"` } func (r *RedditConfig) UserAgent() string { diff --git a/config/twitter.go b/config/twitter.go index 782cc72..cd42497 100644 --- a/config/twitter.go +++ b/config/twitter.go @@ -1,18 +1,19 @@ package config type TwitterConfig struct { - ConsumerKey string `toml:"consumer_key"` - ConsumerSecret string `toml:"consumer_secret"` - AccessToken string `toml:"access_token"` - AccessSecret string `toml:"access_secret"` + Enabled bool `yaml:"enabled"` + ConsumerKey string `yaml:"consumer_key"` + ConsumerSecret string `yaml:"consumer_secret"` + AccessToken string `yaml:"access_token"` + AccessSecret string `yaml:"access_secret"` - Filters []Filter `toml:"filter"` + Filters []Filter `yaml:"filters"` } type Filter struct { - Follows []string `toml:"follows"` - FollowStrict bool `toml:"follow_strict"` - Locations []string `toml:"locations"` - Tracks []string `toml:"tracks"` - Webhook string `toml:"webhook"` + Follows []string `yaml:"follows"` + FollowStrict bool `yaml:"follow_strict"` + Locations []string `yaml:"locations"` + Tracks []string `yaml:"tracks"` + Webhook string `yaml:"webhook"` } diff --git a/go.mod b/go.mod index 6876545..58df694 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,9 @@ go 1.16 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/peterbourgon/ff/v3 v3.1.2 github.com/rs/zerolog v1.26.0 github.com/turnage/graw v0.0.0-20200404033202-65715eea1cd0 go.jolheiser.com/disco v0.0.3 + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 09db1ae..c0aacdc 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,6 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+ 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= @@ -78,5 +76,8 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler/reddit.go b/handler/reddit.go index 2371905..1e45c92 100644 --- a/handler/reddit.go +++ b/handler/reddit.go @@ -3,13 +3,14 @@ package handler import ( "context" "fmt" - "github.com/rs/zerolog/log" - "github.com/turnage/graw" "net/http" "regexp" "strings" "time" + "github.com/rs/zerolog/log" + "github.com/turnage/graw" + "go.jolheiser.com/lurk/config" "github.com/turnage/graw/reddit" diff --git a/handler/twitter.go b/handler/twitter.go index 6c320c6..abf5645 100644 --- a/handler/twitter.go +++ b/handler/twitter.go @@ -3,6 +3,7 @@ package handler import ( "context" "fmt" + "github.com/rs/zerolog/log" "go.jolheiser.com/lurk/config" diff --git a/main.go b/main.go index 482427f..fb3e8ee 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,16 @@ package main import ( "flag" - "github.com/peterbourgon/ff/v3" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "os" "os/signal" "strings" "syscall" "time" + "github.com/peterbourgon/ff/v3" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "go.jolheiser.com/lurk/config" "go.jolheiser.com/lurk/handler" @@ -24,7 +25,7 @@ func main() { zerolog.SetGlobalLevel(zerolog.InfoLevel) fs := flag.NewFlagSet("lurk", flag.ExitOnError) - configFlag := fs.String("config", "lurk.toml", "path to config file") + configFlag := fs.String("config", "config.yaml", "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("") @@ -61,6 +62,10 @@ func main() { } func lurkReddit(cfg *config.Config) { + if !cfg.Reddit.Enabled { + log.Info().Msg("reddit is disabled") + return + } bot, err := reddit.NewBot(reddit.BotConfig{ Agent: cfg.Reddit.UserAgent(), App: reddit.App{ @@ -89,6 +94,10 @@ func lurkReddit(cfg *config.Config) { } func lurkTwitter(cfg *config.Config) { + if !cfg.Twitter.Enabled { + log.Info().Msg("twitter is disabled") + return + } twitterConfig := oauth1.NewConfig(cfg.Twitter.ConsumerKey, cfg.Twitter.ConsumerSecret) token := oauth1.NewToken(cfg.Twitter.AccessToken, cfg.Twitter.AccessSecret)