mi-v1/cmd/mi/posse.go

52 lines
819 B
Go
Raw Normal View History

2020-01-11 18:38:50 +00:00
package main
2020-01-11 20:18:59 +00:00
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"within.website/ln"
)
type Pusher func(context.Context, Post) error
2020-01-11 18:38:50 +00:00
type Post struct {
Title string
URL string
Tags []string
}
2020-01-11 20:18:59 +00:00
func (p Post) Format() string {
var sb strings.Builder
2020-01-11 22:43:01 +00:00
fmt.Fprintf(&sb, "New post: %s\n\n%s\n\n", p.Title, p.URL)
2020-01-11 20:18:59 +00:00
for _, tg := range p.Tags {
2020-01-11 22:24:59 +00:00
tg = strings.ReplaceAll(tg, "-", "")
fmt.Fprintf(&sb, "#%s ", tg)
2020-01-11 20:18:59 +00:00
}
return sb.String()
}
func (p Post) F() ln.F {
return ln.F{
"post_title": p.Title,
"post_url": p.URL,
"post_tags": p.Tags,
}
}
func (mi *Mi) Push(ctx context.Context, p Post) error {
ln.Log(ctx, p)
var result error
for _, per := range mi.pushers {
if err := per(ctx, p); err != nil {
result = multierror.Append(result, err)
}
}
return result
}