From 2e8323b3e45fa83ff34b627d2b2d870d8c5faf58 Mon Sep 17 00:00:00 2001 From: Tiankai Ma Date: Wed, 1 Jul 2026 21:06:16 +0800 Subject: [PATCH] fix upload auth and jq typed output --- internal/cmd/upload/upload.go | 29 +++++++++++++++-------------- internal/output/output.go | 18 +++++++++++++++++- internal/output/output_test.go | 6 ++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/internal/cmd/upload/upload.go b/internal/cmd/upload/upload.go index e2e7d3c..cda1697 100644 --- a/internal/cmd/upload/upload.go +++ b/internal/cmd/upload/upload.go @@ -1,6 +1,7 @@ package upload import ( + "context" "fmt" "io" "net/http" @@ -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 diff --git a/internal/output/output.go b/internal/output/output.go index 65bf39d..9037a38 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -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 { @@ -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 { diff --git a/internal/output/output_test.go b/internal/output/output_test.go index c2b2e99..9ad0d8d 100644 --- a/internal/output/output_test.go +++ b/internal/output/output_test.go @@ -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") }