diff --git a/cmd/beemo/main.go b/cmd/beemo/main.go index 75e23db5..1658b410 100644 --- a/cmd/beemo/main.go +++ b/cmd/beemo/main.go @@ -4,6 +4,7 @@ package main import ( + "context" "io" "log/slog" "os" @@ -13,7 +14,7 @@ import ( _ "go.uber.org/automaxprocs" "github.com/earthboundkid/versioninfo/v2" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) func main() { @@ -25,7 +26,7 @@ func main() { func run(args []string) error { - app := cli.App{ + app := cli.Command{ Name: "beemo", Usage: "bluesky moderation reporting bot", Version: versioninfo.Short(), @@ -35,14 +36,14 @@ func run(args []string) error { &cli.StringFlag{ Name: "log-level", Usage: "log verbosity level (eg: warn, info, debug)", - EnvVars: []string{"BEEMO_LOG_LEVEL", "GO_LOG_LEVEL", "LOG_LEVEL"}, + Sources: cli.EnvVars("BEEMO_LOG_LEVEL", "GO_LOG_LEVEL", "LOG_LEVEL"), }, &cli.StringFlag{ Name: "slack-webhook-url", // eg: https://hooks.slack.com/services/X1234 Usage: "full URL of slack webhook", Required: true, - EnvVars: []string{"SLACK_WEBHOOK_URL"}, + Sources: cli.EnvVars("SLACK_WEBHOOK_URL"), }, } app.Commands = []*cli.Command{ @@ -55,37 +56,37 @@ func run(args []string) error { Name: "pds-host", Usage: "method, hostname, and port of PDS instance", Value: "http://localhost:4849", - EnvVars: []string{"ATP_PDS_HOST"}, + Sources: cli.EnvVars("ATP_PDS_HOST"), }, &cli.StringFlag{ Name: "admin-host", Usage: "method, hostname, and port of admin interface (eg, Ozone), for direct links", Value: "http://localhost:3000", - EnvVars: []string{"ATP_ADMIN_HOST"}, + Sources: cli.EnvVars("ATP_ADMIN_HOST"), }, &cli.IntFlag{ Name: "poll-period", Usage: "API poll period in seconds", Value: 30, - EnvVars: []string{"POLL_PERIOD"}, + Sources: cli.EnvVars("POLL_PERIOD"), }, &cli.StringFlag{ Name: "handle", Usage: "for PDS login", Required: true, - EnvVars: []string{"ATP_AUTH_HANDLE"}, + Sources: cli.EnvVars("ATP_AUTH_HANDLE"), }, &cli.StringFlag{ Name: "password", Usage: "for PDS login", Required: true, - EnvVars: []string{"ATP_AUTH_PASSWORD"}, + Sources: cli.EnvVars("ATP_AUTH_PASSWORD"), }, &cli.StringFlag{ Name: "admin-password", Usage: "admin authentication password for PDS", Required: true, - EnvVars: []string{"ATP_AUTH_ADMIN_PASSWORD"}, + Sources: cli.EnvVars("ATP_AUTH_ADMIN_PASSWORD"), }, }, }, @@ -98,29 +99,29 @@ func run(args []string) error { Name: "relay-host", Usage: "method, hostname, and port of Relay instance (websocket)", Value: "wss://bsky.network", - EnvVars: []string{"ATP_RELAY_HOST"}, + Sources: cli.EnvVars("ATP_RELAY_HOST"), }, &cli.StringFlag{ Name: "mention-dids", Usage: "DIDs to look for in mentions (comma-separated)", Required: true, - EnvVars: []string{"BEEMO_MENTION_DIDS"}, + Sources: cli.EnvVars("BEEMO_MENTION_DIDS"), }, &cli.IntFlag{ Name: "minimum-words", Usage: "minimum length of post text (word count; zero for no minimum)", Value: 0, - EnvVars: []string{"BEEMO_MINIMUM_WORDS"}, + Sources: cli.EnvVars("BEEMO_MINIMUM_WORDS"), }, }, }, } - return app.Run(args) + return app.Run(context.Background(), args) } -func configLogger(cctx *cli.Context, writer io.Writer) *slog.Logger { +func configLogger(cmd *cli.Command, writer io.Writer) *slog.Logger { var level slog.Level - switch strings.ToLower(cctx.String("log-level")) { + switch strings.ToLower(cmd.String("log-level")) { case "error": level = slog.LevelError case "warn": diff --git a/cmd/beemo/notify_mentions.go b/cmd/beemo/notify_mentions.go index 7e4c5d14..f1cba426 100644 --- a/cmd/beemo/notify_mentions.go +++ b/cmd/beemo/notify_mentions.go @@ -11,7 +11,7 @@ import ( "github.com/bluesky-social/indigo/atproto/identity" "github.com/bluesky-social/indigo/atproto/syntax" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) type MentionChecker struct { @@ -61,14 +61,13 @@ func (mc *MentionChecker) ProcessPost(ctx context.Context, did syntax.DID, rkey return nil } -func notifyMentions(cctx *cli.Context) error { - ctx := context.Background() - logger := configLogger(cctx, os.Stdout) - relayHost := cctx.String("relay-host") - minimumWords := cctx.Int("minimum-words") +func notifyMentions(ctx context.Context, cmd *cli.Command) error { + logger := configLogger(cmd, os.Stdout) + relayHost := cmd.String("relay-host") + minimumWords := cmd.Int("minimum-words") mentionDIDs := []syntax.DID{} - for _, raw := range strings.Split(cctx.String("mention-dids"), ",") { + for _, raw := range strings.Split(cmd.String("mention-dids"), ",") { did, err := syntax.ParseDID(raw) if err != nil { return err @@ -77,7 +76,7 @@ func notifyMentions(cctx *cli.Context) error { } checker := MentionChecker{ - slackWebhookURL: cctx.String("slack-webhook-url"), + slackWebhookURL: cmd.String("slack-webhook-url"), mentionDIDs: mentionDIDs, logger: logger, directory: identity.DefaultDirectory(), diff --git a/cmd/beemo/notify_reports.go b/cmd/beemo/notify_reports.go index b81bc212..a3b0c6fb 100644 --- a/cmd/beemo/notify_reports.go +++ b/cmd/beemo/notify_reports.go @@ -12,30 +12,29 @@ import ( "github.com/bluesky-social/indigo/util" "github.com/bluesky-social/indigo/xrpc" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) -func pollNewReports(cctx *cli.Context) error { - ctx := context.Background() - logger := configLogger(cctx, os.Stdout) - slackWebhookURL := cctx.String("slack-webhook-url") +func pollNewReports(ctx context.Context, cmd *cli.Command) error { + logger := configLogger(cmd, os.Stdout) + slackWebhookURL := cmd.String("slack-webhook-url") // record last-seen report timestamp since := time.Now() // NOTE: uncomment this for testing //since = time.Now().Add(time.Duration(-12) * time.Hour) - period := time.Duration(cctx.Int("poll-period")) * time.Second + period := time.Duration(cmd.Int("poll-period")) * time.Second // create a new session xrpcc := &xrpc.Client{ Client: util.RobustHTTPClient(), - Host: cctx.String("pds-host"), - Auth: &xrpc.AuthInfo{Handle: cctx.String("handle")}, + Host: cmd.String("pds-host"), + Auth: &xrpc.AuthInfo{Handle: cmd.String("handle")}, } auth, err := comatproto.ServerCreateSession(ctx, xrpcc, &comatproto.ServerCreateSession_Input{ Identifier: xrpcc.Auth.Handle, - Password: cctx.String("password"), + Password: cmd.String("password"), }) if err != nil { return err @@ -45,7 +44,7 @@ func pollNewReports(cctx *cli.Context) error { xrpcc.Auth.Did = auth.Did xrpcc.Auth.Handle = auth.Handle - adminToken := cctx.String("admin-password") + adminToken := cmd.String("admin-password") if len(adminToken) > 0 { xrpcc.AdminToken = &adminToken } @@ -70,7 +69,7 @@ func pollNewReports(cctx *cli.Context) error { // query just new reports (regardless of resolution state) var limit int64 = 50 me, err := toolsozone.ModerationQueryEvents( - cctx.Context, + ctx, xrpcc, nil, // addedLabels []string nil, // addedTags []string @@ -113,9 +112,9 @@ func pollNewReports(cctx *cli.Context) error { // ok, we found a "new" report, need to notify msg := fmt.Sprintf("⚠️ New report at `%s` ⚠️\n", evt.CreatedAt) msg += fmt.Sprintf("report id: `%d`\t", evt.Id) - msg += fmt.Sprintf("instance: `%s`\n", cctx.String("pds-host")) + msg += fmt.Sprintf("instance: `%s`\n", cmd.String("pds-host")) msg += fmt.Sprintf("reasonType: `%s`\t", shortType) - msg += fmt.Sprintf("Admin: %s/reports/%d\n", cctx.String("admin-host"), evt.Id) + msg += fmt.Sprintf("Admin: %s/reports/%d\n", cmd.String("admin-host"), evt.Id) //msg += fmt.Sprintf("reportedByDid: `%s`\n", report.ReportedByDid) logger.Info("found new report, notifying slack", "report", report) err := sendSlackMsg(ctx, msg, slackWebhookURL)