Skip to content

Commit 9b09240

Browse files
committed
feat: file flag for topic produce
1 parent 5201eb6 commit 9b09240

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

docs/commands/rhoas_kafka_topic_produce.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/kafka/topic/produce/produce.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type options struct {
2020
topicName string
2121
kafkaID string
2222
key string
23+
file string
2324
partition int32
2425

2526
f *factory.Factory
@@ -57,6 +58,7 @@ func NewProduceTopicCommand(f *factory.Factory) *cobra.Command {
5758
flags.StringVar(&opts.topicName, "name", "", f.Localizer.MustLocalize("kafka.topic.common.flag.name.description"))
5859
flags.StringVar(&opts.key, "key", "", f.Localizer.MustLocalize("kafka.topic.produce.flag.key.description"))
5960
flags.Int32Var(&opts.partition, "partition", 0, f.Localizer.MustLocalize("kafka.topic.produce.flag.partition.description"))
61+
flags.StringVar(&opts.file, "file", "", "File location of value")
6062

6163
_ = cmd.MarkFlagRequired("name")
6264

@@ -82,19 +84,29 @@ func runCmd(opts *options) error {
8284

8385
var value string
8486

85-
// if value being piped then cannot have delimeter as \n
86-
info, _ := os.Stdin.Stat()
87-
if info.Mode()&os.ModeCharDevice == 0 {
88-
bytes, err := ioutil.ReadAll(os.Stdin)
87+
if opts.file != "" {
88+
bytes, err := ioutil.ReadFile(opts.file)
8989
if err != nil {
9090
return err
9191
}
9292

9393
value = string(bytes)
9494
} else {
95-
value, err = bufio.NewReader(os.Stdin).ReadString('\n')
96-
if err != nil {
97-
return err
95+
// if value is being piped then cannot have delimeter as \n
96+
info, _ := os.Stdin.Stat()
97+
if info.Mode()&os.ModeCharDevice == 0 {
98+
bytes, err := ioutil.ReadAll(os.Stdin)
99+
if err != nil {
100+
return err
101+
}
102+
103+
value = string(bytes)
104+
} else {
105+
value, err = bufio.NewReader(os.Stdin).ReadString('\n')
106+
if err != nil {
107+
return err
108+
}
109+
98110
}
99111
}
100112

0 commit comments

Comments
 (0)