Skip to content

Commit

Permalink
Merge pull request #187 from anhtranbk/feat/add-cmd-list-all-topics
Browse files Browse the repository at this point in the history
feat: add new command list all topics
  • Loading branch information
k-yomo authored Jan 30, 2024
2 parents fe54639 + a3a64b4 commit 2ba3e8a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
56 changes: 56 additions & 0 deletions cmd/list_topics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"context"
"io"

"github.com/k-yomo/pubsub_cli/pkg"
"github.com/mitchellh/colorstring"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// newCreateTopicCmd returns the command to create topics
func newListTopicsCmd(out io.Writer) *cobra.Command {
command := &cobra.Command{
Use: "list",
Short: "lists all Pub/Sub topics in the given project",
Long: "lists all Pub/Sub topics in the given project",
Example: "pubsub_cli list",
Aliases: []string{"ls"},
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
projectID, err := cmd.Flags().GetString(projectFlagName)
if err != nil {
return err
}
emulatorHost, err := cmd.Flags().GetString(hostFlagName)
if err != nil {
return err
}
gcpCredentialFilePath, err := cmd.Flags().GetString(credFileFlagName)
if err != nil {
return err
}
pubsubClient, err := pkg.NewPubSubClient(cmd.Context(), projectID, emulatorHost, gcpCredentialFilePath)
if err != nil {
return errors.Wrap(err, "initialize pubsub client")
}
return listTopics(cmd.Context(), out, pubsubClient)
},
}
command.SetOut(out)
return command
}

// listTopics lists all Pub/Sub topics in the project
func listTopics(ctx context.Context, out io.Writer, pubsubClient *pkg.PubSubClient) error {
topics, err := pubsubClient.FindAllTopics(ctx)
if err != nil {
return errors.Wrapf(err, "list topics")
}
for _, topic := range topics {
_, _ = colorstring.Fprintf(out, "[green]name: '%s'\n", topic.String())
}
return nil
}
78 changes: 78 additions & 0 deletions cmd/list_topics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"bytes"
"testing"

"github.com/k-yomo/pubsub_cli/pkg"
"github.com/spf13/cobra"
)

func Test_listTopics(t *testing.T) {
t.Parallel()

_, err := pkg.NewTestPubSubClient(t)
if err != nil {
t.Fatal(err)
}

type args struct {
rootCmd *cobra.Command
args []string
}
tests := []struct {
name string
args args
check func()
wantErr bool
}{
{
name: "parent cmd without projectFlag causes error",
args: args{rootCmd: func() *cobra.Command {
cmd := &cobra.Command{}
cmd.PersistentFlags().String(hostFlagName, "host", "")
cmd.PersistentFlags().String(credFileFlagName, "cred.json", "")
return cmd
}(), args: []string{"create_topic", "test_topic"}},
check: func() {},
wantErr: true,
},
{
name: "parent cmd without hostFlag causes error",
args: args{rootCmd: func() *cobra.Command {
cmd := &cobra.Command{}
cmd.PersistentFlags().String(projectFlagName, "project", "")
cmd.PersistentFlags().String(credFileFlagName, "cred.json", "")
return cmd
}(), args: []string{"create_topic", "test_topic"}},
check: func() {},
wantErr: true,
},
{
name: "parent cmd without credFileFlag causes error",
args: args{rootCmd: func() *cobra.Command {
cmd := &cobra.Command{}
cmd.PersistentFlags().String(projectFlagName, "project", "")
cmd.PersistentFlags().String(hostFlagName, "host", "")
return cmd
}(), args: []string{"create_topic", "test_topic"}},
check: func() {},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
cmd := newListTopicsCmd(out)
tt.args.rootCmd.SetArgs(tt.args.args)
tt.args.rootCmd.AddCommand(cmd)

err := tt.args.rootCmd.Execute()
if (err != nil) != tt.wantErr {
t.Errorf("createTopic() error = %v, wantErr %v", err, tt.wantErr)
return
}
tt.check()
})
}
}
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cmd

import (
"fmt"
"github.com/mitchellh/colorstring"
"github.com/spf13/cobra"
"io"
"os"

"github.com/mitchellh/colorstring"
"github.com/spf13/cobra"
)

const projectFlagName = "project"
Expand Down Expand Up @@ -48,6 +49,7 @@ func newRootCmd(out io.Writer) *cobra.Command {
newCreateSubscriptionCmd(out),
newRegisterPushCmd(out),
newConnectCmd(out),
newListTopicsCmd(out),
)
return rootCmd
}

0 comments on commit 2ba3e8a

Please sign in to comment.