Skip to content

feat(drive): add download command #65

@rianjs

Description

@rianjs

Summary

Add gro drive download command to download files from Google Drive, including exporting Google Workspace files to common formats.

Usage

# Download regular files (PDF, images, etc.)
gro drive download <file-id>
gro drive download <file-id> --output ./report.pdf

# Export Google Docs to different formats
gro drive download <file-id> --format pdf      # Document → PDF
gro drive download <file-id> --format docx     # Document → Word
gro drive download <file-id> --format md       # Document → Markdown
gro drive download <file-id> --format txt      # Document → Plain text

# Export Google Sheets
gro drive download <file-id> --format xlsx     # Spreadsheet → Excel
gro drive download <file-id> --format csv      # Spreadsheet → CSV

# Export Google Slides
gro drive download <file-id> --format pptx     # Presentation → PowerPoint
gro drive download <file-id> --format pdf      # Presentation → PDF

# Output to stdout
gro drive download <file-id> --stdout

Flags

Flag Short Default Description
--output -o ./<filename> Output file path
--format -f - Export format for Google files
--stdout - false Write to stdout instead of file

Export Format Mapping

Google Documents

Format MIME Type
pdf application/pdf
docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
txt text/plain
html text/html
md text/markdown
rtf application/rtf
odt application/vnd.oasis.opendocument.text

Google Spreadsheets

Format MIME Type
pdf application/pdf
xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
csv text/csv
tsv text/tab-separated-values
ods application/vnd.oasis.opendocument.spreadsheet

Google Presentations

Format MIME Type
pdf application/pdf
pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
odp application/vnd.oasis.opendocument.presentation

Google Drawings

Format MIME Type
pdf application/pdf
png image/png
svg image/svg+xml
jpg image/jpeg

Implementation

Files to Create/Modify

  • internal/cmd/drive/download.go - Download command implementation
  • internal/cmd/drive/download_test.go - Unit tests
  • internal/cmd/drive/drive.go - Register download subcommand
  • internal/drive/files.go - Add DownloadFile() and ExportFile() methods

Logic Flow

func runDownload(cmd *cobra.Command, args []string) error {
    fileID := args[0]
    
    // Get file metadata first
    file, err := client.GetFile(fileID)
    
    // Determine if this is a Google Workspace file
    if isGoogleWorkspaceFile(file.MimeType) {
        // Must use Export API
        if format == "" {
            return fmt.Errorf("Google %s requires --format flag", getTypeName(file.MimeType))
        }
        exportMime := getExportMimeType(file.MimeType, format)
        data, err := client.ExportFile(fileID, exportMime)
    } else {
        // Regular file - use Get with alt=media
        data, err := client.DownloadFile(fileID)
    }
    
    // Write to file or stdout
    if stdout {
        os.Stdout.Write(data)
    } else {
        outputPath := determineOutputPath(file.Name, format, outputFlag)
        os.WriteFile(outputPath, data, 0644)
    }
}

API Calls

// Download regular file
resp, err := srv.Files.Get(fileID).Download()

// Export Google Workspace file
resp, err := srv.Files.Export(fileID, exportMimeType).Download()

Output

Downloading: Q4 Budget Report.docx
Format: Microsoft Word (docx)
Size: 245 KB
Saved to: ./Q4 Budget Report.docx

Error Handling

  • Invalid file ID → "file not found: "
  • Google Doc without --format → "Google Document requires --format flag (pdf, docx, txt, html, md)"
  • Invalid format for file type → "format 'csv' not supported for Google Document"
  • Permission denied → "access denied: you don't have permission to download this file"

Acceptance Criteria

  • Downloads regular files (PDF, images, etc.)
  • Exports Google Docs to PDF, DOCX, TXT, MD
  • Exports Google Sheets to XLSX, CSV
  • Exports Google Slides to PPTX, PDF
  • Respects --output path
  • --stdout writes to stdout
  • Clear error when --format missing for Google files
  • Clear error for invalid format
  • make verify passes

Blocked By

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions