From a7a931758aab52e6682da7aeeceafe71adde6a63 Mon Sep 17 00:00:00 2001 From: satoniho-mba Date: Wed, 4 Oct 2017 22:12:08 +0900 Subject: [PATCH] [develop] Added ls --document: flag to list Google document --- drive/ls.go | 19 +++++++++++-------- drive/path.go | 10 ++++++++++ gdrive.go | 29 +++-------------------------- handlers_drive.go | 1 + 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/drive/ls.go b/drive/ls.go index e04da156..0c95120b 100644 --- a/drive/ls.go +++ b/drive/ls.go @@ -13,6 +13,7 @@ type ListDirectoryArgs struct { Out io.Writer Id string Recursive bool + ShowDoc bool } func (args *ListDirectoryArgs) normalize(drive *Drive) { @@ -28,31 +29,29 @@ func (self *Drive) ListDirectory(args ListDirectoryArgs) (err error) { return fmt.Errorf("Failed to get file: %s", err) } if isDir(f) { - printer := NewDirectoryPrinter(self, args) + printer := NewDirectoryPrinter(self, &args) printer.Print(f, "") } return } type DirectoryPrinter struct { - Out io.Writer Drive *Drive PathFinder *remotePathFinder - Recursive bool + Args *ListDirectoryArgs } -func NewDirectoryPrinter(drive *Drive, args ListDirectoryArgs) *DirectoryPrinter { +func NewDirectoryPrinter(drive *Drive, args *ListDirectoryArgs) *DirectoryPrinter { return &DirectoryPrinter{ - Out: args.Out, Drive: drive, PathFinder: drive.newPathFinder(), - Recursive: args.Recursive, + Args: args, } } func (printer *DirectoryPrinter) Print(file *drive.File, absPath string) error { w := new(tabwriter.Writer) - w.Init(printer.Out, 0, 0, 3, ' ', 0) + w.Init(printer.Args.Out, 0, 0, 3, ' ', 0) if len(absPath) == 0 { name, err := printer.PathFinder.getAbsPath(file) @@ -82,6 +81,10 @@ func (printer *DirectoryPrinter) Print(file *drive.File, absPath string) error { var directories []directory for _, f := range files { + if isDoc(f) && !printer.Args.ShowDoc { + continue + } + fullpath := printer.PathFinder.JoinPath(absPath, f.Name) if isDir(f) { directories = append(directories, directory{f, fullpath}) @@ -94,7 +97,7 @@ func (printer *DirectoryPrinter) Print(file *drive.File, absPath string) error { fmt.Fprintf(w, "%v%v\n", fullpath, term) } - if printer.Recursive { + if printer.Args.Recursive { fmt.Fprintf(w, "\n") for _, d := range directories { printer.Print(d.f, d.fullpath) diff --git a/drive/path.go b/drive/path.go index f62b89b9..8b6ce1f5 100644 --- a/drive/path.go +++ b/drive/path.go @@ -152,3 +152,13 @@ func (self *remotePathFinder) queryEntryByName(name string, parent string) *driv return files[0] } + +func isDoc(f *drive.File) bool { + if isDir(f) { + return false + } + if isBinary(f) { + return false + } + return true +} diff --git a/gdrive.go b/gdrive.go index 9a38975e..f00ac426 100644 --- a/gdrive.go +++ b/gdrive.go @@ -106,29 +106,6 @@ func main() { FlagGroups: cli.FlagGroups{ cli.NewFlagGroup("global", globalFlags...), cli.NewFlagGroup("options", - cli.StringFlag{ - Name: "sortOrder", - Patterns: []string{"--order"}, - Description: "Sort order. See https://godoc.org/google.golang.org/api/drive/v3#FilesListCall.OrderBy", - }, - cli.IntFlag{ - Name: "nameWidth", - Patterns: []string{"--name-width"}, - Description: fmt.Sprintf("Width of name column, default: %d, minimum: 9, use 0 for full width", DefaultNameWidth), - DefaultValue: DefaultNameWidth, - }, - cli.BoolFlag{ - Name: "skipHeader", - Patterns: []string{"--no-header"}, - Description: "Dont print the header", - OmitValue: true, - }, - cli.BoolFlag{ - Name: "sizeInBytes", - Patterns: []string{"--bytes"}, - Description: "Size in bytes", - OmitValue: true, - }, cli.BoolFlag{ Name: "recursive", Patterns: []string{"-r", "--recursive"}, @@ -136,9 +113,9 @@ func main() { OmitValue: true, }, cli.BoolFlag{ - Name: "id", - Patterns: []string{"--id"}, - Description: "Print file ID", + Name: "doc", + Patterns: []string{"-d", "--document"}, + Description: "List Google documents", OmitValue: true, }, ), diff --git a/handlers_drive.go b/handlers_drive.go index 5efb83a8..9055c38f 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -39,6 +39,7 @@ func lsHandler(ctx cli.Context) { Out: os.Stdout, Id: args.String("fileId"), Recursive: args.Bool("recursive"), + ShowDoc: args.Bool("doc"), }) checkErr(err) }