2022-08-28 19:21:57 +08:00
|
|
|
// Package ls provides the ls command.
|
2016-10-08 21:24:37 +08:00
|
|
|
package ls
|
|
|
|
|
|
|
|
import (
|
2023-11-04 22:49:15 +08:00
|
|
|
"encoding/json"
|
2016-10-08 21:24:37 +08:00
|
|
|
"fmt"
|
2023-11-04 22:49:15 +08:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2016-10-08 21:24:37 +08:00
|
|
|
"sort"
|
2023-11-04 22:49:15 +08:00
|
|
|
"strings"
|
2016-10-08 21:24:37 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2023-11-04 22:49:15 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/config"
|
2019-10-11 23:55:04 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2023-11-04 22:49:15 +08:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2016-10-08 21:24:37 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-11-04 22:49:15 +08:00
|
|
|
listLong bool
|
|
|
|
jsonOutput bool
|
|
|
|
filterName string
|
|
|
|
filterType string
|
|
|
|
filterSource string
|
|
|
|
filterDescription string
|
|
|
|
orderBy string
|
2016-10-08 21:24:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2024-08-09 18:09:32 +08:00
|
|
|
flags.BoolVarP(cmdFlags, &listLong, "long", "", false, "Show type and description in addition to name", "")
|
2023-11-04 22:49:15 +08:00
|
|
|
flags.StringVarP(cmdFlags, &filterName, "name", "", "", "Filter remotes by name", "")
|
|
|
|
flags.StringVarP(cmdFlags, &filterType, "type", "", "", "Filter remotes by type", "")
|
2024-08-09 18:09:32 +08:00
|
|
|
flags.StringVarP(cmdFlags, &filterSource, "source", "", "", "Filter remotes by source, e.g. 'file' or 'environment'", "")
|
|
|
|
flags.StringVarP(cmdFlags, &filterDescription, "description", "", "", "Filter remotes by description", "")
|
2023-11-04 22:49:15 +08:00
|
|
|
flags.StringVarP(cmdFlags, &orderBy, "order-by", "", "", "Instructions on how to order the result, e.g. 'type,name=descending'", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &jsonOutput, "json", "", false, "Format output as JSON", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// lessFn compares to remotes for order by
|
|
|
|
type lessFn func(a, b config.Remote) bool
|
|
|
|
|
|
|
|
// newLess returns a function for comparing remotes based on an order by string
|
|
|
|
func newLess(orderBy string) (less lessFn, err error) {
|
|
|
|
if orderBy == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(strings.ToLower(orderBy), ",")
|
|
|
|
n := len(parts)
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
fieldAndDirection := strings.SplitN(parts[i], "=", 2)
|
|
|
|
|
|
|
|
descending := false
|
|
|
|
if len(fieldAndDirection) > 1 {
|
|
|
|
switch fieldAndDirection[1] {
|
|
|
|
case "ascending", "asc":
|
|
|
|
case "descending", "desc":
|
|
|
|
descending = true
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown --order-by direction %q", fieldAndDirection[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var field func(o config.Remote) string
|
|
|
|
switch fieldAndDirection[0] {
|
|
|
|
case "name":
|
|
|
|
field = func(o config.Remote) string {
|
|
|
|
return o.Name
|
|
|
|
}
|
|
|
|
case "type":
|
|
|
|
field = func(o config.Remote) string {
|
|
|
|
return o.Type
|
|
|
|
}
|
|
|
|
case "source":
|
|
|
|
field = func(o config.Remote) string {
|
|
|
|
return o.Source
|
|
|
|
}
|
|
|
|
case "description":
|
|
|
|
field = func(o config.Remote) string {
|
|
|
|
return o.Description
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown --order-by field %q", fieldAndDirection[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
var thisLess lessFn
|
|
|
|
if descending {
|
|
|
|
thisLess = func(a, b config.Remote) bool {
|
|
|
|
return field(a) > field(b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
thisLess = func(a, b config.Remote) bool {
|
|
|
|
return field(a) < field(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == n-1 {
|
|
|
|
less = thisLess
|
|
|
|
} else {
|
|
|
|
nextLess := less
|
|
|
|
less = func(a, b config.Remote) bool {
|
|
|
|
if field(a) == field(b) {
|
|
|
|
return nextLess(a, b)
|
|
|
|
}
|
|
|
|
return thisLess(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return less, nil
|
2016-10-08 21:24:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2023-11-04 22:49:15 +08:00
|
|
|
Use: "listremotes [<filter>]",
|
2023-06-15 02:07:43 +08:00
|
|
|
Short: `List all the remotes in the config file and defined in environment variables.`,
|
2016-10-08 21:24:37 +08:00
|
|
|
Long: `
|
2023-11-04 22:49:15 +08:00
|
|
|
rclone listremotes lists all the available remotes from the config file,
|
|
|
|
or the remotes matching an optional filter.
|
|
|
|
|
|
|
|
Prints the result in human-readable format by default, and as a simple list of
|
|
|
|
remote names, or if used with flag ` + "`--long`" + ` a tabular format including
|
2024-08-09 18:09:32 +08:00
|
|
|
the remote names, types and descriptions. Using flag ` + "`--json`" + ` produces
|
|
|
|
machine-readable output instead, which always includes all attributes - including
|
|
|
|
the source (file or environment).
|
2016-10-08 21:24:37 +08:00
|
|
|
|
2023-11-04 22:49:15 +08:00
|
|
|
Result can be filtered by a filter argument which applies to all attributes,
|
|
|
|
and/or filter flags specific for each attribute. The values must be specified
|
|
|
|
according to regular rclone filtering pattern syntax.
|
2016-10-08 21:24:37 +08:00
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.34",
|
|
|
|
},
|
2023-11-04 22:49:15 +08:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
|
|
|
var filterDefault string
|
|
|
|
if len(args) > 0 {
|
|
|
|
filterDefault = args[0]
|
|
|
|
}
|
|
|
|
filters := make(map[string]*regexp.Regexp)
|
|
|
|
for k, v := range map[string]string{
|
|
|
|
"all": filterDefault,
|
|
|
|
"name": filterName,
|
|
|
|
"type": filterType,
|
|
|
|
"source": filterSource,
|
|
|
|
"description": filterDescription,
|
|
|
|
} {
|
|
|
|
if v != "" {
|
|
|
|
filterRe, err := filter.GlobStringToRegexp(v, false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %s filter argument: %w", k, err)
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "Filter for %s: %s", k, filterRe.String())
|
|
|
|
filters[k] = filterRe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remotes := config.GetRemotes()
|
|
|
|
maxName := 0
|
|
|
|
maxType := 0
|
|
|
|
i := 0
|
2016-10-08 21:24:37 +08:00
|
|
|
for _, remote := range remotes {
|
2023-11-04 22:49:15 +08:00
|
|
|
include := true
|
|
|
|
for k, v := range filters {
|
|
|
|
if k == "all" && !(v.MatchString(remote.Name) || v.MatchString(remote.Type) || v.MatchString(remote.Source) || v.MatchString(remote.Description)) {
|
|
|
|
include = false
|
|
|
|
} else if k == "name" && !v.MatchString(remote.Name) {
|
|
|
|
include = false
|
|
|
|
} else if k == "type" && !v.MatchString(remote.Type) {
|
|
|
|
include = false
|
|
|
|
} else if k == "source" && !v.MatchString(remote.Source) {
|
|
|
|
include = false
|
|
|
|
} else if k == "description" && !v.MatchString(remote.Description) {
|
|
|
|
include = false
|
|
|
|
}
|
2016-10-08 21:24:37 +08:00
|
|
|
}
|
2023-11-04 22:49:15 +08:00
|
|
|
if include {
|
|
|
|
if len(remote.Name) > maxName {
|
|
|
|
maxName = len(remote.Name)
|
|
|
|
}
|
|
|
|
if len(remote.Type) > maxType {
|
|
|
|
maxType = len(remote.Type)
|
|
|
|
}
|
|
|
|
remotes[i] = remote
|
|
|
|
i++
|
2023-12-03 00:27:55 +08:00
|
|
|
}
|
2016-10-08 21:24:37 +08:00
|
|
|
}
|
2023-11-04 22:49:15 +08:00
|
|
|
remotes = remotes[:i]
|
|
|
|
|
|
|
|
less, err := newLess(orderBy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if less != nil {
|
|
|
|
sliceLessFn := func(i, j int) bool {
|
|
|
|
return less(remotes[i], remotes[j])
|
|
|
|
}
|
|
|
|
sort.SliceStable(remotes, sliceLessFn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonOutput {
|
|
|
|
fmt.Println("[")
|
|
|
|
first := true
|
|
|
|
for _, remote := range remotes {
|
|
|
|
out, err := json.Marshal(remote)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal remote object: %w", err)
|
|
|
|
}
|
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
fmt.Print(",\n")
|
|
|
|
}
|
|
|
|
_, err = os.Stdout.Write(out)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write to output: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !first {
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
fmt.Println("]")
|
|
|
|
} else if listLong {
|
|
|
|
for _, remote := range remotes {
|
2024-08-09 18:09:32 +08:00
|
|
|
fmt.Printf("%-*s %-*s %s\n", maxName+1, remote.Name+":", maxType, remote.Type, remote.Description)
|
2023-11-04 22:49:15 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, remote := range remotes {
|
|
|
|
fmt.Printf("%s:\n", remote.Name)
|
2016-10-08 21:24:37 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-04 22:49:15 +08:00
|
|
|
return nil
|
2016-10-08 21:24:37 +08:00
|
|
|
},
|
|
|
|
}
|