Skip to content

Commit

Permalink
Add quota flag
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed May 23, 2015
1 parent cc867e0 commit 1f613ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
32 changes: 22 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func List(d *gdrive.Drive, query, titleFilter string, maxResults int, sharedStat
items = append(items, map[string]string{
"Id": f.Id,
"Title": util.TruncateString(f.Title, 40),
"Size": util.FileSizeFormat(f.FileSize),
"Size": util.FileSizeFormat(f.FileSize, false),
"Created": util.ISODateToLocal(f.CreatedDate),
})
}
Expand Down Expand Up @@ -101,16 +101,16 @@ func Info(d *gdrive.Drive, fileId string) error {
if err != nil {
return fmt.Errorf("An error occurred: %v\n", err)
}
printInfo(d, info)
printInfo(d, info, false)
return nil
}

func printInfo(d *gdrive.Drive, f *drive.File) {
func printInfo(d *gdrive.Drive, f *drive.File, sizeInBytes bool) {
fields := map[string]string{
"Id": f.Id,
"Title": f.Title,
"Description": f.Description,
"Size": util.FileSizeFormat(f.FileSize),
"Size": util.FileSizeFormat(f.FileSize, sizeInBytes),
"Created": util.ISODateToLocal(f.CreatedDate),
"Modified": util.ISODateToLocal(f.ModifiedDate),
"Owner": strings.Join(f.OwnerNames, ", "),
Expand Down Expand Up @@ -140,7 +140,7 @@ func Folder(d *gdrive.Drive, title string, parentId string, share bool) error {
if err != nil {
return err
}
printInfo(d, info)
printInfo(d, info, false)
fmt.Printf("Folder '%s' created\n", info.Title)
return nil
}
Expand Down Expand Up @@ -189,9 +189,9 @@ func UploadStdin(d *gdrive.Drive, input io.ReadCloser, title string, parentId st
bytes := info.FileSize

// Print information about uploaded file
printInfo(d, info)
printInfo(d, info, false)
fmt.Printf("MIME Type: %s\n", mimeType)
fmt.Printf("Uploaded '%s' at %s, total %s\n", info.Title, getRate(bytes), util.FileSizeFormat(bytes))
fmt.Printf("Uploaded '%s' at %s, total %s\n", info.Title, getRate(bytes), util.FileSizeFormat(bytes, false))

// Share file if the share flag was provided
if share {
Expand Down Expand Up @@ -297,9 +297,9 @@ func uploadFile(d *gdrive.Drive, input *os.File, inputInfo os.FileInfo, title st
bytes := info.FileSize

// Print information about uploaded file
printInfo(d, info)
printInfo(d, info, false)
fmt.Printf("MIME Type: %s\n", mimeType)
fmt.Printf("Uploaded '%s' at %s, total %s\n", info.Title, getRate(bytes), util.FileSizeFormat(bytes))
fmt.Printf("Uploaded '%s' at %s, total %s\n", info.Title, getRate(bytes), util.FileSizeFormat(bytes, false))

// Share file if the share flag was provided
if share {
Expand Down Expand Up @@ -375,7 +375,7 @@ func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool,
return fmt.Errorf("An error occurred: %s", err)
}

fmt.Printf("Downloaded '%s' at %s, total %s\n", fileName, getRate(bytes), util.FileSizeFormat(bytes))
fmt.Printf("Downloaded '%s' at %s, total %s\n", fileName, getRate(bytes), util.FileSizeFormat(bytes, false))

if deleteAfterDownload {
err = Delete(d, fileId)
Expand Down Expand Up @@ -435,6 +435,18 @@ func Unshare(d *gdrive.Drive, fileId string) error {
return nil
}

func Quota(d *gdrive.Drive, sizeInBytes bool) error {
info, err := d.About.Get().Do()
if err != nil {
return fmt.Errorf("An error occurred: %v\n", err)
}

fmt.Printf("Used: %s\n", util.FileSizeFormat(info.QuotaBytesUsed, sizeInBytes))
fmt.Printf("Free: %s\n", util.FileSizeFormat(info.QuotaBytesTotal-info.QuotaBytesUsed, sizeInBytes))
fmt.Printf("Total: %s\n", util.FileSizeFormat(info.QuotaBytesTotal, sizeInBytes))
return nil
}

func isShared(d *gdrive.Drive, fileId string) bool {
r, err := d.Permissions.List(fileId).Do()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type Options struct {
Preview bool `goptions:"-p, --preview, mutexgroup='urltype', description='Generate preview url (default)'"`
Download bool `goptions:"-d, --download, mutexgroup='urltype', description='Generate download url'"`
} `goptions:"url"`

Quota struct {
SizeInBytes bool `goptions:"--bytes, description='Show size in bytes'"`
} `goptions:"quota"`
}

func main() {
Expand Down Expand Up @@ -145,6 +149,9 @@ func main() {
fmt.Println(util.PreviewUrl(opts.Url.FileId))
}

case "quota":
err = cli.Quota(drive, opts.Quota.SizeInBytes)

default:
goptions.PrintHelp()
}
Expand Down
10 changes: 7 additions & 3 deletions util/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func FormatBool(b bool) string {
return strings.Title(strconv.FormatBool(b))
}

func FileSizeFormat(bytes int64) string {
func FileSizeFormat(bytes int64, forceBytes bool) string {
if forceBytes {
return fmt.Sprintf("%v B", bytes)
}

units := []string{"B", "KB", "MB", "GB", "TB", "PB"}

var i int
Expand Down Expand Up @@ -122,10 +126,10 @@ func MeasureTransferRate() func(int64) string {
return func(bytes int64) string {
seconds := int64(time.Now().Sub(start).Seconds())
if seconds < 1 {
return fmt.Sprintf("%s/s", FileSizeFormat(bytes))
return fmt.Sprintf("%s/s", FileSizeFormat(bytes, false))
}
bps := bytes / seconds
return fmt.Sprintf("%s/s", FileSizeFormat(bps))
return fmt.Sprintf("%s/s", FileSizeFormat(bps, false))
}
}

Expand Down

0 comments on commit 1f613ca

Please sign in to comment.