Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.48.1 - Unreleased

- Drive/Docs: return `[]` instead of `null` from `drive labels list`, `drive labels file list`, `drive comments list` and `docs comments list` with `--json` when there is nothing to list, including with `--results-only`. Google omits empty list fields, so these printed `null`. (#1326) — thanks @saariuslystoned.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/comment_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ type driveCommentListOptions struct {
}

func listDriveComments(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) {
comments, nextPageToken, err := listDriveCommentPages(ctx, svc, fileID, opts)
if err != nil {
return nil, "", err
}
return nonNilSlice(comments), nextPageToken, nil
}

func listDriveCommentPages(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) {
fetch := func(pageToken string) ([]*drive.Comment, string, error) {
return fetchDriveCommentsPage(ctx, svc, fileID, opts.max, pageToken, opts.since, driveCommentFieldsForList(opts))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/drive_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *DriveLabelsFileListCmd) Run(ctx context.Context, flags *RootFlags) erro
}
if outfmt.IsJSON(ctx) {
return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{
"labels": resp.Labels,
"labels": nonNilSlice(resp.Labels),
"labelCount": len(resp.Labels),
"nextPageToken": resp.NextPageToken,
})
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *DriveLabelsListCmd) Run(ctx context.Context, flags *RootFlags) error {

if outfmt.IsJSON(ctx) {
return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{
"labels": resp.Labels,
"labels": nonNilSlice(resp.Labels),
"labelCount": len(resp.Labels),
"nextPageToken": resp.NextPageToken,
})
Expand Down
113 changes: 113 additions & 0 deletions internal/cmd/empty_list_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cmd

import (
"encoding/json"
"net/http"
"strings"
"testing"

"google.golang.org/api/drivelabels/v2"

"github.com/openclaw/gogcli/internal/app"
)

// Google omits empty list fields, so each fake returns {}.
func TestEmptyListJSON(t *testing.T) {
t.Run("drive labels list", func(t *testing.T) {
svc, closeSvc := newGoogleTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/v2/labels" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{})
}), drivelabels.NewService)
defer closeSvc()

assertEmptyListJSON(t, "labels", func(args []string) executeTestResult {
return executeWithTestRuntime(t, args, &app.Runtime{Services: app.Services{
DriveLabels: fixedDriveLabelsTestService(svc),
}})
}, []string{"--account", "a@example.com", "drive", "labels", "list"})
})

t.Run("drive labels file list", func(t *testing.T) {
svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/files/file1/listLabels" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{})
}))
defer closeSvc()

assertEmptyListJSON(t, "labels", func(args []string) executeTestResult {
return executeWithDriveTestService(t, args, svc)
}, []string{"--account", "a@example.com", "drive", "labels", "file", "list", "file1"})
})

t.Run("docs comments list", func(t *testing.T) {
svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/drive/v3")
if r.Method != http.MethodGet || path != "/files/empty/comments" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{})
}))
defer closeSvc()

assertEmptyListJSON(t, "comments", func(args []string) executeTestResult {
return executeWithDriveTestService(t, args, svc)
}, []string{"--account", "a@b.com", "docs", "comments", "list", "empty"})
})

t.Run("drive comments list", func(t *testing.T) {
svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/drive/v3")
if r.Method != http.MethodGet || path != "/files/empty/comments" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{})
}))
defer closeSvc()

assertEmptyListJSON(t, "comments", func(args []string) executeTestResult {
return executeWithDriveTestService(t, args, svc)
}, []string{"--account", "a@b.com", "drive", "comments", "list", "empty"})
})
}

func assertEmptyListJSON(t *testing.T, listKey string, run func([]string) executeTestResult, args []string) {
t.Helper()

jsonArgs := append([]string{"--json"}, args...)
result := run(jsonArgs)
if result.err != nil {
t.Fatalf("Execute: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout)
}
var parsed map[string]json.RawMessage
if err := json.Unmarshal([]byte(result.stdout), &parsed); err != nil {
t.Fatalf("json parse: %v\n%s", err, result.stdout)
}
raw, ok := parsed[listKey]
if !ok {
t.Fatalf("missing %s key: %s", listKey, result.stdout)
}
if strings.TrimSpace(string(raw)) != "[]" {
t.Fatalf("%s = %s, want []\nstdout=%s", listKey, raw, result.stdout)
}

onlyArgs := append([]string{"--json", "--results-only"}, args...)
result = run(onlyArgs)
if result.err != nil {
t.Fatalf("Execute --results-only: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout)
}
if got := strings.TrimSpace(result.stdout); got != "[]" {
t.Fatalf("--results-only = %q, want []", result.stdout)
}
}
9 changes: 9 additions & 0 deletions internal/cmd/output_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ func escapeTerminalControls(value string) string {
}
return output.String()
}

// nonNilSlice returns s, or an empty slice when s is nil. Google omits empty
// list fields, and JSON output should still encode an empty list as [].
func nonNilSlice[T any](s []T) []T {
if s == nil {
return []T{}
}
return s
}