You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.0 KiB

2 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/client"
  7. "os"
  8. "time"
  9. )
  10. var (
  11. master = flag.String("master", "localhost:9333", "master csv list")
  12. topic = flag.String("topic", "", "topic name")
  13. )
  14. func main() {
  15. flag.Parse()
  16. publisher := client.NewPublisher(&client.PublisherOption{
  17. Masters: *master,
  18. Topic: *topic,
  19. })
  20. err := eachLineStdin(func(line string) error {
  21. if len(line) > 0 {
  22. if err := publisher.Publish(&client.Message{
  23. Key: nil,
  24. Content: []byte(line),
  25. Properties: nil,
  26. Ts: time.Time{},
  27. }); err != nil {
  28. return err
  29. }
  30. }
  31. return nil
  32. })
  33. publisher.Shutdown()
  34. if err != nil {
  35. fmt.Printf("error: %v\n", err)
  36. }
  37. }
  38. func eachLineStdin(eachLineFn func(string) error) error {
  39. scanner := bufio.NewScanner(os.Stdin)
  40. for scanner.Scan() {
  41. text := scanner.Text()
  42. if err := eachLineFn(text); err != nil {
  43. return err
  44. }
  45. }
  46. // handle error
  47. if scanner.Err() != nil {
  48. return fmt.Errorf("scan stdin: %v", scanner.Err())
  49. }
  50. return nil
  51. }