Skip to content

Commit 90bd7e8

Browse files
re-use common code
1 parent 5708e9f commit 90bd7e8

8 files changed

Lines changed: 102 additions & 100 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
> - The command invocation is `stream-cli chat [verb-noun] [args] [options]` instead of `stream [verb:noun] [args] [options]`. The most obvious change is using dash instead of colon. We also added the `chat` keyword to preserve domain for our other product [Feeds](https://getstream.io/activity-feeds/).
1111
> - The 1.0.0 Go version's feature set is matching the old one. But if you miss anything, feel free to open an issue.
1212
13-
Stream's Command Line Interface (CLI) makes it easy to create and manage your [Stream](https://getstream.io) apps directly from the terminal. Currently, only Chat is supported; however, the ability to manage Feeds will be coming soon.
13+
Stream's Command Line Interface (CLI) makes it easy to create and manage your [Stream](https://getstream.io) apps directly from the terminal. It supports both Chat and Feeds, including a unified `import` command for data imports.
1414

1515
# 📚 Documentation
1616
The full documentation is deployed to [GitHub Pages](https://getstream.github.io/stream-cli/).
@@ -85,13 +85,19 @@ stream-cli version 1.0.0
8585
Basic commands use the following syntax:
8686

8787
```shell
88-
$ stream-cli [chat|feeds] [command] [args] [options]
88+
$ stream-cli [chat|import] [command] [args] [options]
8989
```
9090

91-
Example:
91+
Examples:
9292

9393
```shell
9494
$ stream-cli chat get-channel -t messaging -i redteam
95+
96+
# Import data into Chat
97+
$ stream-cli import chat upload-import data.json --mode insert
98+
99+
# Import data into Feeds
100+
$ stream-cli import feeds upload-import data.json
95101
```
96102

97103
The `--help` keyword is available every step of the way. Examples:
@@ -100,6 +106,9 @@ The `--help` keyword is available every step of the way. Examples:
100106
$ stream-cli --help
101107
$ stream-cli chat --help
102108
$ stream-cli chat get-channel --help
109+
$ stream-cli import --help
110+
$ stream-cli import chat --help
111+
$ stream-cli import feeds --help
103112
```
104113

105114
# 💬 Auto completion

pkg/cmd/chat/imports/imports.go

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package imports
22

33
import (
4-
"context"
5-
"net/http"
6-
"os"
74
"path/filepath"
85
"time"
96

@@ -23,44 +20,16 @@ func NewCmds() []*cobra.Command {
2320
}
2421
}
2522

26-
func uploadToS3(ctx context.Context, filename, url string) error {
27-
data, err := os.Open(filename)
28-
if err != nil {
29-
return err
30-
}
31-
defer data.Close()
32-
33-
stat, err := data.Stat()
34-
if err != nil {
35-
return err
36-
}
37-
38-
req, err := http.NewRequestWithContext(ctx, "PUT", url, data)
39-
if err != nil {
40-
return err
41-
}
42-
req.Header.Set("Content-Type", "application/json")
43-
req.ContentLength = stat.Size()
44-
45-
resp, err := http.DefaultClient.Do(req)
46-
if err != nil {
47-
return err
48-
}
49-
defer resp.Body.Close()
50-
51-
return nil
52-
}
53-
5423
func uploadCmd() *cobra.Command {
5524
cmd := &cobra.Command{
5625
Use: "upload-import [filename] --mode [upsert|insert] --output-format [json|tree]",
5726
Short: "Upload an import",
5827
Example: heredoc.Doc(`
5928
# Uploads an import and prints it as JSON
60-
$ stream-cli chat upload-import data.json --mode insert
29+
$ stream-cli import chat upload-import data.json --mode insert
6130
6231
# Uploads an import and prints it as a browsable tree
63-
$ stream-cli chat upload-import data.json --mode insert --output-format tree
32+
$ stream-cli import chat upload-import data.json --mode insert --output-format tree
6433
`),
6534
Args: cobra.ExactArgs(1),
6635
RunE: func(cmd *cobra.Command, args []string) error {
@@ -87,7 +56,7 @@ func uploadCmd() *cobra.Command {
8756
return err
8857
}
8958

90-
if err := uploadToS3(cmd.Context(), filename, createImportURLResp.UploadURL); err != nil {
59+
if err := utils.UploadToS3(cmd.Context(), filename, createImportURLResp.UploadURL); err != nil {
9160
return err
9261
}
9362
createImportResp, err := c.CreateImport(cmd.Context(), createImportURLResp.Path, mode, opts...)
@@ -114,10 +83,10 @@ func getCmd() *cobra.Command {
11483
Short: "Get import",
11584
Example: heredoc.Doc(`
11685
# Returns an import and prints it as JSON
117-
$ stream-cli chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272
86+
$ stream-cli import chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272
11887
11988
# Returns an import and prints it as JSON, and wait for it to complete
120-
$ stream-cli chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch
89+
$ stream-cli import chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch
12190
`),
12291
Args: cobra.ExactArgs(1),
12392
RunE: func(cmd *cobra.Command, args []string) error {
@@ -164,10 +133,10 @@ func listCmd() *cobra.Command {
164133
Short: "List imports",
165134
Example: heredoc.Doc(`
166135
# List all imports as json (default)
167-
$ stream-cli chat list-imports
136+
$ stream-cli import chat list-imports
168137
169138
# List all imports as browsable tree
170-
$ stream-cli chat list-imports --output-format tree
139+
$ stream-cli import chat list-imports --output-format tree
171140
`),
172141
RunE: func(cmd *cobra.Command, args []string) error {
173142
c, err := config.GetConfig(cmd).GetClient(cmd)

pkg/cmd/chat/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/GetStream/stream-cli/pkg/cmd/chat/device"
1010
"github.com/GetStream/stream-cli/pkg/cmd/chat/events"
1111
"github.com/GetStream/stream-cli/pkg/cmd/chat/file"
12-
"github.com/GetStream/stream-cli/pkg/cmd/chat/imports"
1312
"github.com/GetStream/stream-cli/pkg/cmd/chat/message"
1413
"github.com/GetStream/stream-cli/pkg/cmd/chat/push"
1514
"github.com/GetStream/stream-cli/pkg/cmd/chat/reaction"
@@ -29,7 +28,6 @@ func NewRootCmd() *cobra.Command {
2928
cmd.AddCommand(device.NewCmds()...)
3029
cmd.AddCommand(events.NewCmds()...)
3130
cmd.AddCommand(file.NewCmds()...)
32-
cmd.AddCommand(imports.NewCmds()...)
3331
cmd.AddCommand(message.NewCmds()...)
3432
cmd.AddCommand(user.NewCmds()...)
3533
cmd.AddCommand(push.NewCmds()...)

pkg/cmd/feeds/imports/imports.go

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10-
"os"
1110
"path/filepath"
1211
"strings"
1312
"time"
@@ -76,44 +75,16 @@ func getImportV2Task(ctx context.Context, app *config.App, id string) (map[strin
7675
return result, nil
7776
}
7877

79-
func uploadToS3(ctx context.Context, filename, url string) error {
80-
data, err := os.Open(filename)
81-
if err != nil {
82-
return err
83-
}
84-
defer data.Close()
85-
86-
stat, err := data.Stat()
87-
if err != nil {
88-
return err
89-
}
90-
91-
req, err := http.NewRequestWithContext(ctx, "PUT", url, data)
92-
if err != nil {
93-
return err
94-
}
95-
req.Header.Set("Content-Type", "application/json")
96-
req.ContentLength = stat.Size()
97-
98-
resp, err := http.DefaultClient.Do(req)
99-
if err != nil {
100-
return err
101-
}
102-
defer resp.Body.Close()
103-
104-
return nil
105-
}
106-
10778
func uploadCmd() *cobra.Command {
10879
cmd := &cobra.Command{
10980
Use: "upload-import [filename] --output-format [json|tree]",
11081
Short: "Upload an import for Feeds",
11182
Example: heredoc.Doc(`
11283
# Uploads a feeds import and prints it as JSON
113-
$ stream-cli feeds upload-import data.json
84+
$ stream-cli import feeds upload-import data.json
11485
11586
# Uploads a feeds import and prints it as a browsable tree
116-
$ stream-cli feeds upload-import data.json --output-format tree
87+
$ stream-cli import feeds upload-import data.json --output-format tree
11788
`),
11889
Args: cobra.ExactArgs(1),
11990
RunE: func(cmd *cobra.Command, args []string) error {
@@ -130,7 +101,7 @@ func uploadCmd() *cobra.Command {
130101
if err != nil {
131102
return err
132103
}
133-
if err := uploadToS3(cmd.Context(), filename, createURLResp.Data.UploadUrl); err != nil {
104+
if err := utils.UploadToS3(cmd.Context(), filename, createURLResp.Data.UploadUrl); err != nil {
134105
return err
135106
}
136107

@@ -177,10 +148,10 @@ func getCmd() *cobra.Command {
177148
Short: "Get a feeds import task",
178149
Example: heredoc.Doc(`
179150
# Returns a feeds import and prints it as JSON
180-
$ stream-cli feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272
151+
$ stream-cli import feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272
181152
182153
# Returns a feeds import and watches for completion
183-
$ stream-cli feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch
154+
$ stream-cli import feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch
184155
`),
185156
Args: cobra.ExactArgs(1),
186157
RunE: func(cmd *cobra.Command, args []string) error {
@@ -228,13 +199,13 @@ func listCmd() *cobra.Command {
228199
Short: "List feeds import tasks",
229200
Example: heredoc.Doc(`
230201
# List all feeds imports as json (default)
231-
$ stream-cli feeds list-imports
202+
$ stream-cli import feeds list-imports
232203
233204
# List feeds imports filtered by state
234-
$ stream-cli feeds list-imports --state 2
205+
$ stream-cli import feeds list-imports --state 2
235206
236207
# List all feeds imports as browsable tree
237-
$ stream-cli feeds list-imports --output-format tree
208+
$ stream-cli import feeds list-imports --output-format tree
238209
`),
239210
RunE: func(cmd *cobra.Command, args []string) error {
240211
client, err := config.GetConfig(cmd).GetFeedsClient(cmd)

pkg/cmd/feeds/root.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/cmd/importcmd/root.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package importcmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
chatImports "github.com/GetStream/stream-cli/pkg/cmd/chat/imports"
7+
feedsImports "github.com/GetStream/stream-cli/pkg/cmd/feeds/imports"
8+
)
9+
10+
func NewRootCmd() *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "import",
13+
Short: "Import data into your Stream applications",
14+
}
15+
16+
chatCmd := &cobra.Command{
17+
Use: "chat",
18+
Short: "Import data into Chat",
19+
}
20+
chatCmd.AddCommand(chatImports.NewCmds()...)
21+
22+
feedsCmd := &cobra.Command{
23+
Use: "feeds",
24+
Short: "Import data into Feeds",
25+
}
26+
feedsCmd.AddCommand(feedsImports.NewCmds()...)
27+
28+
cmd.AddCommand(chatCmd, feedsCmd)
29+
30+
return cmd
31+
}

pkg/cmd/root/root.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/GetStream/stream-cli/pkg/cmd/chat"
1010
cfgCmd "github.com/GetStream/stream-cli/pkg/cmd/config"
11-
feedsCmd "github.com/GetStream/stream-cli/pkg/cmd/feeds"
11+
"github.com/GetStream/stream-cli/pkg/cmd/importcmd"
1212
"github.com/GetStream/stream-cli/pkg/config"
1313
"github.com/GetStream/stream-cli/pkg/version"
1414
)
@@ -29,6 +29,12 @@ func NewCmd() *cobra.Command {
2929
3030
# Create a new Chat user
3131
$ stream-cli chat upsert-user --properties "{\"id\":\"my-user-1\"}"
32+
33+
# Upload a chat import
34+
$ stream-cli import chat upload-import data.json --mode insert
35+
36+
# Upload a feeds import
37+
$ stream-cli import feeds upload-import data.json
3238
`),
3339
Version: version.FmtVersion(),
3440
}
@@ -40,7 +46,7 @@ func NewCmd() *cobra.Command {
4046
root.AddCommand(
4147
cfgCmd.NewRootCmd(),
4248
chat.NewRootCmd(),
43-
feedsCmd.NewRootCmd(),
49+
importcmd.NewRootCmd(),
4450
)
4551

4652
cobra.OnInitialize(config.GetInitConfig(root, cfgPath))

pkg/utils/upload.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
)
8+
9+
// UploadToS3 uploads a local file to the given S3 presigned URL via HTTP PUT.
10+
func UploadToS3(ctx context.Context, filename, url string) error {
11+
data, err := os.Open(filename)
12+
if err != nil {
13+
return err
14+
}
15+
defer data.Close()
16+
17+
stat, err := data.Stat()
18+
if err != nil {
19+
return err
20+
}
21+
22+
req, err := http.NewRequestWithContext(ctx, "PUT", url, data)
23+
if err != nil {
24+
return err
25+
}
26+
req.Header.Set("Content-Type", "application/json")
27+
req.ContentLength = stat.Size()
28+
29+
resp, err := http.DefaultClient.Do(req)
30+
if err != nil {
31+
return err
32+
}
33+
defer resp.Body.Close()
34+
35+
return nil
36+
}

0 commit comments

Comments
 (0)