github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/mastodon/mastodon.go (about)

     1  package mastodon
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caarlos0/env/v9"
     7  	"github.com/caarlos0/log"
     8  	"github.com/goreleaser/goreleaser/internal/tmpl"
     9  	"github.com/goreleaser/goreleaser/pkg/context"
    10  	"github.com/mattn/go-mastodon"
    11  )
    12  
    13  const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
    14  
    15  type Pipe struct{}
    16  
    17  func (Pipe) String() string { return "mastodon" }
    18  
    19  func (Pipe) Skip(ctx *context.Context) bool {
    20  	return !ctx.Config.Announce.Mastodon.Enabled || ctx.Config.Announce.Mastodon.Server == ""
    21  }
    22  
    23  type Config struct {
    24  	ClientID     string `env:"MASTODON_CLIENT_ID,notEmpty"`
    25  	ClientSecret string `env:"MASTODON_CLIENT_SECRET,notEmpty"`
    26  	AccessToken  string `env:"MASTODON_ACCESS_TOKEN,notEmpty"`
    27  }
    28  
    29  func (Pipe) Default(ctx *context.Context) error {
    30  	if ctx.Config.Announce.Mastodon.MessageTemplate == "" {
    31  		ctx.Config.Announce.Mastodon.MessageTemplate = defaultMessageTemplate
    32  	}
    33  	return nil
    34  }
    35  
    36  func (Pipe) Announce(ctx *context.Context) error {
    37  	msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Mastodon.MessageTemplate)
    38  	if err != nil {
    39  		return fmt.Errorf("mastodon: %w", err)
    40  	}
    41  
    42  	var cfg Config
    43  	if err := env.Parse(&cfg); err != nil {
    44  		return fmt.Errorf("mastodon: %w", err)
    45  	}
    46  
    47  	client := mastodon.NewClient(&mastodon.Config{
    48  		Server:       ctx.Config.Announce.Mastodon.Server,
    49  		ClientID:     cfg.ClientID,
    50  		ClientSecret: cfg.ClientSecret,
    51  		AccessToken:  cfg.AccessToken,
    52  	})
    53  
    54  	log.Infof("posting: '%s'", msg)
    55  	if _, err := client.PostStatus(ctx, &mastodon.Toot{
    56  		Status: msg,
    57  	}); err != nil {
    58  		return fmt.Errorf("mastodon: %w", err)
    59  	}
    60  	return nil
    61  }