Skip to content

Commit

Permalink
feat: add device crud
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Apr 27, 2022
1 parent 9b6a67b commit 045f3c8
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.2.15
github.com/GetStream/stream-chat-go/v5 v5.6.0
github.com/GetStream/stream-chat-go/v5 v5.7.0
github.com/MakeNowJust/heredoc v1.0.0
github.com/cheynewallace/tabby v1.1.1
github.com/gizak/termui/v3 v3.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/AlecAivazis/survey/v2 v2.2.15/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZt
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/GetStream/stream-chat-go/v5 v5.6.0 h1:EUe3/zhWCdEpd83aNxGj8IzEE5VJRf15dIsCvujoG3A=
github.com/GetStream/stream-chat-go/v5 v5.6.0/go.mod h1:ET7NyUYplNy8+tyliin6Q3kKwbd/+FHQWMAW6zucisY=
github.com/GetStream/stream-chat-go/v5 v5.7.0 h1:KCsxAF8UqDDRDPQbIh6SUOpW20klERPEE4gKpPFkRME=
github.com/GetStream/stream-chat-go/v5 v5.7.0/go.mod h1:ET7NyUYplNy8+tyliin6Q3kKwbd/+FHQWMAW6zucisY=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
Expand Down
137 changes: 137 additions & 0 deletions pkg/cmd/chat/device/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package device

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

func NewCmds() []*cobra.Command {
return []*cobra.Command{
createCmd(),
listCmd(),
deleteCmd()}
}

func createCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-device --id [device-id] --push-provider [firebase|apn|xiaomi|huawei] --push-provider-name [provider-name] --user-id [user-id]",
Short: "Create a device",
Long: heredoc.Doc(`
Registering a device associates it with a user and tells
the push provider to send new message notifications to the device.
`),
Example: heredoc.Doc(`
# Create a device with a firebase push provider
$ stream-cli chat create-device --id "my-device-id" --push-provider firebase --push-provider-name "my-firebase-project-id" --user-id "my-user-id"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

id, _ := cmd.Flags().GetString("id")
provider, _ := cmd.Flags().GetString("push-provider")
providerName, _ := cmd.Flags().GetString("push-provider-name")
userID, _ := cmd.Flags().GetString("user-id")

d := &stream_chat.Device{ID: id, PushProvider: provider, PushProviderName: providerName, UserID: userID}

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

cmd.Println("Successfully created device")
return nil
},
}

fl := cmd.Flags()
fl.StringP("id", "i", "", "[required] Device ID")
fl.StringP("push-provider", "p", "", "[required] Push provider. Can be apn, firebase, xiaomi, huawei")
fl.StringP("push-provider-name", "n", "", "[optional] Push provider name")
fl.StringP("user-id", "u", "", "[required] User ID")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("push-provider")
_ = cmd.MarkFlagRequired("user-id")

return cmd
}

func listCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-devices --user-id [user-id] --output-format [json|tree]",
Short: "List devices",
Long: "Provides a list of all devices associated with a user.",
Example: heredoc.Doc(`
# List devices for a user
$ stream-cli chat list-devices --user-id "my-user-id"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

userID, _ := cmd.Flags().GetString("user-id")

devices, err := c.GetDevices(cmd.Context(), userID)
if err != nil {
return err
}

return utils.PrintObject(cmd, devices.Devices)
},
}

fl := cmd.Flags()
fl.StringP("user-id", "u", "", "[required] User ID")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
_ = cmd.MarkFlagRequired("user-id")

return cmd
}

func deleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-device --id [device-id] --user-id [user-id]",
Short: "Delete a device",
Long: heredoc.Doc(`
Unregistering a device removes the device from the user
and stops further new message notifications.
`),
Example: heredoc.Doc(`
# Delete "my-device-id" device
$ stream-cli chat delete-device --id "my-device-id" --user-id "my-user-id"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

id, _ := cmd.Flags().GetString("id")
userID, _ := cmd.Flags().GetString("user-id")

_, err = c.DeleteDevice(cmd.Context(), userID, id)
if err != nil {
return err
}

cmd.Println("Successfully deleted device")
return nil
},
}

fl := cmd.Flags()
fl.StringP("id", "i", "", "[required] Device ID to delete")
fl.StringP("user-id", "u", "", "[required] ID of the user who deletes the device")
_ = cmd.MarkFlagRequired("id")
_ = cmd.MarkFlagRequired("user-id")

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

import (
"bytes"
"testing"

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

func TestDevice(t *testing.T) {
cmd := test.GetRootCmdWithSubCommands(NewCmds()...)
userID := test.CreateUser()
deviceID := test.RandomString(10)
t.Cleanup(func() {
test.DeleteUser(userID)
})

cmd.SetArgs([]string{"create-device", "-i", deviceID, "-p", "apn", "-u", userID})
_, err := cmd.ExecuteC()
require.NoError(t, err)

cmd.SetArgs([]string{"list-devices", "-u", userID})
_, err = cmd.ExecuteC()
require.NoError(t, err)
require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), deviceID)

cmd.SetArgs([]string{"delete-device", "-i", deviceID, "-u", userID})
_, err = cmd.ExecuteC()
require.NoError(t, err)
require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), "Successfully deleted device")
}
2 changes: 1 addition & 1 deletion pkg/cmd/chat/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func getCmd() *cobra.Command {

func listCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-imports --offset [int] --limit [int]--output-format [json|tree]",
Use: "list-imports --offset [int] --limit [int] --output-format [json|tree]",
Short: "List imports",
Example: heredoc.Doc(`
# List all imports as json (default)
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/chat/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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/channeltype"
"github.com/GetStream/stream-cli/pkg/cmd/chat/device"
"github.com/GetStream/stream-cli/pkg/cmd/chat/file"
"github.com/GetStream/stream-cli/pkg/cmd/chat/imports"
"github.com/GetStream/stream-cli/pkg/cmd/chat/message"
Expand All @@ -21,6 +22,7 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(app.NewCmds()...)
cmd.AddCommand(channel.NewCmds()...)
cmd.AddCommand(channeltype.NewCmds()...)
cmd.AddCommand(device.NewCmds()...)
cmd.AddCommand(file.NewCmds()...)
cmd.AddCommand(imports.NewCmds()...)
cmd.AddCommand(message.NewCmds()...)
Expand Down

0 comments on commit 045f3c8

Please sign in to comment.