-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathimports.go
More file actions
168 lines (137 loc) · 4.42 KB
/
imports.go
File metadata and controls
168 lines (137 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package imports
import (
"path/filepath"
"time"
stream "github.com/GetStream/stream-chat-go/v8"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/GetStream/stream-cli/pkg/config"
"github.com/GetStream/stream-cli/pkg/utils"
)
func NewCmds() []*cobra.Command {
return []*cobra.Command{
uploadCmd(),
getCmd(),
listCmd(),
}
}
func uploadCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upload-import [filename] --mode [upsert|insert] --output-format [json|tree]",
Short: "Upload an import",
Example: heredoc.Doc(`
# Uploads an import and prints it as JSON
$ stream-cli import chat upload-import data.json --mode insert
# Uploads an import and prints it as a browsable tree
$ stream-cli import chat upload-import data.json --mode insert --output-format tree
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
filename := args[0]
mode := stream.UpsertMode
if m, _ := cmd.Flags().GetString("mode"); stream.ImportMode(m) == stream.InsertMode {
mode = stream.InsertMode
}
var opts []stream.CreateImportOption
if cmd.Flags().Changed("merge-custom") {
mergeCustom, _ := cmd.Flags().GetBool("merge-custom")
opts = append(opts, stream.WithMergeCustom(mergeCustom))
}
createImportURLResp, err := c.CreateImportURL(cmd.Context(), filepath.Base(filename))
if err != nil {
return err
}
if err := utils.UploadToS3(cmd.Context(), filename, createImportURLResp.UploadURL); err != nil {
return err
}
createImportResp, err := c.CreateImport(cmd.Context(), createImportURLResp.Path, mode, opts...)
if err != nil {
return err
}
return utils.PrintObject(cmd, createImportResp.ImportTask)
},
}
fl := cmd.Flags()
fl.StringP("mode", "m", "upsert", "[optional] Import mode. Can be upsert or insert")
fl.Bool("merge-custom", false, "[optional] Merge custom data during import")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
fl.Bool("lighter-validation-id", false, "[optional] allows to pass ! in channel ID")
return cmd
}
func getCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-import [import-id] --output-format [json|tree] --watch",
Short: "Get import",
Example: heredoc.Doc(`
# Returns an import and prints it as JSON
$ stream-cli import chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272
# Returns an import and prints it as JSON, and wait for it to complete
$ stream-cli import chat get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
id := args[0]
watch, _ := cmd.Flags().GetBool("watch")
for {
resp, err := c.GetImport(cmd.Context(), id)
if err != nil {
return err
}
err = utils.PrintObject(cmd, resp.ImportTask)
if err != nil {
return err
}
if !watch {
break
}
time.Sleep(5 * time.Second)
}
return nil
},
}
fl := cmd.Flags()
fl.BoolP("watch", "w", false, "[optional] Keep polling the import to track its status")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
return cmd
}
func listCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-imports --offset [int] --limit [int] --output-format [json|tree]",
Short: "List imports",
Example: heredoc.Doc(`
# List all imports as json (default)
$ stream-cli import chat list-imports
# List all imports as browsable tree
$ stream-cli import chat list-imports --output-format tree
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
limit, _ := cmd.Flags().GetInt("limit")
offset, _ := cmd.Flags().GetInt("offset")
resp, err := c.ListImports(cmd.Context(), &stream.ListImportsOptions{
Limit: limit,
Offset: offset,
})
if err != nil {
return err
}
return utils.PrintObject(cmd, resp.ImportTasks)
},
}
fl := cmd.Flags()
fl.IntP("limit", "l", 10, "[optional] The number of imports returned")
fl.IntP("offset", "O", 0, "[optional] The starting offset of imports returned")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
return cmd
}