add pubsub component name to publish command (#435)

This commit is contained in:
Yaron Schneider 2020-08-14 09:36:25 -07:00 committed by GitHub
parent 294fa19a20
commit edaae6e199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -16,12 +16,13 @@ import (
var publishTopic string
var publishPayload string
var pubsubName string
var PublishCmd = &cobra.Command{
Use: "publish",
Short: "Publish an event to multiple consumers",
Run: func(cmd *cobra.Command, args []string) {
err := publish.SendPayloadToTopic(publishTopic, publishPayload)
err := publish.SendPayloadToTopic(publishTopic, publishPayload, pubsubName)
if err != nil {
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error publishing topic %s: %s", publishTopic, err))
return
@ -34,7 +35,9 @@ var PublishCmd = &cobra.Command{
func init() {
PublishCmd.Flags().StringVarP(&publishTopic, "topic", "t", "", "the topic the app is listening on")
PublishCmd.Flags().StringVarP(&publishPayload, "data", "d", "", "(optional) a json serialized string")
PublishCmd.Flags().StringVarP(&pubsubName, "pubsub", "", "", "name of the pub/sub component")
PublishCmd.MarkFlagRequired("app-id")
PublishCmd.MarkFlagRequired("topic")
PublishCmd.MarkFlagRequired("pubsub")
RootCmd.AddCommand(PublishCmd)
}

View File

@ -16,10 +16,13 @@ import (
)
// SendPayloadToTopic publishes the topic
func SendPayloadToTopic(topic, payload string) error {
func SendPayloadToTopic(topic, payload, pubsubName string) error {
if topic == "" {
return errors.New("topic is missing")
}
if pubsubName == "" {
return errors.New("pubsubName is missing")
}
l, err := standalone.List()
if err != nil {
@ -37,7 +40,7 @@ func SendPayloadToTopic(topic, payload string) error {
b = []byte(payload)
}
url := fmt.Sprintf("http://localhost:%s/v%s/publish/%s", fmt.Sprintf("%v", app.HTTPPort), api.RuntimeAPIVersion, topic)
url := fmt.Sprintf("http://localhost:%s/v%s/publish/%s/%s", fmt.Sprintf("%v", app.HTTPPort), api.RuntimeAPIVersion, pubsubName, topic)
// nolint: gosec
r, err := http.Post(url, "application/json", bytes.NewBuffer(b))