micro--go-micro
3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
85 行
1.9 KiB
Go
85 行
1.9 KiB
Go
package resource
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"go-micro.dev/v6/broker"
|
|
)
|
|
|
|
// brokerCommand exposes the broker interface: publish, subscribe.
|
|
func brokerCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "broker",
|
|
Usage: "Publish and subscribe to broker topics",
|
|
Description: `Interact with the message broker.
|
|
|
|
micro broker publish <topic> <message> Publish a message to a topic
|
|
micro broker subscribe <topic> Stream messages from a topic`,
|
|
Subcommands: []*cli.Command{
|
|
{
|
|
Name: "publish",
|
|
Usage: "Publish a message to a topic",
|
|
ArgsUsage: "<topic> <message>",
|
|
Action: brokerPublish,
|
|
},
|
|
{
|
|
Name: "subscribe",
|
|
Usage: "Stream messages from a topic",
|
|
ArgsUsage: "<topic>",
|
|
Action: brokerSubscribe,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func brokerPublish(c *cli.Context) error {
|
|
topic := c.Args().Get(0)
|
|
msg := c.Args().Get(1)
|
|
if topic == "" || msg == "" {
|
|
return fail("usage: micro broker publish <topic> <message>")
|
|
}
|
|
|
|
b := broker.DefaultBroker
|
|
if err := b.Connect(); err != nil {
|
|
return fail("broker connect: %v", err)
|
|
}
|
|
|
|
if err := b.Publish(topic, &broker.Message{Body: []byte(msg)}); err != nil {
|
|
return fail("publish: %v", err)
|
|
}
|
|
|
|
fmt.Printf("Published to %q\n", topic)
|
|
return nil
|
|
}
|
|
|
|
func brokerSubscribe(c *cli.Context) error {
|
|
topic := c.Args().First()
|
|
if topic == "" {
|
|
return fail("usage: micro broker subscribe <topic>")
|
|
}
|
|
|
|
b := broker.DefaultBroker
|
|
if err := b.Connect(); err != nil {
|
|
return fail("broker connect: %v", err)
|
|
}
|
|
|
|
sub, err := b.Subscribe(topic, func(e broker.Event) error {
|
|
fmt.Printf("%s\n", string(e.Message().Body))
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return fail("subscribe: %v", err)
|
|
}
|
|
defer func() { _ = sub.Unsubscribe() }()
|
|
|
|
fmt.Printf("Subscribed to %q (Ctrl-C to stop)...\n", topic)
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
<-sig
|
|
return nil
|
|
}
|