Skip to content

Commit

Permalink
[develop] driveIdResolver.queryEntryByName return the first item if m…
Browse files Browse the repository at this point in the history
…ultiple items with the same name exist.
  • Loading branch information
SGH623QVQT committed Oct 3, 2017
1 parent 4168bfa commit 864ad7e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion drive/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (self *Drive) Id(args IdArgs) error {
//fmt.Fprintf(args.Out, "AbsPath='%v', Error='%v'\n", args.AbsPath, args.Error)

resolver := self.newIdResolver()
Id, err := resolver.getFileID(args.AbsPath)
Id, err := resolver.getFileId(args.AbsPath)
if err != nil && args.Error == true {
return err
}
Expand Down
22 changes: 9 additions & 13 deletions drive/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (drive *Drive) newIdResolver() *driveIdResolver {
}
}

func (self *driveIdResolver) getFileID(abspath string) (string, error) {
func (self *driveIdResolver) getFileId(abspath string) (string, error) {
if !strings.HasPrefix(abspath, "/") {
return "", fmt.Errorf("'%s' is not absolute path", abspath)
}
Expand All @@ -90,26 +90,26 @@ func (self *driveIdResolver) getFileID(abspath string) (string, error) {
pathes := strings.Split(abspath, "/")
var parent = "root"
for _, path := range pathes {
entries, err := self.queryEntryByName(path, parent)
if err != nil {
return "", err
entry := self.queryEntryByName(path, parent)
if entry == nil {
return "", fmt.Errorf("path not found: '%v'", abspath)
}
parent = entries[0].Id
parent = entry.Id
}
return parent, nil
}

func (self *driveIdResolver) secureFileId(expr string) string {
if strings.Contains(expr, "/") {
id, err := self.getFileID(expr)
id, err := self.getFileId(expr)
if err == nil {
return id
}
}
return expr
}

func (self *driveIdResolver) queryEntryByName(name string, parent string) ([]*drive.File, error) {
func (self *driveIdResolver) queryEntryByName(name string, parent string) *drive.File {
conditions := []string{
"trashed = false",
fmt.Sprintf("name = '%v'", name),
Expand All @@ -125,12 +125,8 @@ func (self *driveIdResolver) queryEntryByName(name string, parent string) ([]*dr
})

if len(files) == 0 {
return nil, fmt.Errorf("name not found: '%v'", name)
}

if len(files) != 1 {
return nil, fmt.Errorf("ambiguous name: '%v'", name)
return nil
}

return files, nil
return files[0]
}

0 comments on commit 864ad7e

Please sign in to comment.