(ctx context.Context, flags *RootFlags)
| 251 | } |
| 252 | |
| 253 | func (c *DocsCreateCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 254 | u := ui.FromContext(ctx) |
| 255 | title := strings.TrimSpace(c.Title) |
| 256 | if title == "" { |
| 257 | return usage("empty title") |
| 258 | } |
| 259 | |
| 260 | f := &drive.File{ |
| 261 | Name: title, |
| 262 | MimeType: "application/vnd.google-apps.document", |
| 263 | } |
| 264 | parent := strings.TrimSpace(c.Parent) |
| 265 | if parent != "" { |
| 266 | f.Parents = []string{parent} |
| 267 | } |
| 268 | |
| 269 | if err := dryRunExit(ctx, flags, "docs.create", map[string]any{ |
| 270 | strFile: f, |
| 271 | "sourceFile": c.File, |
| 272 | "parent": parent, |
| 273 | "pageless": c.Pageless, |
| 274 | }); err != nil { |
| 275 | return err |
| 276 | } |
| 277 | |
| 278 | account, driveSvc, err := requireDriveService(ctx, flags) |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | |
| 283 | createCall := driveSvc.Files.Create(f). |
| 284 | SupportsAllDrives(true). |
| 285 | Fields("id, name, mimeType, webViewLink") |
| 286 | |
| 287 | // When --file is set, upload the markdown content and let Drive convert it. |
| 288 | var images []markdownImage |
| 289 | if c.File != "" { |
| 290 | raw, readErr := os.ReadFile(c.File) |
| 291 | if readErr != nil { |
| 292 | return fmt.Errorf("read markdown file: %w", readErr) |
| 293 | } |
| 294 | markdown := prepareMarkdown(string(raw)) |
| 295 | images = markdown.images |
| 296 | |
| 297 | createCall = createCall.Media( |
| 298 | strings.NewReader(markdown.cleaned), |
| 299 | gapi.ContentType("text/markdown"), |
| 300 | ) |
| 301 | } |
| 302 | |
| 303 | created, err := createCall.Context(ctx).Do() |
| 304 | if err != nil { |
| 305 | return err |
| 306 | } |
| 307 | if created == nil { |
| 308 | return errors.New("create failed") |
| 309 | } |
| 310 |
nothing calls this directly
no test coverage detected