From eed10ce514f28e4acf772f76c92ca05eebec105f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 5 May 2022 21:20:22 -0600 Subject: Fix various problems with the srv build --- srv/src/cmd/mailinglist-cli/main.go | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 srv/src/cmd/mailinglist-cli/main.go (limited to 'srv/src/cmd/mailinglist-cli/main.go') diff --git a/srv/src/cmd/mailinglist-cli/main.go b/srv/src/cmd/mailinglist-cli/main.go new file mode 100644 index 0000000..c3207df --- /dev/null +++ b/srv/src/cmd/mailinglist-cli/main.go @@ -0,0 +1,120 @@ +package main + +import ( + "context" + "errors" + "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 main() { + + ctx := context.Background() + + cfg := cfg.New(cfg.Params{ + EnvPrefix: "MEDIOCRE_BLOG", + }) + + 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 { + logger.Fatal(ctx, "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) + } + + mailingListDBFile := path.Join(*dataDir, "mailinglist.sqlite3") + ctx = mctx.Annotate(ctx, "mailingListDBFile", mailingListDBFile) + + mlStore, err := mailinglist.NewStore(mailingListDBFile) + if err != nil { + logger.Fatal(ctx, "initializing mailing list storage", err) + } + defer mlStore.Close() + + mlParams.Store = mlStore + mlParams.Mailer = mailer + mlParams.Clock = clock + + ml := mailinglist.New(mlParams) + + 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 { + logger.Fatal(ctx, "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") + + if err := cfg.Init(ctx); err != nil { + logger.Fatal(ctx, "initializing", err) + } + + if *title == "" { + logger.FatalString(ctx, "-title is required") + + } else if *url == "" { + logger.FatalString(ctx, "-url is required") + } + + err := ml.Publish(*title, *url) + if err != nil { + logger.Fatal(ctx, "publishing", err) + } + + default: + logger.FatalString(ctx, "invalid sub-command, must be list|publish") + } +} -- cgit v1.2.3