mirror of https://github.com/dapr/cli.git
Adds publish-app-id to publish command (#548)
This commit is contained in:
parent
d09b09c0dd
commit
c5b2e11775
|
|
@ -279,12 +279,12 @@ Publish a message:
|
|||
|
||||
* Linux/Mac
|
||||
```bash
|
||||
$ dapr publish --pubsub pubsub --topic myevent --data '{ "name": "yoda" }'
|
||||
$ dapr publish --publish-app-id nodeapp --pubsub pubsub --topic myevent --data '{ "name": "yoda" }'
|
||||
```
|
||||
|
||||
* Windows
|
||||
```bash
|
||||
C:> dapr publish --pubsub pubsub --topic myevent --data "{ \"name\": \"yoda\" }"
|
||||
C:> dapr publish --publish-app-id nodeapp --pubsub pubsub --topic myevent --data "{ \"name\": \"yoda\" }"
|
||||
```
|
||||
|
||||
### Invoking
|
||||
|
|
|
|||
|
|
@ -15,21 +15,22 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
publishAppID string
|
||||
pubsubName string
|
||||
publishTopic string
|
||||
publishPayload string
|
||||
pubsubName 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
|
||||
dapr publish --topic sample --pubsub target --data '{"key":"value"}'
|
||||
# Publish to sample topic in target pubsub via a publishing app
|
||||
dapr publish --publish-app-id myapp --pubsub target --topic sample --data '{"key":"value"}'
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client := standalone.NewClient()
|
||||
err := client.Publish(publishTopic, publishPayload, pubsubName)
|
||||
err := client.Publish(publishAppID, pubsubName, publishTopic, publishPayload)
|
||||
if err != nil {
|
||||
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error publishing topic %s: %s", publishTopic, err))
|
||||
os.Exit(1)
|
||||
|
|
@ -40,11 +41,12 @@ dapr publish --topic sample --pubsub target --data '{"key":"value"}'
|
|||
}
|
||||
|
||||
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(&pubsubName, "pubsub", "p", "", "The name of the pub/sub component")
|
||||
PublishCmd.Flags().BoolP("help", "h", false, "Print this help message")
|
||||
PublishCmd.MarkFlagRequired("app-id")
|
||||
PublishCmd.MarkFlagRequired("publish-app-id")
|
||||
PublishCmd.MarkFlagRequired("topic")
|
||||
PublishCmd.MarkFlagRequired("pubsub")
|
||||
RootCmd.AddCommand(PublishCmd)
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ type daprProcess struct {
|
|||
type Client interface {
|
||||
// Invoke is a command to invoke a remote or local dapr instance
|
||||
Invoke(appID, method, data, verb string) (string, error)
|
||||
// Publish is used to publish event to a topic in a pubsub.
|
||||
Publish(topic, payload, pubsubName string) error
|
||||
// Publish is used to publish event to a topic in a pubsub for an app ID.
|
||||
Publish(publishAppID, pubsubName, topic, payload string) error
|
||||
}
|
||||
|
||||
type Standalone struct {
|
||||
|
|
|
|||
|
|
@ -15,20 +15,25 @@ import (
|
|||
)
|
||||
|
||||
// Publish publishes payload to topic in pubsub referenced by pubsubName.
|
||||
func (s *Standalone) Publish(topic, payload, pubsubName string) error {
|
||||
if topic == "" {
|
||||
return errors.New("topic is missing")
|
||||
func (s *Standalone) Publish(publishAppID, pubsubName, topic, payload string) error {
|
||||
if publishAppID == "" {
|
||||
return errors.New("publishAppID is missing")
|
||||
}
|
||||
|
||||
if pubsubName == "" {
|
||||
return errors.New("pubsubName is missing")
|
||||
}
|
||||
|
||||
if topic == "" {
|
||||
return errors.New("topic is missing")
|
||||
}
|
||||
|
||||
l, err := s.process.List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
daprHTTPPort, err := getDaprHTTPPort(l)
|
||||
daprHTTPPort, err := getDaprHTTPPort(l, publishAppID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -53,9 +58,9 @@ func (s *Standalone) Publish(topic, payload, pubsubName string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getDaprHTTPPort(list []ListOutput) (int, error) {
|
||||
func getDaprHTTPPort(list []ListOutput, publishAppID string) (int, error) {
|
||||
for i := 0; i < len(list); i++ {
|
||||
if list[i].AppID != "" {
|
||||
if list[i].AppID == publishAppID {
|
||||
return list[i].HTTPPort, nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
func TestPublish(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
publishAppID string
|
||||
pubsubName string
|
||||
payload string
|
||||
topic string
|
||||
|
|
@ -27,6 +28,15 @@ func TestPublish(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "test empty topic",
|
||||
publishAppID: "",
|
||||
payload: "test",
|
||||
pubsubName: "test",
|
||||
errString: "publishAppID is missing",
|
||||
errorExpected: true,
|
||||
},
|
||||
{
|
||||
name: "test empty topic",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
pubsubName: "test",
|
||||
errString: "topic is missing",
|
||||
|
|
@ -34,6 +44,7 @@ func TestPublish(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "test empty pubsubName",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
topic: "test",
|
||||
errString: "pubsubName is missing",
|
||||
|
|
@ -41,6 +52,7 @@ func TestPublish(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "test list error",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
topic: "test",
|
||||
pubsubName: "test",
|
||||
|
|
@ -49,10 +61,11 @@ func TestPublish(t *testing.T) {
|
|||
errorExpected: true,
|
||||
},
|
||||
{
|
||||
name: "test empty appID in list output",
|
||||
payload: "test",
|
||||
topic: "test",
|
||||
pubsubName: "test",
|
||||
name: "test empty appID in list output",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
topic: "test",
|
||||
pubsubName: "test",
|
||||
lo: ListOutput{
|
||||
// empty appID
|
||||
Command: "test",
|
||||
|
|
@ -62,13 +75,26 @@ func TestPublish(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "successful call",
|
||||
publishAppID: "myAppID",
|
||||
pubsubName: "testPubsubName",
|
||||
topic: "testTopic",
|
||||
payload: "test payload",
|
||||
lo: ListOutput{
|
||||
AppID: "not my myAppID",
|
||||
},
|
||||
errString: "couldn't find a running Dapr instance",
|
||||
errorExpected: true,
|
||||
},
|
||||
{
|
||||
name: "successful call",
|
||||
publishAppID: "myAppID",
|
||||
pubsubName: "testPubsubName",
|
||||
topic: "testTopic",
|
||||
payload: "test payload",
|
||||
expectedPath: "/v1.0/publish/testPubsubName/testTopic",
|
||||
postResponse: "test payload",
|
||||
lo: ListOutput{
|
||||
AppID: "notempty",
|
||||
AppID: "myAppID",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -84,7 +110,7 @@ func TestPublish(t *testing.T) {
|
|||
Err: tc.listErr,
|
||||
},
|
||||
}
|
||||
err := client.Publish(tc.topic, tc.payload, tc.pubsubName)
|
||||
err := client.Publish(tc.publishAppID, tc.pubsubName, tc.topic, tc.payload)
|
||||
if tc.errorExpected {
|
||||
assert.Error(t, err, "expected an error")
|
||||
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
|
||||
|
|
|
|||
Loading…
Reference in New Issue