Run executes the insert-text command.
(ctx context.Context, flags *RootFlags)
| 27 | |
| 28 | // Run executes the insert-text command. |
| 29 | func (c *SlidesInsertTextCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 30 | u := ui.FromContext(ctx) |
| 31 | |
| 32 | presentationID := strings.TrimSpace(c.PresentationID) |
| 33 | if presentationID == "" { |
| 34 | return usage("empty presentationId") |
| 35 | } |
| 36 | objectID := strings.TrimSpace(c.ObjectID) |
| 37 | if objectID == "" { |
| 38 | return usage("empty objectId") |
| 39 | } |
| 40 | |
| 41 | // Resolve text: '-' means read from stdin. |
| 42 | text := c.Text |
| 43 | if text == "-" { |
| 44 | data, err := io.ReadAll(commandIO(ctx).In) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("read text from stdin: %w", err) |
| 47 | } |
| 48 | text = string(data) |
| 49 | } |
| 50 | if text == "" && !c.Replace { |
| 51 | return usage("empty text") |
| 52 | } |
| 53 | if c.InsertionIndex < 0 { |
| 54 | return usage("insertion-index must be >= 0") |
| 55 | } |
| 56 | var cellLocation *slides.TableCellLocation |
| 57 | if c.Row != nil || c.Col != nil { |
| 58 | if c.Row == nil || c.Col == nil { |
| 59 | return usage("--row and --col must be provided together") |
| 60 | } |
| 61 | if *c.Row < 0 { |
| 62 | return usage("--row must be >= 0") |
| 63 | } |
| 64 | if *c.Col < 0 { |
| 65 | return usage("--col must be >= 0") |
| 66 | } |
| 67 | cellLocation = slidesTableCellLocation(*c.Row, *c.Col) |
| 68 | } |
| 69 | |
| 70 | // Build the batchUpdate request body. |
| 71 | var requests []*slides.Request |
| 72 | if c.Replace { |
| 73 | requests = buildSlidesClearAndInsertTextRequestsAt(objectID, text, cellLocation) |
| 74 | } else { |
| 75 | requests = append(requests, &slides.Request{ |
| 76 | InsertText: &slides.InsertTextRequest{ |
| 77 | CellLocation: cellLocation, |
| 78 | ObjectId: objectID, |
| 79 | Text: text, |
| 80 | InsertionIndex: c.InsertionIndex, |
| 81 | }, |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | body := &slides.BatchUpdatePresentationRequest{Requests: requests} |
| 86 |