Skip to content

Commit

Permalink
about drive: quota querying in
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Odeke committed Jan 6, 2015
1 parent c73f691 commit 4803315
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ Use `drive help` for further reference.

$ drive trash [path1 path2 ...] # Sends the remote specified files to the trash.
$ drive untrash [path1 path2 ...] # Restore the specified remote files from trash.
$ drive emptytrash
$ drive emptytrash [-no-prompt] # No prompt is presented before emptying out your trash.
$ drive emptytrash
$ drive emptytrash [-no-prompt] # No prompt is presented before emptying out your trash.

$ drive quota # To return quota information

## Configuration

Expand Down Expand Up @@ -249,7 +251,7 @@ if successful will create a directory logo\_exports which will look like:

* The next pull that you do should clean up that file off your disk.

2) Using your terminal, take that file out of its position and then perform a push on only that file.
2)

![drive trash/untrash](https://github.com/odeke-em/wiki_content/blob/master/drive/trash-untrash.png)

Expand Down Expand Up @@ -277,6 +279,18 @@ if successful will create a directory logo\_exports which will look like:

![drive emptytrash](https://github.com/odeke-em/wiki_content/blob/master/drive/emptytrash.png)

**Quota Information**
====
+ What does `quota` do ?
Returns you information about your drive.

+ How is it done ?

`drive quota`
`Bytes Used: 967MB`
`Bytes Free: 15GB`
`Total Bytes: 16GB`
`Account type: LIMITED`


## LICENSE
Expand Down
67 changes: 67 additions & 0 deletions about.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package drive

import (
"fmt"
)

const (
Barely = iota
AlmostExceeded
HalfwayExceeded
Exceeded
Unknown
)

func (g *Commands) Quota() (err error) {
about, err := g.rem.About()
if err != nil {
return err
}

freeBytes := about.QuotaBytesTotal - about.QuotaBytesUsed
fmt.Printf(
"Bytes Used: %v\nBytes Free: %v\nTotal Bytes: %v\nAccount type: %s\n",
prettyBytes(about.QuotaBytesUsed), prettyBytes(freeBytes),
prettyBytes(about.QuotaBytesTotal), about.QuotaType)
return nil
}

func (g *Commands) QuotaStatus(query int64) (status int, err error) {
about, err := g.rem.About()
if err != nil || query < 0 {
return Unknown, err
}

// Sanity check
if about.QuotaBytesTotal < 1 {
return Unknown, fmt.Errorf("QuotaBytesTotal < 1")
}

toBeUsed := query + about.QuotaBytesTotal
if about.QuotaBytesUsed >= toBeUsed {
return Exceeded, nil
}

percentage := float64(about.QuotaBytesUsed) / float64(toBeUsed)
if percentage < 0.5 {
return Barely, nil
}
if percentage < 0.8 {
return HalfwayExceeded, nil
}
return AlmostExceeded, nil
}
15 changes: 15 additions & 0 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
descDiff = "compares a local file with remote"
descEmptyTrash = "cleans out your trash"
descList = "lists the contents of remote path"
descQuota = "prints out the space information"
descPublish = "publishes a file and prints its publicly available url"
descTrash = "moves the file to trash"
descUntrash = "restores the file from trash"
Expand All @@ -63,6 +64,7 @@ func main() {
command.On("push", descPush, &pushCmd{}, []string{})
command.On("pub", descPublish, &publishCmd{}, []string{})
command.On("emptytrash", descEmptyTrash, &emptyTrashCmd{}, []string{})
command.On("quota", descQuota, &quotaCmd{}, []string{})
command.On("trash", descTrash, &trashCmd{}, []string{})
command.On("untrash", descUntrash, &untrashCmd{}, []string{})
command.On("unpub", descUnpublish, &unpublishCmd{}, []string{})
Expand Down Expand Up @@ -91,6 +93,19 @@ func (cmd *initCmd) Run(args []string) {
exitWithError(drive.New(initContext(args), nil).Init())
}

type quotaCmd struct{}

func (cmd *quotaCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
return fs
}

func (cmd *quotaCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(drive.New(context, &drive.Options{
Path: path,
}).Quota())
}

type listCmd struct {
hidden *bool
pageCount *int
Expand Down
4 changes: 4 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ func (r *Remote) FindByPathShared(p string) (file []*File, err error) {
return r.findShared(nonEmpty)
}

func (r *Remote) About() (about *drive.About, err error) {
return r.service.About.Get().Do()
}

func (r *Remote) findByPathRecvRaw(parentId string, p []string, trashed bool) (file *File, err error) {
// find the file or directory under parentId and titled with p[0]
req := r.service.Files.List()
Expand Down

0 comments on commit 4803315

Please sign in to comment.