(ctx context.Context, kctx *kong.Context, flags *RootFlags)
| 253 | } |
| 254 | |
| 255 | func (c *DocsFootnoteCmd) Run(ctx context.Context, kctx *kong.Context, flags *RootFlags) error { |
| 256 | docID := normalizeGoogleID(strings.TrimSpace(c.DocID)) |
| 257 | if docID == "" { |
| 258 | return usage("empty docId") |
| 259 | } |
| 260 | text, provided, err := resolveTextInput(ctx, c.Text, c.File, kctx) |
| 261 | if err != nil { |
| 262 | return err |
| 263 | } |
| 264 | if !provided || text == "" { |
| 265 | return usage("required: non-empty --text or --file") |
| 266 | } |
| 267 | placement, err := c.Placement.plan(kctx) |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | payload := c.Placement.dryRunPayload(placement) |
| 272 | payload["documentId"] = docID |
| 273 | payload["textBytes"] = len(text) |
| 274 | if dryRunErr := dryRunExit(ctx, flags, "docs.insert-footnote", payload); dryRunErr != nil { |
| 275 | return dryRunErr |
| 276 | } |
| 277 | svc, err := requireDocsService(ctx, flags) |
| 278 | if err != nil { |
| 279 | return err |
| 280 | } |
| 281 | resolved, err := resolveDocsPlacement(ctx, svc, docID, c.Placement.Tab, placement) |
| 282 | if err != nil { |
| 283 | return err |
| 284 | } |
| 285 | if resolved.InTable { |
| 286 | return usage("docs.insert-footnote cannot target text inside a table") |
| 287 | } |
| 288 | createResponse, err := svc.Documents.BatchUpdate(docID, &docs.BatchUpdateDocumentRequest{ |
| 289 | Requests: []*docs.Request{{CreateFootnote: &docs.CreateFootnoteRequest{ |
| 290 | Location: &docs.Location{Index: resolved.Index, TabId: resolved.TabID}, |
| 291 | }}}, |
| 292 | WriteControl: docsRequiredRevisionWriteControl(resolved.RequiredRevisionID), |
| 293 | }).Context(ctx).Do() |
| 294 | if err != nil { |
| 295 | return fmt.Errorf("create footnote: %w", err) |
| 296 | } |
| 297 | footnoteID := docsCreatedFootnoteID(createResponse) |
| 298 | if footnoteID == "" { |
| 299 | return fmt.Errorf("create footnote response missing footnote ID") |
| 300 | } |
| 301 | _, err = svc.Documents.BatchUpdate(docID, &docs.BatchUpdateDocumentRequest{Requests: []*docs.Request{{ |
| 302 | InsertText: &docs.InsertTextRequest{ |
| 303 | EndOfSegmentLocation: &docs.EndOfSegmentLocation{SegmentId: footnoteID, TabId: resolved.TabID}, |
| 304 | Text: text, |
| 305 | }, |
| 306 | }}}).Context(ctx).Do() |
| 307 | if err != nil { |
| 308 | return fmt.Errorf("footnote %s was created but could not be populated: %w", footnoteID, err) |
| 309 | } |
| 310 | result := map[string]any{ |
| 311 | "documentId": docID, "footnoteId": footnoteID, "segmentId": footnoteID, |
| 312 | "segmentType": docsSegmentKindFootnote, "atIndex": resolved.Index, "requests": 2, |
nothing calls this directly
no test coverage detected