Skip to content

Commit

Permalink
Merge branch 'fwwkr/drive-cmd-lsdrives' into fwwkr/master
Browse files Browse the repository at this point in the history
  • Loading branch information
mawaya committed Jul 3, 2020
2 parents 2ba1833 + 7213cdf commit 2191489
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions backend/drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,32 @@ func (f *Fs) makeShortcut(ctx context.Context, srcPath string, dstFs *Fs, dstPat
return dstFs.newObjectWithInfo(dstPath, info)
}

// List all team drives with right
func (f *Fs) listDrives(ctx context.Context) (driveMap map[string]*drive.Drive, err error) {
driveMap = make(map[string]*drive.Drive)

listDrives := f.svc.Drives.List().PageSize(100)
for {
var drives *drive.DriveList
err = f.pacer.Call(func() (bool, error) {
drives, err = listDrives.Context(ctx).Do()
return f.shouldRetry(err)
})
if err != nil {
return nil, errors.Errorf("Listing team drives failed: %v\n", err)
}
for _, drive := range drives.Drives {
driveMap[drive.Id] = drive
}
if drives.NextPageToken == "" {
break
}
listDrives.PageToken(drives.NextPageToken)
}

return driveMap, err
}

var commandHelp = []fs.CommandHelp{{
Name: "get",
Short: "Get command for fetching the drive config parameters",
Expand Down Expand Up @@ -3520,6 +3546,19 @@ authenticated with "drive2:" can't read files from "drive:".
Opts: map[string]string{
"target": "optional target remote for the shortcut destination",
},
}, {
Name: "lsdrives",
Short: "List all shared drives with right",
Long: `This command list all shared drives with right
Usage:
rclone backend lsdrives drive:
rclone backend lsdrives drive: -o separator=;
`,
Opts: map[string]string{
"separator": `Separator for the items in the format. (default "[TAB]")`,
},
}}

// Command the backend to run a named command
Expand Down Expand Up @@ -3583,6 +3622,45 @@ func (f *Fs) Command(ctx context.Context, name string, arg []string, opt map[str
}
}
return f.makeShortcut(ctx, arg[0], dstFs, arg[1])

// Mod
case "lsdrives":
if len(arg) >= 1 {
return nil, errors.New("no arguments needed")
}

sep := "\t"
if s, ok := opt["separator"]; ok {
sep = s
}

driveMap, err := f.listDrives(ctx)
if err != nil {
return nil, err
}

if len(driveMap) == 0 {
fs.Infof(f, "No team drives found")
return nil, nil
}

// sort
keys := make([]string, 0)
for _, drive := range driveMap {
keys = append(keys, drive.Name)
}
sort.Strings(keys)

for _, k := range keys {
for _, drive := range driveMap {
if drive.Name != k {
continue
}
fmt.Printf("%s%s%s\n", drive.Id, sep, drive.Name)
}
}

return nil, nil
default:
return nil, fs.ErrorCommandNotFound
}
Expand Down

0 comments on commit 2191489

Please sign in to comment.