()
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | flag.Parse() |
| 38 | |
| 39 | if *brokerList == "" { |
| 40 | printUsageErrorAndExit("no -brokers specified. Alternatively, set the KAFKA_PEERS environment variable") |
| 41 | } |
| 42 | |
| 43 | if *topic == "" { |
| 44 | printUsageErrorAndExit("no -topic specified") |
| 45 | } |
| 46 | |
| 47 | if *verbose { |
| 48 | sarama.Logger = logger |
| 49 | } |
| 50 | |
| 51 | config := sarama.NewConfig() |
| 52 | config.Producer.RequiredAcks = sarama.WaitForAll |
| 53 | config.Producer.Return.Successes = true |
| 54 | |
| 55 | if *tlsEnabled { |
| 56 | tlsConfig, err := tls.NewConfig(*tlsClientCert, *tlsClientKey) |
| 57 | if err != nil { |
| 58 | printErrorAndExit(69, "Failed to create TLS config: %s", err) |
| 59 | } |
| 60 | |
| 61 | config.Net.TLS.Enable = true |
| 62 | config.Net.TLS.Config = tlsConfig |
| 63 | config.Net.TLS.Config.InsecureSkipVerify = *tlsSkipVerify |
| 64 | } |
| 65 | |
| 66 | switch *partitioner { |
| 67 | case "": |
| 68 | if *partition >= 0 { |
| 69 | config.Producer.Partitioner = sarama.NewManualPartitioner |
| 70 | } else { |
| 71 | config.Producer.Partitioner = sarama.NewHashPartitioner |
| 72 | } |
| 73 | case "hash": |
| 74 | config.Producer.Partitioner = sarama.NewHashPartitioner |
| 75 | case "random": |
| 76 | config.Producer.Partitioner = sarama.NewRandomPartitioner |
| 77 | case "manual": |
| 78 | config.Producer.Partitioner = sarama.NewManualPartitioner |
| 79 | if *partition == -1 { |
| 80 | printUsageErrorAndExit("-partition is required when partitioning manually") |
| 81 | } |
| 82 | default: |
| 83 | printUsageErrorAndExit(fmt.Sprintf("Partitioner %s not supported.", *partitioner)) |
| 84 | } |
| 85 | |
| 86 | message := &sarama.ProducerMessage{Topic: *topic, Partition: int32(*partition)} |
| 87 | |
| 88 | if *key != "" { |
| 89 | message.Key = sarama.StringEncoder(*key) |
| 90 | } |
| 91 | |
| 92 | if *value != "" { |
| 93 | message.Value = sarama.StringEncoder(*value) |
nothing calls this directly
no test coverage detected