From caed0af6edd1c2ca7ce433f82a3d56451221ad38 Mon Sep 17 00:00:00 2001 From: peterdeme Date: Thu, 3 Mar 2022 15:05:47 +0100 Subject: [PATCH] fix: fix per pr comments --- go.mod | 23 ++++++++++------------- pkg/cmd/chat/channel/channel.go | 22 +++++++++++----------- pkg/cmd/chat/channel/channel_test.go | 12 ++++++------ pkg/cmd/chat/root.go | 6 +++--- pkg/cmd/chat/watch/watch.go | 2 +- pkg/cmd/config/config.go | 19 +++++++++++++------ pkg/cmd/root/root.go | 6 +++--- pkg/config/config.go | 1 - 8 files changed, 47 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 9e3705ce..21154f52 100644 --- a/go.mod +++ b/go.mod @@ -13,30 +13,27 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/golang-jwt/jwt/v4 v4.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/magiconair/properties v1.8.5 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/subosito/gotenv v1.2.0 // indirect - gopkg.in/ini.v1 v1.66.2 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect -) - -require ( - github.com/golang-jwt/jwt/v4 v4.0.0 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect - github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.7.0 + github.com/subosito/gotenv v1.2.0 // indirect golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect golang.org/x/text v0.3.7 // indirect + gopkg.in/ini.v1 v1.66.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/pkg/cmd/chat/channel/channel.go b/pkg/cmd/chat/channel/channel.go index e358f625..4f938453 100644 --- a/pkg/cmd/chat/channel/channel.go +++ b/pkg/cmd/chat/channel/channel.go @@ -12,16 +12,16 @@ import ( "github.com/spf13/cobra" ) -func NewChannelCmds() []*cobra.Command { +func NewCmds() []*cobra.Command { return []*cobra.Command{ - getChannelCmd(), - createChannelCmd(), - deleteChannelCmd(), - updateChannelCmd(), - listChannelsCmd()} + getCmd(), + createCmd(), + deleteCmd(), + updateCmd(), + listCmd()} } -func getChannelCmd() *cobra.Command { +func getCmd() *cobra.Command { cmd := &cobra.Command{ Use: "get-channel --type [channel-type] --id [channel-id]", Short: "Returns a channel", @@ -64,7 +64,7 @@ func getChannelCmd() *cobra.Command { return cmd } -func createChannelCmd() *cobra.Command { +func createCmd() *cobra.Command { cmd := &cobra.Command{ Use: "create-channel --type [channel-type] --id [channel-id]", Short: "Creates a channel", @@ -104,7 +104,7 @@ func createChannelCmd() *cobra.Command { return cmd } -func deleteChannelCmd() *cobra.Command { +func deleteCmd() *cobra.Command { cmd := &cobra.Command{ Use: "delete-channel --type [channel-type] --id [channel-id]", Short: "Deletes a channel", @@ -143,7 +143,7 @@ func deleteChannelCmd() *cobra.Command { return cmd } -func updateChannelCmd() *cobra.Command { +func updateCmd() *cobra.Command { cmd := &cobra.Command{ Use: "update-channel --type [channel-type] --id [channel-id]", Short: "Updates a channel", @@ -183,7 +183,7 @@ func updateChannelCmd() *cobra.Command { return cmd } -func listChannelsCmd() *cobra.Command { +func listCmd() *cobra.Command { cmd := &cobra.Command{ Use: "list-channels --type [channel-type]", Short: "Lists channels", diff --git a/pkg/cmd/chat/channel/channel_test.go b/pkg/cmd/chat/channel/channel_test.go index aabb30ba..bab51cab 100644 --- a/pkg/cmd/chat/channel/channel_test.go +++ b/pkg/cmd/chat/channel/channel_test.go @@ -12,7 +12,7 @@ import ( ) func TestCreateChannel(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) ch := test.RandomString(10) t.Cleanup(func() { test.DeleteChannel(ch) @@ -30,7 +30,7 @@ func TestCreateChannel(t *testing.T) { } func TestCreateChannelAlreadyExists(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) ch := test.InitChannel(t) t.Cleanup(func() { test.DeleteChannel(ch) @@ -44,7 +44,7 @@ func TestCreateChannelAlreadyExists(t *testing.T) { } func TestGetChannel(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) ch := test.InitChannel(t) t.Cleanup(func() { test.DeleteChannel(ch) @@ -57,7 +57,7 @@ func TestGetChannel(t *testing.T) { } func TestDeleteChannel(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) ch := test.InitChannel(t) cmd.SetArgs([]string{"delete-channel", "-t", "messaging", "-i", ch, "--hard"}) _, err := cmd.ExecuteC() @@ -70,7 +70,7 @@ func TestDeleteChannel(t *testing.T) { } func TestUpdateChannel(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) ch := test.InitChannel(t) t.Cleanup(func() { test.DeleteChannel(ch) @@ -88,7 +88,7 @@ func TestUpdateChannel(t *testing.T) { } func TestListChannel(t *testing.T) { - cmd := test.GetRootCmdWithSubCommands(NewChannelCmds()...) + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) cmd.SetArgs([]string{"list-channels", "-t", "messaging", "-l", "1"}) _, err := cmd.ExecuteC() require.NoError(t, err) diff --git a/pkg/cmd/chat/root.go b/pkg/cmd/chat/root.go index fea6f3fe..2c9819f1 100644 --- a/pkg/cmd/chat/root.go +++ b/pkg/cmd/chat/root.go @@ -6,14 +6,14 @@ import ( "github.com/spf13/cobra" ) -func NewChatRootCmd() *cobra.Command { +func NewRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "chat", Short: "Interact with your Stream Chat application", } - cmd.AddCommand(watch.NewWatchCmd()) - cmd.AddCommand(channel.NewChannelCmds()...) + cmd.AddCommand(watch.NewCmd()) + cmd.AddCommand(channel.NewCmds()...) return cmd } diff --git a/pkg/cmd/chat/watch/watch.go b/pkg/cmd/chat/watch/watch.go index de4a2999..a6c5d2bf 100644 --- a/pkg/cmd/chat/watch/watch.go +++ b/pkg/cmd/chat/watch/watch.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -func NewWatchCmd() *cobra.Command { +func NewCmd() *cobra.Command { cmd := &cobra.Command{ Use: "watch [task-id]", Short: "Waits for an async task to complete", diff --git a/pkg/cmd/config/config.go b/pkg/cmd/config/config.go index d3af3c1f..7a7b3877 100644 --- a/pkg/cmd/config/config.go +++ b/pkg/cmd/config/config.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func NewRootConfigCmd() *cobra.Command { +func NewRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "config", Short: "Manage app configurations", @@ -26,6 +26,7 @@ func newAppCmd() *cobra.Command { Use: "new", Short: "Add a new application", Long: "Add a new application which can be used for further operations", + RunE: func(cmd *cobra.Command, args []string) error { return runQuestionnaire(cmd) }, @@ -34,13 +35,19 @@ func newAppCmd() *cobra.Command { func removeAppCmd() *cobra.Command { return &cobra.Command{ - Use: "remove [app-name]", - Short: "Remove an application. This operation is irrevocable", - Args: cobra.ExactArgs(1), + Use: "remove [app-name-1] [app-name-2] ...", + Short: "Remove 1 or more application. This operation is irrevocable", + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { config := cfg.GetConfig(cmd) - return config.Remove(args[0]) + for _, appName := range args { + if err := config.Remove(appName); err != nil { + return err + } + } + + return nil }, } } @@ -53,7 +60,7 @@ func listAppsCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0) t := tabby.NewCustom(w) - t.AddHeader("", "Name", "Access Key", "Secret Key", "Url") + t.AddHeader("", "Name", "Access Key", "Secret Key", "URL") config := cfg.GetConfig(cmd) diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index a6da9753..4a153cbd 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -21,12 +21,12 @@ func NewRootCmd() *cobra.Command { } fl := root.PersistentFlags() - fl.String("app", "", "[optional] Application id to use as it's defined in the configuration file") + fl.String("app", "", "[optional] Application name to use as it's defined in the configuration file") fl.StringVar(cfgPath, "config", "", "[optional] Explicit config file path") root.AddCommand( - cfgCmd.NewRootConfigCmd(), - chat.NewChatRootCmd(), + cfgCmd.NewRootCmd(), + chat.NewRootCmd(), ) cobra.OnInitialize(config.GetInitConfig(root, cfgPath)) diff --git a/pkg/config/config.go b/pkg/config/config.go index d38a7bf2..29e38c92 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -175,7 +175,6 @@ func GetInitConfig(cmd *cobra.Command, cfgPath *string) func() { } viper.SetConfigFile(configPath) - viper.SetConfigType("yaml") err := viper.ReadInConfig() if err != nil && os.IsNotExist(err) {