aboutsummaryrefslogtreecommitdiffstats
path: root/drive/list.go
blob: b2e3662b557688da308280c64b43d72590410a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package drive

import (
    "fmt"
    "os"
    "text/tabwriter"
    "google.golang.org/api/drive/v3"
)

type ListFilesArgs struct {
    MaxFiles int64
    NameWidth int64
    Query string
    SkipHeader bool
    SizeInBytes bool
}

func (self *Drive) List(args ListFilesArgs) (err error) {
    fileList, err := self.service.Files.List().PageSize(args.MaxFiles).Q(args.Query).Fields("nextPageToken", "files(id,name,size,createdTime)").Do()
    if err != nil {
        return fmt.Errorf("Failed listing files: %s", err)
    }

    PrintFileList(PrintFileListArgs{
        Files: fileList.Files,
        NameWidth: int(args.NameWidth),
        SkipHeader: args.SkipHeader,
        SizeInBytes: args.SizeInBytes,
    })

    return
}

type PrintFileListArgs struct {
    Files []*drive.File
    NameWidth int
    SkipHeader bool
    SizeInBytes bool
}

func PrintFileList(args PrintFileListArgs) {
    w := new(tabwriter.Writer)
    w.Init(os.Stdout, 0, 0, 3, ' ', 0)

    if !args.SkipHeader {
        fmt.Fprintln(w, "Id\tName\tSize\tCreated")
    }

    for _, f := range args.Files {
        fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
            f.Id,
            truncateString(f.Name, args.NameWidth),
            formatSize(f.Size, args.SizeInBytes),
            formatDatetime(f.CreatedTime),
        )
    }

    w.Flush()
}