refactor(cmd): share raw response helpers

This commit is contained in:
Peter Steinberger 2026-05-05 08:30:06 +01:00
parent 4e61efe0b8
commit 20afed7f4b
No known key found for this signature in database
10 changed files with 56 additions and 48 deletions

View File

@ -2,10 +2,6 @@ package cmd
import (
"context"
"errors"
"os"
"github.com/steipete/gogcli/internal/outfmt"
)
// CalendarRawCmd dumps the full Events.Get response as JSON, using the
@ -44,9 +40,10 @@ func (c *CalendarRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if event == nil {
return errors.New("event not found")
event, err = requireRawResponse(event, "event not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, event, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, event, c.Pretty)
}

View File

@ -71,11 +71,12 @@ func (c *DocsRawCmd) Run(ctx context.Context, flags *RootFlags) error {
}
return err
}
if doc == nil {
return errors.New("doc not found")
doc, err = requireRawResponse(doc, "doc not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, doc, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, doc, c.Pretty)
}
type DocsExportCmd struct {

View File

@ -149,8 +149,9 @@ func (c *DriveRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if f == nil {
return errors.New("file not found")
f, err = requireRawResponse(f, "file not found")
if err != nil {
return err
}
// Round-trip through JSON so we can redact by key when needed.
@ -176,7 +177,7 @@ func (c *DriveRawCmd) Run(ctx context.Context, flags *RootFlags) error {
}
}
return outfmt.WriteRaw(ctx, os.Stdout, m, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, m, c.Pretty)
}
type DriveLsCmd struct {

View File

@ -2,11 +2,7 @@ package cmd
import (
"context"
"errors"
"os"
"strings"
"github.com/steipete/gogcli/internal/outfmt"
)
// FormsRawCmd dumps the full Forms.Get response as JSON.
@ -37,9 +33,10 @@ func (c *FormsRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if form == nil {
return errors.New("form not found")
form, err = requireRawResponse(form, "form not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, form, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, form, c.Pretty)
}

View File

@ -2,12 +2,8 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/steipete/gogcli/internal/outfmt"
)
// GmailRawCmd dumps the full Users.Messages.Get response as JSON. Note the
@ -56,9 +52,10 @@ func (c *GmailRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if msg == nil {
return errors.New("message not found")
msg, err = requireRawResponse(msg, "message not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, msg, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, msg, c.Pretty)
}

View File

@ -2,11 +2,7 @@ package cmd
import (
"context"
"errors"
"os"
"strings"
"github.com/steipete/gogcli/internal/outfmt"
)
// defaultPeopleRawMask is the field mask used when the user does not
@ -68,9 +64,10 @@ func runPeopleRaw(ctx context.Context, flags *RootFlags, id, fields string, pret
if err != nil {
return wrapPeopleAPIError(err)
}
if person == nil {
return errors.New("person not found")
person, err = requireRawResponse(person, "person not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, person, outfmt.RawOptions{Pretty: pretty})
return writeRawJSON(ctx, person, pretty)
}

View File

@ -0,0 +1,20 @@
package cmd
import (
"context"
"errors"
"os"
"github.com/steipete/gogcli/internal/outfmt"
)
func requireRawResponse[T any](response *T, notFoundMessage string) (*T, error) {
if response == nil {
return nil, errors.New(notFoundMessage)
}
return response, nil
}
func writeRawJSON(ctx context.Context, value any, pretty bool) error {
return outfmt.WriteRaw(ctx, os.Stdout, value, outfmt.RawOptions{Pretty: pretty})
}

View File

@ -3,7 +3,6 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
@ -458,15 +457,16 @@ func (c *SheetsRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if resp == nil {
return errors.New("spreadsheet not found")
resp, err = requireRawResponse(resp, "spreadsheet not found")
if err != nil {
return err
}
if len(resp.DeveloperMetadata) > 0 {
u.Err().Println("warning: response contains developerMetadata which may hold third-party app secrets")
}
return outfmt.WriteRaw(ctx, os.Stdout, resp, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, resp, c.Pretty)
}
type SheetsMetadataCmd struct {

View File

@ -69,11 +69,12 @@ func (c *SlidesRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if pres == nil {
return errors.New("presentation not found")
pres, err = requireRawResponse(pres, "presentation not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, pres, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, pres, c.Pretty)
}
type SlidesExportCmd struct {

View File

@ -2,11 +2,7 @@ package cmd
import (
"context"
"errors"
"os"
"strings"
"github.com/steipete/gogcli/internal/outfmt"
)
// TasksRawCmd dumps the full Tasks.Get response as JSON.
@ -46,9 +42,10 @@ func (c *TasksRawCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if task == nil {
return errors.New("task not found")
task, err = requireRawResponse(task, "task not found")
if err != nil {
return err
}
return outfmt.WriteRaw(ctx, os.Stdout, task, outfmt.RawOptions{Pretty: c.Pretty})
return writeRawJSON(ctx, task, c.Pretty)
}