You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
2.5 KiB
119 lines
2.5 KiB
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"io"
|
||
|
|
||
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
||
2 years ago
|
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
||
3 years ago
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
|
||
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
||
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
||
|
"github.com/tilinna/clock"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
2 years ago
|
|
||
3 years ago
|
ctx := context.Background()
|
||
2 years ago
|
|
||
2 years ago
|
cfg := cfgpkg.NewBlogCfg(cfg.Params{})
|
||
3 years ago
|
|
||
2 years ago
|
var dataDir cfgpkg.DataDir
|
||
|
dataDir.SetupCfg(cfg)
|
||
|
defer dataDir.Close()
|
||
|
ctx = mctx.WithAnnotator(ctx, &dataDir)
|
||
3 years ago
|
|
||
|
var mailerParams mailinglist.MailerParams
|
||
|
mailerParams.SetupCfg(cfg)
|
||
|
ctx = mctx.WithAnnotator(ctx, &mailerParams)
|
||
|
|
||
|
var mlParams mailinglist.Params
|
||
|
mlParams.SetupCfg(cfg)
|
||
|
ctx = mctx.WithAnnotator(ctx, &mlParams)
|
||
|
|
||
|
// initialization
|
||
|
err := cfg.Init(ctx)
|
||
|
|
||
|
logger := mlog.NewLogger(nil)
|
||
|
defer logger.Close()
|
||
|
|
||
|
logger.Info(ctx, "process started")
|
||
|
defer logger.Info(ctx, "process exiting")
|
||
|
|
||
|
if err != nil {
|
||
2 years ago
|
logger.Fatal(ctx, "initializing", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
clock := clock.Realtime()
|
||
|
|
||
|
var mailer mailinglist.Mailer
|
||
|
if mailerParams.SMTPAddr == "" {
|
||
|
logger.Info(ctx, "-smtp-addr not given, using NullMailer")
|
||
|
mailer = mailinglist.NullMailer
|
||
|
} else {
|
||
|
mailer = mailinglist.NewMailer(mailerParams)
|
||
|
}
|
||
|
|
||
2 years ago
|
mlStore, err := mailinglist.NewStore(dataDir)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logger.Fatal(ctx, "initializing mailing list storage", err)
|
||
3 years ago
|
}
|
||
|
defer mlStore.Close()
|
||
|
|
||
|
mlParams.Store = mlStore
|
||
|
mlParams.Mailer = mailer
|
||
|
mlParams.Clock = clock
|
||
|
|
||
|
ml := mailinglist.New(mlParams)
|
||
|
|
||
3 years ago
|
subCmd := cfg.SubCmd()
|
||
|
ctx = mctx.Annotate(ctx, "subCmd", subCmd)
|
||
3 years ago
|
|
||
3 years ago
|
switch subCmd {
|
||
2 years ago
|
|
||
3 years ago
|
case "list":
|
||
2 years ago
|
|
||
3 years ago
|
for it := mlStore.GetAll(); ; {
|
||
|
email, err := it()
|
||
|
if errors.Is(err, io.EOF) {
|
||
|
break
|
||
|
} else if err != nil {
|
||
2 years ago
|
logger.Fatal(ctx, "retrieving next email", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
ctx := mctx.Annotate(context.Background(),
|
||
|
"email", email.Email,
|
||
|
"createdAt", email.CreatedAt,
|
||
|
"verifiedAt", email.VerifiedAt,
|
||
|
)
|
||
|
|
||
|
logger.Info(ctx, "next")
|
||
|
}
|
||
|
|
||
3 years ago
|
case "publish":
|
||
|
|
||
|
title := cfg.String("title", "", "Title of the post which was published")
|
||
|
url := cfg.String("url", "", "URL of the post which was published")
|
||
2 years ago
|
|
||
|
if err := cfg.Init(ctx); err != nil {
|
||
|
logger.Fatal(ctx, "initializing", err)
|
||
|
}
|
||
3 years ago
|
|
||
|
if *title == "" {
|
||
2 years ago
|
logger.FatalString(ctx, "-title is required")
|
||
3 years ago
|
|
||
|
} else if *url == "" {
|
||
2 years ago
|
logger.FatalString(ctx, "-url is required")
|
||
3 years ago
|
}
|
||
|
|
||
|
err := ml.Publish(*title, *url)
|
||
|
if err != nil {
|
||
2 years ago
|
logger.Fatal(ctx, "publishing", err)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
default:
|
||
2 years ago
|
logger.FatalString(ctx, "invalid sub-command, must be list|publish")
|
||
3 years ago
|
}
|
||
|
}
|