Skip to content

Commit

Permalink
Align printing + git like help descriptions
Browse files Browse the repository at this point in the history
* Aligning printed fields for list.
* Fixed bug in which during pull if parent
 & child had the same path relative to root
 a dir would be created even when child wasn't a dir
* Git like help descriptions ie `drive help push`
  • Loading branch information
Emmanuel Odeke committed Jan 7, 2015
1 parent 1c5f710 commit cd3005e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ However, she no longer has the time to work on it so I am its new maintainer.
Use `drive help` for further reference.

$ drive version
$ drive help cmd e.g drive help push
$ drive init [path]
$ drive pull [-r -no-prompt path1 path2 path3 ...] # pulls from remotes
$ drive pull [-r -no-prompt -hidden path] # pulls even hidden paths from remote
Expand Down
4 changes: 2 additions & 2 deletions about.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (g *Commands) Quota() (err error) {

freeBytes := about.QuotaBytesTotal - about.QuotaBytesUsed
fmt.Printf(
"Account type: %s\nBytes Used: %20d (%s)\n"+
"Bytes Free: %20d (%s)\nTotal Bytes: %20d (%s)\n",
"Account type:\t%s\nBytes Used:\t%-20d (%s)\n"+
"Bytes Free:\t%-20d (%s)\nTotal Bytes:\t%-20d (%s)\n",
about.QuotaType,
about.QuotaBytesUsed, prettyBytes(about.QuotaBytesUsed),
freeBytes, prettyBytes(freeBytes),
Expand Down
60 changes: 30 additions & 30 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,55 @@ import (
)

var context *config.Context

var Version = "0.0.4a"
var DefaultMaxProcs = runtime.NumCPU()

const (
descInit = "inits a directory and authenticates user"
descPull = "pulls remote changes from google drive"
descPush = "push local changes to google drive"
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"
descUnpublish = "revokes public access to a file"
descVersion = "prints the version"
)

func main() {
maxProcs, err := strconv.ParseInt(os.Getenv("GOMAXPROCS"), 10, 0)
if err != nil || maxProcs < 1 {
maxProcs = int64(DefaultMaxProcs)
}
runtime.GOMAXPROCS(int(maxProcs))

command.On("diff", descDiff, &diffCmd{}, []string{})
command.On("init", descInit, &initCmd{}, []string{})
command.On("list", descList, &listCmd{}, []string{})
command.On("pull", descPull, &pullCmd{}, []string{})
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{})
command.On("version", descVersion, &versionCmd{}, []string{})
command.On("diff", drive.DescDiff, &diffCmd{}, []string{})
command.On("init", drive.DescInit, &initCmd{}, []string{})
command.On("list", drive.DescList, &listCmd{}, []string{})
command.On("pull", drive.DescPull, &pullCmd{}, []string{})
command.On("push", drive.DescPush, &pushCmd{}, []string{})
command.On("pub", drive.DescPublish, &publishCmd{}, []string{})
command.On("emptytrash", drive.DescEmptyTrash, &emptyTrashCmd{}, []string{})
command.On("help", drive.DescHelp, &helpCmd{}, []string{})
command.On("quota", drive.DescQuota, &quotaCmd{}, []string{})
command.On("trash", drive.DescTrash, &trashCmd{}, []string{})
command.On("untrash", drive.DescUntrash, &untrashCmd{}, []string{})
command.On("unpub", drive.DescUnpublish, &unpublishCmd{}, []string{})
command.On("version", drive.Version, &versionCmd{}, []string{})
command.ParseAndRun()
}

type helpCmd struct {
args []string
}

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

func (cmd *helpCmd) Run(args []string) {
if len(args) < 1 {
exitWithError(fmt.Errorf("help for more usage"))
}
drive.ShowDescription(args[0])
exitWithError(nil)
}

type versionCmd struct{}

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

func (cmd *versionCmd) Run(args []string) {
fmt.Printf("drive version %s\n", Version)
fmt.Printf("drive version %s\n", drive.Version)
exitWithError(nil)
}

Expand Down
72 changes: 72 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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"
)

var Version = "0.0.4b"

This comment has been minimized.

Copy link
@odeke-em

odeke-em Jan 7, 2015

Owner

@shaggytwodope version 0.0.4b is in. Thank you.


const (
DescInit = "inits a directory and authenticates user"
DescPull = "pulls remote changes from google drive"
DescPush = "push local changes to google drive"
DescDiff = "compares a local file with remote"
DescEmptyTrash = "cleans out your trash"
DescHelp = "Get help for a topic"
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"
DescUnpublish = "revokes public access to a file"
DescVersion = "prints the version"
)

var shortToCmd = map[string][]string{
"diff": []string{DescDiff, "Accepts multiple paths for comparison"},
"emptytrash": []string{DescEmptyTrash},
"init": []string{DescInit, "This is where you drive credentials will be placed"},
"pull": []string{DescPull, "Accepts multiple paths"},
"push": []string{
DescPush, "Accepts multiple paths", "Push comes in a couple of flavors",
"\t* Ordinary push: `drive push path1 path2 path3`",
"\t* Mounted push: `drive push -m path1 [path2 path3] drive_context_path`",
},
"list": []string{DescList, "Accepts multiple paths"},
"quota": []string{DescQuota},
"trash": []string{DescTrash, "Accepts multiple paths"},
"untrash": []string{DescUntrash, "Accepts multiple paths"},
"pub": []string{DescPublish, "Accepts multiple paths"},
"unpub": []string{DescUnpublish, "Accepts multiple paths"},
"version": []string{DescVersion, fmt.Sprintf("current version is: %s", Version)},
}

func ShowDescription(topic string) {
help, ok := shortToCmd[topic]
if !ok {
fmt.Printf("Unkown command '%s' type `drive help` for usage\n", topic)
} else {
description, documentation := help[0], help[1:]
fmt.Printf("Name\n\t%s - %s\n", topic, description)
if len(documentation) >= 1 {
fmt.Println("Description")
for _, line := range documentation {
fmt.Printf("\t%s\n", line)
}
}
}
}
8 changes: 6 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"strings"
)

const (
DriveAbsRootPath = "My Drive"
)

var BytesPerKB = float64(1024)

type byteDescription func(b int64) string
Expand Down Expand Up @@ -139,7 +143,7 @@ func (f *File) pretty(opt attribute) {
fmt.Printf("%-10s ", f.UserPermission.Role)
}
fPath := fmt.Sprintf("%s/%s", opt.parent, f.Name)
fmt.Printf("%-10s %-6s %s", prettyBytes(f.Size), fPath, f.ModTime)
fmt.Printf("%-10s\t%-60s\t\t%-20s", prettyBytes(f.Size), fPath, f.ModTime)
fmt.Println()
}

Expand All @@ -161,7 +165,7 @@ func (g *Commands) breadthFirst(parentId, parent, child string, depth int, inTra
expr = fmt.Sprintf("'%s' in parents and trashed=false", parentId)
}
headPath := ""
if parent != "" {
if parent != "" && parent != DriveAbsRootPath {
headPath = parent
}
if child != "" {
Expand Down
9 changes: 6 additions & 3 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ func (g *Commands) localAdd(wg *sync.WaitGroup, change *Change, exports []string

// make parent's dir if not exists
destAbsDir := g.context.AbsPathOf(change.Parent)
os.MkdirAll(destAbsDir, os.ModeDir|0755)

if err != nil {
return
if destAbsDir != destAbsPath {
err = os.MkdirAll(destAbsDir, os.ModeDir|0755)
if err != nil {
return err
}
}

if change.Src.IsDir {
return os.Mkdir(destAbsPath, os.ModeDir|0755)
}
Expand Down

0 comments on commit cd3005e

Please sign in to comment.