Skip to content

Commit

Permalink
mkdir implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jan 17, 2016
1 parent 9e3c856 commit 9e19c83
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _release/bin
Session.vim
.netrwhist
drive_old
foo.txt
21 changes: 21 additions & 0 deletions drive/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"golang.org/x/net/context"
)

const DirectoryMimeType = "application/vnd.google-apps.folder"

func (self *Drive) List(args ListFilesArgs) {
fileList, err := self.service.Files.List().PageSize(args.MaxFiles).Q(args.Query).Fields("nextPageToken", "files(id,name,size,createdTime)").Do()
errorF(err, "Failed listing files: %s\n", err)
Expand Down Expand Up @@ -116,6 +118,25 @@ func (self *Drive) Info(args FileInfoArgs) {
})
}

func (self *Drive) Mkdir(args MkdirArgs) {
dstFile := &drive.File{Name: args.Name, MimeType: DirectoryMimeType}

// Set parent folder if provided
if args.Parent != "" {
dstFile.Parents = []string{args.Parent}
}

// Create folder
f, err := self.service.Files.Create(dstFile).Do()
errorF(err, "Failed to create folder: %s", err)

PrintFileInfo(PrintFileInfoArgs{File: f})

//if args.Share {
// self.Share(TODO)
//}
}

//func newFile(args UploadFileArgs) *drive.File {
//
//}
4 changes: 4 additions & 0 deletions drive/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func formatList(a []string) string {
}

func formatSize(bytes int64, forceBytes bool) string {
if bytes == 0 {
return ""
}

if forceBytes {
return fmt.Sprintf("%v B", bytes)
}
Expand Down
6 changes: 6 additions & 0 deletions drive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ type FileInfoArgs struct {
SizeInBytes bool
}

type MkdirArgs struct {
Name string
Parent string
Share bool
}

type PrintFileListArgs struct {
Files []*drive.File
NameWidth int
Expand Down
2 changes: 1 addition & 1 deletion gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func main() {
&cli.Handler{
Pattern: "[global options] mkdir [options] <name>",
Description: "Create directory",
Callback: handler,
Callback: mkdirHandler,
Flags: cli.Flags{
"global options": globalFlags,
"options": []cli.Flag{
Expand Down
22 changes: 14 additions & 8 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ const TokenFilename = "token_v2.json"

func listHandler(ctx cli.Context) {
args := ctx.Args()
gdrive := newDrive(args)

gdrive.List(drive.ListFilesArgs{
newDrive(args).List(drive.ListFilesArgs{
MaxFiles: args.Int64("maxFiles"),
NameWidth: args.Int64("nameWidth"),
Query: args.String("query"),
Expand All @@ -28,9 +27,8 @@ func listHandler(ctx cli.Context) {

func downloadHandler(ctx cli.Context) {
args := ctx.Args()
gdrive := newDrive(args)

gdrive.Download(drive.DownloadFileArgs{
newDrive(args).Download(drive.DownloadFileArgs{
Id: args.String("id"),
Force: args.Bool("force"),
Stdout: args.Bool("stdout"),
Expand All @@ -40,9 +38,8 @@ func downloadHandler(ctx cli.Context) {

func uploadHandler(ctx cli.Context) {
args := ctx.Args()
gdrive := newDrive(args)

gdrive.Upload(drive.UploadFileArgs{
newDrive(args).Upload(drive.UploadFileArgs{
Path: args.String("path"),
Name: args.String("name"),
Parent: args.String("parent"),
Expand All @@ -55,14 +52,23 @@ func uploadHandler(ctx cli.Context) {

func infoHandler(ctx cli.Context) {
args := ctx.Args()
gdrive := newDrive(args)

gdrive.Info(drive.FileInfoArgs{
newDrive(args).Info(drive.FileInfoArgs{
Id: args.String("id"),
SizeInBytes: args.Bool("sizeInBytes"),
})
}

func mkdirHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Mkdir(drive.MkdirArgs{
Name: args.String("name"),
Parent: args.String("parent"),
Share: args.Bool("share"),
})
}

func deleteHandler(ctx cli.Context) {
fmt.Println("Deleting...")
}
Expand Down

0 comments on commit 9e19c83

Please sign in to comment.