|
| 1 | +package imports |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + getstream "github.com/GetStream/getstream-go/v4" |
| 16 | + "github.com/MakeNowJust/heredoc" |
| 17 | + "github.com/golang-jwt/jwt/v5" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + |
| 20 | + "github.com/GetStream/stream-cli/pkg/config" |
| 21 | + "github.com/GetStream/stream-cli/pkg/utils" |
| 22 | +) |
| 23 | + |
| 24 | +func NewCmds() []*cobra.Command { |
| 25 | + return []*cobra.Command{ |
| 26 | + uploadCmd(), |
| 27 | + getCmd(), |
| 28 | + listCmd(), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// getImportV2Task works around a server-side issue where GET /api/v2/imports/v2/{id} |
| 33 | +// requires a JSON body. The SDK sends no body for GET requests, causing a 400 error. |
| 34 | +func getImportV2Task(ctx context.Context, app *config.App, id string) (map[string]any, error) { |
| 35 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{"server": true}) |
| 36 | + authToken, err := token.SignedString([]byte(app.AccessSecretKey)) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("creating auth token: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + baseURL := app.ChatURL |
| 42 | + if baseURL == "" { |
| 43 | + baseURL = config.DefaultChatEdgeURL |
| 44 | + } |
| 45 | + |
| 46 | + reqURL := fmt.Sprintf("%s/api/v2/imports/v2/%s?api_key=%s", |
| 47 | + baseURL, url.PathEscape(id), url.QueryEscape(app.AccessKey)) |
| 48 | + |
| 49 | + req, err := http.NewRequestWithContext(ctx, "GET", reqURL, strings.NewReader("{}")) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + req.Header.Set("Content-Type", "application/json") |
| 54 | + req.Header.Set("Authorization", authToken) |
| 55 | + req.Header.Set("Stream-Auth-Type", "jwt") |
| 56 | + |
| 57 | + resp, err := http.DefaultClient.Do(req) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + defer resp.Body.Close() |
| 62 | + |
| 63 | + body, err := io.ReadAll(resp.Body) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + if resp.StatusCode >= 400 { |
| 69 | + return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body)) |
| 70 | + } |
| 71 | + |
| 72 | + var result map[string]any |
| 73 | + if err := json.Unmarshal(body, &result); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return result, nil |
| 77 | +} |
| 78 | + |
| 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 | + |
| 107 | +func uploadCmd() *cobra.Command { |
| 108 | + cmd := &cobra.Command{ |
| 109 | + Use: "upload-import [filename] --output-format [json|tree]", |
| 110 | + Short: "Upload an import for Feeds", |
| 111 | + Example: heredoc.Doc(` |
| 112 | + # Uploads a feeds import and prints it as JSON |
| 113 | + $ stream-cli feeds upload-import data.json |
| 114 | +
|
| 115 | + # Uploads a feeds import and prints it as a browsable tree |
| 116 | + $ stream-cli feeds upload-import data.json --output-format tree |
| 117 | + `), |
| 118 | + Args: cobra.ExactArgs(1), |
| 119 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 120 | + client, err := config.GetConfig(cmd).GetFeedsClient(cmd) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + filename := args[0] |
| 126 | + |
| 127 | + createURLResp, err := client.CreateImportURL(cmd.Context(), &getstream.CreateImportURLRequest{ |
| 128 | + Filename: getstream.PtrTo(filepath.Base(filename)), |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + if err := uploadToS3(cmd.Context(), filename, createURLResp.Data.UploadUrl); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + bucket, region, err := utils.S3BucketAndRegionFromUploadURL(createURLResp.Data.UploadUrl) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + dir := createURLResp.Data.Path |
| 142 | + |
| 143 | + skipReferencesCheck, err := cmd.Flags().GetBool("skip-references-check") |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + resp, err := client.CreateImportV2Task(cmd.Context(), &getstream.CreateImportV2TaskRequest{ |
| 149 | + Product: "feeds", |
| 150 | + Settings: getstream.ImportV2TaskSettings{ |
| 151 | + SkipReferencesCheck: getstream.PtrTo(skipReferencesCheck), |
| 152 | + S3: &getstream.ImportV2TaskSettingsS3{ |
| 153 | + Bucket: getstream.PtrTo(bucket), |
| 154 | + Dir: &dir, |
| 155 | + Region: getstream.PtrTo(region), |
| 156 | + }, |
| 157 | + }, |
| 158 | + }) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return utils.PrintObject(cmd, resp.Data) |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + fl := cmd.Flags() |
| 168 | + fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree") |
| 169 | + fl.Bool("skip-references-check", false, "[optional] Skip references validation for the import (default false)") |
| 170 | + |
| 171 | + return cmd |
| 172 | +} |
| 173 | + |
| 174 | +func getCmd() *cobra.Command { |
| 175 | + cmd := &cobra.Command{ |
| 176 | + Use: "get-import [task-id] --output-format [json|tree] --watch", |
| 177 | + Short: "Get a feeds import task", |
| 178 | + Example: heredoc.Doc(` |
| 179 | + # Returns a feeds import and prints it as JSON |
| 180 | + $ stream-cli feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 |
| 181 | +
|
| 182 | + # Returns a feeds import and watches for completion |
| 183 | + $ stream-cli feeds get-import dcb6e366-93ec-4e52-af6f-b0c030ad5272 --watch |
| 184 | + `), |
| 185 | + Args: cobra.ExactArgs(1), |
| 186 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 187 | + cfg := config.GetConfig(cmd) |
| 188 | + app, err := cfg.GetDefaultAppOrExplicit(cmd) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + id := args[0] |
| 194 | + watch, _ := cmd.Flags().GetBool("watch") |
| 195 | + |
| 196 | + for { |
| 197 | + result, err := getImportV2Task(cmd.Context(), app, id) |
| 198 | + if err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + |
| 202 | + err = utils.PrintObject(cmd, result) |
| 203 | + if err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + |
| 207 | + if !watch { |
| 208 | + break |
| 209 | + } |
| 210 | + |
| 211 | + time.Sleep(5 * time.Second) |
| 212 | + } |
| 213 | + |
| 214 | + return nil |
| 215 | + }, |
| 216 | + } |
| 217 | + |
| 218 | + fl := cmd.Flags() |
| 219 | + fl.BoolP("watch", "w", false, "[optional] Keep polling the import to track its status") |
| 220 | + fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree") |
| 221 | + |
| 222 | + return cmd |
| 223 | +} |
| 224 | + |
| 225 | +func listCmd() *cobra.Command { |
| 226 | + cmd := &cobra.Command{ |
| 227 | + Use: "list-imports --output-format [json|tree] --state [1-4]", |
| 228 | + Short: "List feeds import tasks", |
| 229 | + Example: heredoc.Doc(` |
| 230 | + # List all feeds imports as json (default) |
| 231 | + $ stream-cli feeds list-imports |
| 232 | +
|
| 233 | + # List feeds imports filtered by state |
| 234 | + $ stream-cli feeds list-imports --state 2 |
| 235 | +
|
| 236 | + # List all feeds imports as browsable tree |
| 237 | + $ stream-cli feeds list-imports --output-format tree |
| 238 | + `), |
| 239 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 240 | + client, err := config.GetConfig(cmd).GetFeedsClient(cmd) |
| 241 | + if err != nil { |
| 242 | + return err |
| 243 | + } |
| 244 | + |
| 245 | + state, _ := cmd.Flags().GetInt("state") |
| 246 | + |
| 247 | + req := &getstream.ListImportV2TasksRequest{} |
| 248 | + if state > 0 { |
| 249 | + req.State = getstream.PtrTo(state) |
| 250 | + } |
| 251 | + |
| 252 | + resp, err := client.ListImportV2Tasks(cmd.Context(), req) |
| 253 | + if err != nil { |
| 254 | + return err |
| 255 | + } |
| 256 | + |
| 257 | + return utils.PrintObject(cmd, resp.Data) |
| 258 | + }, |
| 259 | + } |
| 260 | + |
| 261 | + fl := cmd.Flags() |
| 262 | + fl.IntP("state", "s", 0, "[optional] Filter imports by state (1-4)") |
| 263 | + fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree") |
| 264 | + |
| 265 | + return cmd |
| 266 | +} |
0 commit comments