Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions internal/cmd/upload/upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package upload

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -118,29 +119,29 @@ func newCmdFile() *cobra.Command {
return err
}
cm := cmdutil.AsMap(createResp)
uploadURL, _ := cm["url"].(string)
uploadKey, _ := cm["key"].(string)

if uploadURL == "" {
return fmt.Errorf("server did not return an upload URL")
if uploadKey == "" {
return fmt.Errorf("server did not return an upload key")
}

// Step 2: PUT to S3
req, err := http.NewRequest("PUT", uploadURL, f)
if err != nil {
return err
}
req.Header.Set("Content-Type", contentType)
req.ContentLength = stat.Size()

httpClient := &http.Client{}
resp, err := httpClient.Do(req)
// Step 2: PUT through the authenticated object upload endpoint.
resp, err := c.PutApiUploadsObjectWithBody(
api.Ctx(),
&openapi.PutApiUploadsObjectParams{Key: uploadKey},
contentType,
f,
func(_ context.Context, req *http.Request) error {
req.ContentLength = stat.Size()
return nil
},
)
if err != nil {
return err
}
_ = resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("S3 upload failed with status %d", resp.StatusCode)
return fmt.Errorf("object upload failed with status %d", resp.StatusCode)
}

// Step 3: Complete
Expand Down
18 changes: 17 additions & 1 deletion internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func ApplyJQ(data any, expr string) error {
if err != nil {
return fmt.Errorf("invalid --jq expression: %w", err)
}
iter := query.Run(data)
normalized, err := normalizeJQInput(data)
if err != nil {
return err
}
iter := query.Run(normalized)
for {
v, ok := iter.Next()
if !ok {
Expand All @@ -94,6 +98,18 @@ func ApplyJQ(data any, expr string) error {
return nil
}

func normalizeJQInput(data any) (any, error) {
raw, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("prepare --jq input: %w", err)
}
var normalized any
if err := json.Unmarshal(raw, &normalized); err != nil {
return nil, fmt.Errorf("prepare --jq input: %w", err)
}
return normalized, nil
}

// --- JSON output ---

func JSON(data any) error {
Expand Down
6 changes: 6 additions & 0 deletions internal/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func TestApplyJQErrors(t *testing.T) {
if err := ApplyJQ([]any{1, 2, 3}, ".[]"); err != nil {
t.Errorf("array iteration expr returned error: %v", err)
}
if err := ApplyJQ([]struct {
ID int `json:"id"`
Name string `json:"name"`
}{{ID: 1, Name: "one"}}, "length"); err != nil {
t.Errorf("typed struct slice expr returned error: %v", err)
}
if err := ApplyJQ(data, ".invalid-["); err == nil {
t.Error("invalid expr returned nil error")
}
Expand Down
Loading