get a basic mailinglist-cli working
This commit is contained in:
parent
6feffc568a
commit
ca27cd6c67
3
Makefile
3
Makefile
@ -12,6 +12,9 @@ install.prod:
|
||||
srv.shell:
|
||||
nix-shell -A srv.shell --command 'cd srv; return'
|
||||
|
||||
srv.shell.prod:
|
||||
nix-shell -A srv.shell --arg baseConfig '(import ./prod.config.nix)' --command 'cd srv; return'
|
||||
|
||||
static.shell:
|
||||
nix-shell -A static.shell --command 'cd static; return'
|
||||
|
||||
|
100
srv/cmd/mailinglist-cli/main.go
Normal file
100
srv/cmd/mailinglist-cli/main.go
Normal file
@ -0,0 +1,100 @@
|
||||
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
|
||||
|
||||
args := cfg.Args()
|
||||
if len(args) == 0 {
|
||||
args = append(args, "")
|
||||
}
|
||||
|
||||
action, args := args[0], args[1:]
|
||||
|
||||
switch action {
|
||||
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")
|
||||
}
|
||||
|
||||
default:
|
||||
logger.Fatal(ctx, "invalid action")
|
||||
}
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
{pkgs, config, staticBuild}: rec {
|
||||
|
||||
opts = [
|
||||
"-pow-secret=${config.powSecret}"
|
||||
mailingListOpts = [
|
||||
"-ml-smtp-addr=${config.mlSMTPAddr}"
|
||||
"-ml-smtp-auth='${config.mlSMTPAuth}'"
|
||||
"-data-dir=${config.dataDir}"
|
||||
"-public-url=${config.publicURL}"
|
||||
];
|
||||
|
||||
opts = mailingListOpts ++ [
|
||||
"-pow-secret=${config.powSecret}"
|
||||
"-listen-proto=${config.listenProto}"
|
||||
"-listen-addr=${config.listenAddr}"
|
||||
] ++ (
|
||||
@ -30,9 +33,13 @@
|
||||
go run ./cmd/mediocre-blog/main.go ${toString opts}
|
||||
'';
|
||||
|
||||
runMailingListCLIScript = pkgs.writeScriptBin "run-mailinglist-cli" ''
|
||||
go run ./cmd/mailinglist-cli/main.go ${toString mailingListOpts} $@
|
||||
'';
|
||||
|
||||
shell = pkgs.stdenv.mkDerivation {
|
||||
name = "mediocre-blog-srv-shell";
|
||||
buildInputs = [ pkgs.go runScript ];
|
||||
buildInputs = [ pkgs.go runScript runMailingListCLIScript ];
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user