-
Notifications
You must be signed in to change notification settings - Fork 72
/
produce.go
183 lines (145 loc) · 5.08 KB
/
produce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package produce
import (
"io"
"net/http"
"os"
kafkaflagutil "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
kafkainstanceclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/kafkainstance/apiv1/client"
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/kafkacmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)
var outputFormatTypes = []string{dump.JSONFormat, dump.YAMLFormat}
type options struct {
topicName string
kafkaID string
key string
file string
outputFormat string
partition int32
f *factory.Factory
}
// row is the details of a record produced needed to print to a table
type kafkaRow struct {
Topic string `json:"topic"`
Key string `json:"key"`
Value string `json:"value"`
Partition int32 `json:"partition"`
Offset int64 `json:"offset"`
}
// NewProduceTopicCommand creates a new command for producing to a kafka topic.
func NewProduceTopicCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "produce",
Short: f.Localizer.MustLocalize("kafka.topic.produce.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("kafka.topic.produce.cmd.longDescription"),
Example: f.Localizer.MustLocalize("kafka.topic.produce.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if opts.kafkaID == "" {
kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}
opts.kafkaID = kafkaInstance.GetId()
}
return runCmd(opts)
},
}
flags := kafkaflagutil.NewFlagSet(cmd, f.Localizer)
flags.StringVar(&opts.topicName, "name", "", f.Localizer.MustLocalize("kafka.topic.common.flag.name.description"))
flags.StringVar(&opts.key, "key", "", f.Localizer.MustLocalize("kafka.topic.produce.flag.key.description"))
flags.Int32Var(&opts.partition, "partition", 0, f.Localizer.MustLocalize("kafka.topic.produce.flag.partition.description"))
flags.StringVar(&opts.file, "file", "", f.Localizer.MustLocalize("kafka.topic.produce.flag.file.description"))
flags.StringVar(&opts.outputFormat, "format", "json", f.Localizer.MustLocalize("kafka.topic.produce.flag.format.description"))
_ = cmd.MarkFlagRequired("name")
_ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return kafkacmdutil.FilterValidTopicNameArgs(f, toComplete)
})
flags.AddInstanceID(&opts.kafkaID)
flagutil.EnableStaticFlagCompletion(cmd, "format", outputFormatTypes)
return cmd
}
func runCmd(opts *options) error {
conn, err := opts.f.Connection()
if err != nil {
return err
}
api, _, err := conn.API().KafkaAdmin(opts.kafkaID)
if err != nil {
return err
}
var value string
if opts.file != "" {
bytes, readError := os.ReadFile(opts.file)
if readError != nil {
return readError
}
value = string(bytes)
} else {
// if value is being piped then cannot have delimeter as \n
info, _ := os.Stdin.Stat()
if info.Mode()&os.ModeCharDevice == 0 {
bytes, readError := io.ReadAll(os.Stdin)
if readError != nil {
return readError
}
value = string(bytes)
} else {
promptName := &survey.Input{
Message: opts.f.Localizer.MustLocalize("kafka.topic.produce.input.value"),
Help: opts.f.Localizer.MustLocalize("kafka.topic.produce.input.help"),
}
surveyErr := survey.AskOne(promptName, &value)
if surveyErr != nil {
return surveyErr
}
}
}
record := kafkainstanceclient.Record{
Key: &opts.key,
Value: value,
Partition: &opts.partition,
}
record, httpRes, err := api.RecordsApi.ProduceRecord(opts.f.Context, opts.topicName).Record(record).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
if httpRes == nil {
return err
}
if httpRes.StatusCode == http.StatusRequestEntityTooLarge {
return opts.f.Localizer.MustLocalizeError("kafka.topic.produce.error.messageTooLarge")
}
return err
}
format := opts.outputFormat
if format == dump.EmptyFormat {
format = dump.JSONFormat
}
_ = dump.Formatted(opts.f.IOStreams.Out, format, mapRecordToRow(opts.topicName, &record))
opts.f.Logger.Info("")
opts.f.Logger.Info(icon.SuccessPrefix(), opts.f.Localizer.MustLocalize("kafka.topic.produce.info.produceSuccess",
localize.NewEntry("Topic", opts.topicName),
localize.NewEntry("Offset", record.Offset)))
return nil
}
func mapRecordToRow(topic string, record *kafkainstanceclient.Record) kafkaRow {
return kafkaRow{
Topic: topic,
Key: *record.Key,
Value: record.Value,
Partition: *record.Partition,
Offset: *record.Offset,
}
}