listremotes.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Package ls provides the ls command.
  2. package ls
  3. import (
  4. "fmt"
  5. "sort"
  6. "github.com/rclone/rclone/cmd"
  7. "github.com/rclone/rclone/fs/config"
  8. "github.com/rclone/rclone/fs/config/flags"
  9. "github.com/spf13/cobra"
  10. )
  11. // Globals
  12. var (
  13. listLong bool
  14. )
  15. func init() {
  16. cmd.Root.AddCommand(commandDefinition)
  17. cmdFlags := commandDefinition.Flags()
  18. flags.BoolVarP(cmdFlags, &listLong, "long", "", listLong, "Show the type and the description as well as names", "")
  19. }
  20. var commandDefinition = &cobra.Command{
  21. Use: "listremotes",
  22. Short: `List all the remotes in the config file and defined in environment variables.`,
  23. Long: `
  24. rclone listremotes lists all the available remotes from the config file.
  25. When used with the ` + "`--long`" + ` flag it lists the types and the descriptions too.
  26. `,
  27. Annotations: map[string]string{
  28. "versionIntroduced": "v1.34",
  29. },
  30. Run: func(command *cobra.Command, args []string) {
  31. cmd.CheckArgs(0, 0, command, args)
  32. remotes := config.FileSections()
  33. sort.Strings(remotes)
  34. maxlen := 1
  35. maxlentype := 1
  36. for _, remote := range remotes {
  37. if len(remote) > maxlen {
  38. maxlen = len(remote)
  39. }
  40. t := config.FileGet(remote, "type")
  41. if len(t) > maxlentype {
  42. maxlentype = len(t)
  43. }
  44. }
  45. for _, remote := range remotes {
  46. if listLong {
  47. remoteType := config.FileGet(remote, "type")
  48. description := config.FileGet(remote, "description")
  49. fmt.Printf("%-*s %-*s %s\n", maxlen+1, remote+":", maxlentype+1, remoteType, description)
  50. } else {
  51. fmt.Printf("%s:\n", remote)
  52. }
  53. }
  54. },
  55. }