1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/internal/pipe/reddit/reddit.go
Carlos A Becker 137ed3be74
feat(deps): update caarlos0/env to v8
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2023-04-07 01:50:09 +00:00

77 lines
1.9 KiB
Go

package reddit
import (
"fmt"
"github.com/caarlos0/env/v8"
"github.com/caarlos0/go-reddit/v3/reddit"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
const (
defaultTitleTemplate = `{{ .ProjectName }} {{ .Tag }} is out!`
defaultURLTemplate = `{{ .ReleaseURL }}`
)
type Pipe struct{}
func (Pipe) String() string { return "reddit" }
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Reddit.Enabled }
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 == "" {
ctx.Config.Announce.Reddit.URLTemplate = defaultURLTemplate
}
return nil
}
func (Pipe) Announce(ctx *context.Context) error {
title, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.TitleTemplate)
if err != nil {
return fmt.Errorf("reddit: %w", err)
}
url, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.URLTemplate)
if err != nil {
return fmt.Errorf("reddit: %w", err)
}
linkRequest := reddit.SubmitLinkRequest{
Subreddit: ctx.Config.Announce.Reddit.Sub,
Title: title,
URL: url,
}
var cfg Config
if err := env.Parse(&cfg); err != nil {
return fmt.Errorf("reddit: %w", err)
}
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 {
return fmt.Errorf("reddit: %w", err)
}
post, _, err := client.Post.SubmitLink(ctx, linkRequest)
if err != nil {
return fmt.Errorf("reddit: %w", err)
}
log.Infof("The text post is available at: %s\n", post.URL)
return nil
}