-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
3 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,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 | ||
} |
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,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) | ||
} |
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
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
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