Skip to content

Commit

Permalink
feat: add app settings crud
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Apr 27, 2022
1 parent fa5afe4 commit d9114d7
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 3 deletions.
96 changes: 96 additions & 0 deletions pkg/cmd/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package app

import (
"bytes"
"encoding/json"
"fmt"

stream "github.com/GetStream/stream-chat-go/v5"
"github.com/GetStream/stream-cli/pkg/config"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)

func NewCmds() []*cobra.Command {
return []*cobra.Command{getCmd(), updateCmd()}
}

func getCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-app --output-format [json]",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetStreamClient(cmd)
if err != nil {
return err
}

r, err := c.GetAppConfig(cmd.Context())
if err != nil {
return err
}

format, _ := cmd.Flags().GetString("output-format")

if format == "json" {
unindented, err := json.Marshal(r.App)
if err != nil {
return err
}

indented := bytes.Buffer{}
err = json.Indent(&indented, unindented, "", " ")
if err != nil {
return err
}

cmd.Println(indented.String())
} else {
return fmt.Errorf("unknown output format: %s", format)
}

return nil
},
}

fl := cmd.Flags()
fl.StringP("output-format", "o", "json", "Output format. Can be json or [see-in-next-pull-request]")

return cmd
}

func updateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-app --properties [raw-json-update-propertes]",
Example: heredoc.Doc(`
update-app --properties '{"multi_tenant_enabled": true, "permission_version": "v2"}'
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetStreamClient(cmd)
if err != nil {
return err
}

p, _ := cmd.Flags().GetString("properties")

s := &stream.AppSettings{}
err = json.Unmarshal([]byte(p), s)
if err != nil {
return err
}

_, err = c.UpdateAppSettings(cmd.Context(), s)
if err != nil {
return err
}

cmd.Println("Successfully updated app settings.")
return nil
},
}

fl := cmd.Flags()
fl.StringP("properties", "p", "", "Raw json properties to update")
cmd.MarkFlagRequired("properties")

return cmd
}
32 changes: 32 additions & 0 deletions pkg/cmd/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app

import (
"bytes"
"testing"

"github.com/GetStream/stream-cli/test"
"github.com/stretchr/testify/require"
)

func TestGetAppJsonFormat(t *testing.T) {
cmd := test.GetRootCmdWithSubCommands(NewCmds()...)
cmd.SetArgs([]string{"get-app", "--output-format", "json"})
_, err := cmd.ExecuteC()
require.NoError(t, err)
require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), "organization")
}

func TestGetAppUnknownFormat(t *testing.T) {
cmd := test.GetRootCmdWithSubCommands(NewCmds()...)
cmd.SetArgs([]string{"get-app", "--output-format", "unknown"})
_, err := cmd.ExecuteC()
require.Error(t, err)
require.NotContains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), "organization")
}

func TestUpdateApp(t *testing.T) {
cmd := test.GetRootCmdWithSubCommands(NewCmds()...)
cmd.SetArgs([]string{"update-app", "--properties", "{\"multi_tenant_enabled\":true}"})
_, err := cmd.ExecuteC()
require.NoError(t, err)
}
4 changes: 3 additions & 1 deletion pkg/cmd/chat/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chat

import (
"github.com/GetStream/stream-cli/pkg/cmd/app"
"github.com/GetStream/stream-cli/pkg/cmd/chat/channel"
"github.com/GetStream/stream-cli/pkg/cmd/chat/watch"
"github.com/spf13/cobra"
Expand All @@ -12,8 +13,9 @@ func NewRootCmd() *cobra.Command {
Short: "Interact with your Stream Chat application",
}

cmd.AddCommand(watch.NewCmds()...)
cmd.AddCommand(channel.NewCmds()...)
cmd.AddCommand(app.NewCmds()...)
cmd.AddCommand(watch.NewCmds()...)

return cmd
}
21 changes: 19 additions & 2 deletions pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"errors"
"fmt"
"net/url"
"text/tabwriter"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -133,8 +135,23 @@ func questions() []*survey.Question {
survey.MaxLength(75)),
},
{
Name: "ChatURL",
Prompt: &survey.Input{Message: "(optional) Which base URL do you want to use for Chat? Default value is our edge URL."},
Name: "ChatURL",
Prompt: &survey.Input{
Message: "(optional) Which base URL do you want to use for Chat?",
Default: cfg.DefaultChatEdgeURL,
},
Validate: func(ans interface{}) error {
u, ok := ans.(string)
if !ok {
return errors.New("invalid url")
}

_, err := url.ParseRequestURI(u)
if err != nil {
return errors.New("invalid url format. make sure it matches <scheme>://<host>")
}
return nil
},
Transform: func(ans interface{}) interface{} {
s, ok := ans.(string)

Expand Down
6 changes: 6 additions & 0 deletions scripts/test_all_commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ printf "\n\n #### Delete channel ####\n\n"

printf "\n\n #### List configs ####\n\n"
./stream-cli config list

printf "\n\n #### Update app settings ####\n\n"
./stream-cli chat update-app -p '{"multi_tenant_enabled":true}'

printf "\n\n #### Get app settings ####\n\n"
./stream-cli chat get-app

0 comments on commit d9114d7

Please sign in to comment.