2021-11-07 03:04:08 +03:00
|
|
|
package linkedin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-06-28 16:29:19 +00:00
|
|
|
"github.com/caarlos0/env/v9"
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2021-11-07 03:04:08 +03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2021-11-11 23:53:40 -03:00
|
|
|
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
|
2021-11-07 03:04:08 +03:00
|
|
|
|
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func (Pipe) String() string { return "linkedin" }
|
|
|
|
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.LinkedIn.Enabled }
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
AccessToken string `env:"LINKEDIN_ACCESS_TOKEN,notEmpty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
if ctx.Config.Announce.LinkedIn.MessageTemplate == "" {
|
|
|
|
ctx.Config.Announce.LinkedIn.MessageTemplate = defaultMessageTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Announce(ctx *context.Context) error {
|
|
|
|
message, err := tmpl.New(ctx).Apply(ctx.Config.Announce.LinkedIn.MessageTemplate)
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("linkedin: %w", err)
|
2021-11-07 03:04:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
if err := env.Parse(&cfg); err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("linkedin: %w", err)
|
2021-11-07 03:04:08 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 22:24:15 -03:00
|
|
|
c, err := createLinkedInClient(oauthClientConfig{
|
2021-11-07 03:04:08 +03:00
|
|
|
Context: ctx,
|
|
|
|
AccessToken: cfg.AccessToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("linkedin: %w", err)
|
2021-11-07 03:04:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
url, err := c.Share(message)
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("linkedin: %w", err)
|
2021-11-07 03:04:08 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 22:24:15 -03:00
|
|
|
log.Infof("The text post is available at: %s\n", url)
|
2021-11-07 03:04:08 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|