-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from anhtranbk/feat/add-cmd-list-all-topics
feat: add new command list all topics
- Loading branch information
Showing
3 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters