diff --git a/internal/cmd/calendar_raw.go b/internal/cmd/calendar_raw.go index 7679a83..e227939 100644 --- a/internal/cmd/calendar_raw.go +++ b/internal/cmd/calendar_raw.go @@ -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) } diff --git a/internal/cmd/docs.go b/internal/cmd/docs.go index 1309e04..a15881d 100644 --- a/internal/cmd/docs.go +++ b/internal/cmd/docs.go @@ -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 { diff --git a/internal/cmd/drive.go b/internal/cmd/drive.go index 18a857e..5c63b7b 100644 --- a/internal/cmd/drive.go +++ b/internal/cmd/drive.go @@ -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 { diff --git a/internal/cmd/forms_raw.go b/internal/cmd/forms_raw.go index ef78cbf..2b2f308 100644 --- a/internal/cmd/forms_raw.go +++ b/internal/cmd/forms_raw.go @@ -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) } diff --git a/internal/cmd/gmail_raw.go b/internal/cmd/gmail_raw.go index 7bd8ae4..111454d 100644 --- a/internal/cmd/gmail_raw.go +++ b/internal/cmd/gmail_raw.go @@ -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) } diff --git a/internal/cmd/people_raw.go b/internal/cmd/people_raw.go index 97a0edb..09ff7d0 100644 --- a/internal/cmd/people_raw.go +++ b/internal/cmd/people_raw.go @@ -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) } diff --git a/internal/cmd/raw_helpers.go b/internal/cmd/raw_helpers.go new file mode 100644 index 0000000..749fed6 --- /dev/null +++ b/internal/cmd/raw_helpers.go @@ -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}) +} diff --git a/internal/cmd/sheets.go b/internal/cmd/sheets.go index ae4e1eb..e374688 100644 --- a/internal/cmd/sheets.go +++ b/internal/cmd/sheets.go @@ -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 { diff --git a/internal/cmd/slides.go b/internal/cmd/slides.go index 4e8dd9f..0673103 100644 --- a/internal/cmd/slides.go +++ b/internal/cmd/slides.go @@ -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 { diff --git a/internal/cmd/tasks_raw.go b/internal/cmd/tasks_raw.go index f14ae02..2e0383d 100644 --- a/internal/cmd/tasks_raw.go +++ b/internal/cmd/tasks_raw.go @@ -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) }