Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add subscriptions command to Dapr CLI #1424

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cmd/subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (

Check failure on line 3 in cmd/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofumpt`-ed (gofumpt)
"github.com/dapr/cli/pkg/print"

Check failure on line 4 in cmd/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `goimports`-ed with -local github.com/dapr/ (goimports)
"github.com/dapr/cli/pkg/standalone"
"github.com/spf13/cobra"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
)

/*
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.
*/

var (
subscriptionsAppId string

Check failure on line 25 in cmd/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

ST1003: var subscriptionsAppId should be subscriptionsAppID (stylecheck)
)

var SubscriptionsCmd = &cobra.Command{
Use: "subscriptions",
Short: "List all Dapr subscriptions. Supported platforms: Supported platforms: Kubernetes and self-hosted",
PreRun: func(cmd *cobra.Command, args []string) {
if outputFormat != "" && outputFormat != "json" && outputFormat != "yaml" && outputFormat != "table" {
print.FailureStatusEvent(os.Stdout, "An invalid output format was specified.")
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
if kubernetesMode {
if allNamespaces {
resourceNamespace = meta_v1.NamespaceAll
} else if resourceNamespace == "" {
resourceNamespace = meta_v1.NamespaceAll
}
//do something to get it in k8s

Check failure on line 44 in cmd/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

commentFormatting: put a space between `//` and comment text (gocritic)
} else {
list, err := standalone.Subscriptions(subscriptionsAppId)
if err != nil {
return
}
outputList(list, len(list))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure if I should reuse that here?

}
},
Example: `
# List Dapr subscriptions for a given app
dapr components --app-id myapp
`,
}

func init() {
SubscriptionsCmd.Flags().StringVarP(&subscriptionsAppId, "app-id", "a", "", "The application id to be stopped")
SubscriptionsCmd.MarkFlagRequired("app-id")
SubscriptionsCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "The output format of the list. Valid values are: json, yaml, or table (default)")
RootCmd.AddCommand(SubscriptionsCmd)
}
12 changes: 12 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
type Metadata struct {
ID string `json:"id"`
ActiveActorsCount []MetadataActiveActorsCount `json:"actors"`
Subscriptions []Subscription `json:"subscriptions"`
Extended map[string]string `json:"extended"`
}

Expand All @@ -30,3 +31,14 @@ type MetadataActiveActorsCount struct {
Type string `json:"type"`
Count int `json:"count"`
}

type Subscription struct {
PubSubName string `json:"pubsubname"`
Topic string `json:"topic"`
Rules []Rule `json:"rules"`
DeadLetterTopic string `json:"deadLetterTopic"`
}

type Rule struct {
Path string `json:"path"`
}
55 changes: 55 additions & 0 deletions pkg/standalone/subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package standalone

import (
"errors"

Check failure on line 4 in pkg/standalone/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofumpt`-ed (gofumpt)
"github.com/dapr/cli/pkg/metadata"
)

// ListOutput represents the application ID, application port and creation time.
type SubscriptionsOutput struct {
AppID string `csv:"APP ID" json:"appId" yaml:"appId"`
Topic string `csv:"TOPIC" json:"topic" yaml:"topic"`
PubSubName string `csv:"PUBSUBNAME" json:"pubsubname" yaml:"pubsubname"`
Paths []string `csv:"PATHS" json:"paths" yaml:"paths"`
}

// Stop terminates the application process.
func Subscriptions(appID string) ([]SubscriptionsOutput, error) {
l, err := List()
if err != nil {
return nil, err
}

if len(l) == 0 {
return nil, errors.New("no running Dapr sidecars found")
}

instance, err := getDaprInstance(l, appID)
if err != nil {
return nil, err
}

m, err := metadata.Get(instance.HTTPPort, instance.AppID, "")

Check failure on line 33 in pkg/standalone/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return nil, err
}

var output []SubscriptionsOutput

Check failure on line 38 in pkg/standalone/subscriptions.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Consider pre-allocating `output` (prealloc)

for _, sub := range m.Subscriptions {
o := SubscriptionsOutput{
AppID: appID,
Topic: sub.Topic,
PubSubName: sub.PubSubName,
}

for _, r := range sub.Rules {
o.Paths = append(o.Paths, r.Path)
}

output = append(output, o)
}

return output, nil
}
Loading