2021-08-31 15:48:45 +02:00
|
|
|
package reddit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-07 03:50:09 +02:00
|
|
|
"github.com/caarlos0/env/v8"
|
2022-01-10 16:10:35 +02:00
|
|
|
"github.com/caarlos0/go-reddit/v3/reddit"
|
2022-06-22 02:11:15 +02:00
|
|
|
"github.com/caarlos0/log"
|
2021-08-31 15:48:45 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTitleTemplate = `{{ .ProjectName }} {{ .Tag }} is out!`
|
2021-11-06 20:39:43 +02:00
|
|
|
defaultURLTemplate = `{{ .ReleaseURL }}`
|
2021-08-31 15:48:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2021-09-18 15:21:29 +02:00
|
|
|
func (Pipe) String() string { return "reddit" }
|
|
|
|
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Reddit.Enabled }
|
2021-08-31 15:48:45 +02:00
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Secret string `env:"REDDIT_SECRET,notEmpty"`
|
|
|
|
Password string `env:"REDDIT_PASSWORD,notEmpty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
if ctx.Config.Announce.Reddit.TitleTemplate == "" {
|
|
|
|
ctx.Config.Announce.Reddit.TitleTemplate = defaultTitleTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Config.Announce.Reddit.URLTemplate == "" {
|
2021-11-06 21:49:11 +02:00
|
|
|
ctx.Config.Announce.Reddit.URLTemplate = defaultURLTemplate
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Announce(ctx *context.Context) error {
|
|
|
|
title, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.TitleTemplate)
|
|
|
|
if err != nil {
|
2022-12-28 17:24:21 +02:00
|
|
|
return fmt.Errorf("reddit: %w", err)
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
url, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.URLTemplate)
|
|
|
|
if err != nil {
|
2022-12-28 17:24:21 +02:00
|
|
|
return fmt.Errorf("reddit: %w", err)
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
linkRequest := reddit.SubmitLinkRequest{
|
|
|
|
Subreddit: ctx.Config.Announce.Reddit.Sub,
|
|
|
|
Title: title,
|
|
|
|
URL: url,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
if err := env.Parse(&cfg); err != nil {
|
2022-12-28 17:24:21 +02:00
|
|
|
return fmt.Errorf("reddit: %w", err)
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
credentials := reddit.Credentials{ID: ctx.Config.Announce.Reddit.ApplicationID, Secret: cfg.Secret, Username: ctx.Config.Announce.Reddit.Username, Password: cfg.Password}
|
|
|
|
client, err := reddit.NewClient(credentials)
|
|
|
|
if err != nil {
|
2022-12-28 17:24:21 +02:00
|
|
|
return fmt.Errorf("reddit: %w", err)
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
post, _, err := client.Post.SubmitLink(ctx, linkRequest)
|
|
|
|
if err != nil {
|
2022-12-28 17:24:21 +02:00
|
|
|
return fmt.Errorf("reddit: %w", err)
|
2021-08-31 15:48:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:24:21 +02:00
|
|
|
log.Infof("The text post is available at: %s\n", post.URL)
|
2021-08-31 15:48:45 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|