Exemplo n.º 1
0
func List(d *gdrive.Drive, query, titleFilter string, maxResults int, sharedStatus, noHeader, includeDocs, sizeInBytes bool) error {
	caller := d.Files.List()

	if maxResults > 0 {
		caller.MaxResults(int64(maxResults))
	}

	if titleFilter != "" {
		q := fmt.Sprintf("title contains '%s'", titleFilter)
		caller.Q(q)
	}

	if query != "" {
		caller.Q(query)
	}

	list, err := caller.Do()
	if err != nil {
		return err
	}

	items := make([]map[string]string, 0, 0)

	for _, f := range list.Items {
		if f.DownloadUrl == "" && !includeDocs {
			if f.MimeType != "application/vnd.google-apps.folder" {
				continue
			}
		}
		if f.Labels.Trashed {
			continue
		}

		items = append(items, map[string]string{
			"Id":      f.Id,
			"Title":   util.TruncateString(f.Title, 40),
			"Size":    util.FileSizeFormat(f.FileSize, sizeInBytes),
			"Created": util.ISODateToLocal(f.CreatedDate),
		})
	}

	columnOrder := []string{"Id", "Title", "Size", "Created"}

	if sharedStatus {
		addSharedStatus(d, items)
		columnOrder = append(columnOrder, "Shared")
	}

	util.PrintColumns(items, columnOrder, 3, noHeader)
	return nil
}
Exemplo n.º 2
0
Arquivo: cli.go Projeto: occho/gdrive
func List(d *gdrive.Drive, query, titleFilter string, maxResults int, sharedStatus, noHeader, includeDocs, sizeInBytes bool) error {
	caller := d.Files.List()
	queryList := []string{}

	if maxResults > 0 {
		caller.MaxResults(int64(maxResults))
	}

	if titleFilter != "" {
		q := fmt.Sprintf("title contains '%s'", titleFilter)
		queryList = append(queryList, q)
	}

	if query != "" {
		queryList = append(queryList, query)
	} else {
		// Skip trashed files
		queryList = append(queryList, "trashed = false")

		// Skip google docs
		if !includeDocs {
			for _, mime := range googleMimeTypes {
				q := fmt.Sprintf("mimeType != '%s'", mime)
				queryList = append(queryList, q)
			}
		}
	}

	if len(queryList) > 0 {
		q := strings.Join(queryList, " and ")
		caller.Q(q)
	}

	list, err := caller.Do()
	if err != nil {
		return err
	}

	files := list.Items

	for list.NextPageToken != "" {
		if maxResults > 0 && len(files) > maxResults {
			break
		}

		caller.PageToken(list.NextPageToken)
		list, err = caller.Do()
		if err != nil {
			return err
		}
		files = append(files, list.Items...)
	}

	items := make([]map[string]string, 0, 0)

	for _, f := range files {
		if maxResults > 0 && len(items) >= maxResults {
			break
		}

		items = append(items, map[string]string{
			"Id":      f.Id,
			"Title":   util.TruncateString(f.Title, 40),
			"Size":    util.FileSizeFormat(f.FileSize, sizeInBytes),
			"Created": util.ISODateToLocal(f.CreatedDate),
		})
	}

	columnOrder := []string{"Id", "Title", "Size", "Created"}

	if sharedStatus {
		addSharedStatus(d, items)
		columnOrder = append(columnOrder, "Shared")
	}

	util.PrintColumns(items, columnOrder, 3, noHeader)
	return nil
}