-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
112 lines (98 loc) · 4.21 KB
/
publish.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
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"os"
"runtime"
"github.com/spf13/cobra"
"github.com/halokid/daprcli/pkg/print"
"github.com/halokid/daprcli/pkg/standalone"
)
var (
publishAppID string
pubsubName string
publishTopic string
publishPayload string
publishPayloadFile string
publishSocket string
publishMetadata string
)
var PublishCmd = &cobra.Command{
Use: "publish",
Short: "Publish a pub-sub event. Supported platforms: Self-hosted",
Example: `
# Publish to sample topic in target pubsub via a publishing app
dapr publish --publish-app-id myapp --pubsub target --topic sample --data '{"key":"value"}'
# Publish to sample topic in target pubsub via a publishing app using Unix domain socket
dapr publish --enable-domain-socket --publish-app-id myapp --pubsub target --topic sample --data '{"key":"value"}'
# Publish to sample topic in target pubsub via a publishing app without cloud event
dapr publish --publish-app-id myapp --pubsub target --topic sample --data '{"key":"value"}' --metadata '{"rawPayload":"true","ttlInSeconds":"10"}'
`,
Run: func(cmd *cobra.Command, args []string) {
bytePayload := []byte{}
var err error
if publishPayloadFile != "" && publishPayload != "" {
print.FailureStatusEvent(os.Stderr, "Only one of --data and --data-file allowed in the same publish command")
os.Exit(1)
}
if publishPayloadFile != "" {
bytePayload, err = os.ReadFile(publishPayloadFile)
if err != nil {
print.FailureStatusEvent(os.Stderr, "Error reading payload from '%s'. Error: %s", publishPayloadFile, err)
os.Exit(1)
}
} else if publishPayload != "" {
bytePayload = []byte(publishPayload)
}
client := standalone.NewClient()
// TODO(@daixiang0): add Windows support.
if publishSocket != "" {
if runtime.GOOS == string(windowsOsType) {
print.FailureStatusEvent(os.Stderr, "The unix-domain-socket option is not supported on Windows")
os.Exit(1)
} else {
print.WarningStatusEvent(os.Stdout, "Unix domain sockets are currently a preview feature")
}
}
metadata := make(map[string]interface{})
if publishMetadata != "" {
err = json.Unmarshal([]byte(publishMetadata), &metadata)
if err != nil {
print.FailureStatusEvent(os.Stderr, "Error parsing metadata as JSON. Error: %s", err)
os.Exit(1)
}
}
err = client.Publish(publishAppID, pubsubName, publishTopic, bytePayload, publishSocket, metadata)
if err != nil {
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error publishing topic %s: %s", publishTopic, err))
os.Exit(1)
}
print.SuccessStatusEvent(os.Stdout, "Event published successfully")
},
}
func init() {
PublishCmd.Flags().StringVarP(&publishAppID, "publish-app-id", "i", "", "The ID of the publishing app")
PublishCmd.Flags().StringVarP(&pubsubName, "pubsub", "p", "", "The name of the pub/sub component")
PublishCmd.Flags().StringVarP(&publishTopic, "topic", "t", "", "The topic to be published to")
PublishCmd.Flags().StringVarP(&publishPayload, "data", "d", "", "The JSON serialized data string (optional)")
PublishCmd.Flags().StringVarP(&publishPayloadFile, "data-file", "f", "", "A file containing the JSON serialized data (optional)")
PublishCmd.Flags().StringVarP(&publishSocket, "unix-domain-socket", "u", "", "Path to a unix domain socket dir. If specified, Dapr API servers will use Unix Domain Sockets")
PublishCmd.Flags().StringVarP(&publishMetadata, "metadata", "m", "", "The JSON serialized publish metadata (optional)")
PublishCmd.Flags().BoolP("help", "h", false, "Print this help message")
PublishCmd.MarkFlagRequired("publish-app-id")
PublishCmd.MarkFlagRequired("topic")
PublishCmd.MarkFlagRequired("pubsub")
RootCmd.AddCommand(PublishCmd)
}