1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package main
import (
"context"
"errors"
"fmt"
"io"
"path"
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"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 loggerFatalErr(ctx context.Context, logger *mlog.Logger, descr string, err error) {
logger.Fatal(ctx, fmt.Sprintf("%s: %v", descr, err))
}
func main() {
ctx := context.Background()
cfg := cfg.New()
dataDir := cfg.String("data-dir", ".", "Directory to use for long term storage")
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 {
loggerFatalErr(ctx, logger, "initializing", err)
}
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)
}
mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3"))
if err != nil {
loggerFatalErr(ctx, logger, "initializing mailing list storage", err)
}
defer mlStore.Close()
mlParams.Store = mlStore
mlParams.Mailer = mailer
mlParams.Clock = clock
ml := mailinglist.New(mlParams)
_ = ml
subCmd := cfg.SubCmd()
ctx = mctx.Annotate(ctx, "subCmd", subCmd)
switch subCmd {
case "list":
for it := mlStore.GetAll(); ; {
email, err := it()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
loggerFatalErr(ctx, logger, "retrieving next email", err)
}
ctx := mctx.Annotate(context.Background(),
"email", email.Email,
"createdAt", email.CreatedAt,
"verifiedAt", email.VerifiedAt,
)
logger.Info(ctx, "next")
}
case "publish":
title := cfg.String("title", "", "Title of the post which was published")
url := cfg.String("url", "", "URL of the post which was published")
cfg.Init(ctx)
if *title == "" {
logger.Fatal(ctx, "-title is required")
} else if *url == "" {
logger.Fatal(ctx, "-url is required")
}
err := ml.Publish(*title, *url)
if err != nil {
loggerFatalErr(ctx, logger, "publishing", err)
}
default:
logger.Fatal(ctx, "invalid sub-command, must be list|publish")
}
}
|