Skip to content

Commit

Permalink
[develop] Added ls --document: flag to list Google document
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurahilljp committed Oct 4, 2017
1 parent 2efd928 commit a7a9317
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
19 changes: 11 additions & 8 deletions drive/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ListDirectoryArgs struct {
Out io.Writer
Id string
Recursive bool
ShowDoc bool
}

func (args *ListDirectoryArgs) normalize(drive *Drive) {
Expand All @@ -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)
Expand Down Expand Up @@ -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})
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions drive/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 3 additions & 26 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,16 @@ 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"},
Description: "List directory contents",
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,
},
),
Expand Down
1 change: 1 addition & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a7a9317

Please sign in to comment.