From ace1e2189429d776f028dd2a5dc7776ed774c598 Mon Sep 17 00:00:00 2001
From: Nick Craig-Wood <nick@craig-wood.com>
Date: Sat, 8 Oct 2016 14:24:37 +0100
Subject: [PATCH] Add listremotes command - fixes #558

---
 cmd/all/all.go                 |  1 +
 cmd/listremotes/listremotes.go | 49 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 cmd/listremotes/listremotes.go

diff --git a/cmd/all/all.go b/cmd/all/all.go
index 51f427fee..320d5f2fa 100644
--- a/cmd/all/all.go
+++ b/cmd/all/all.go
@@ -14,6 +14,7 @@ import (
 	_ "github.com/ncw/rclone/cmd/delete"
 	_ "github.com/ncw/rclone/cmd/genautocomplete"
 	_ "github.com/ncw/rclone/cmd/gendocs"
+	_ "github.com/ncw/rclone/cmd/listremotes"
 	_ "github.com/ncw/rclone/cmd/ls"
 	_ "github.com/ncw/rclone/cmd/lsd"
 	_ "github.com/ncw/rclone/cmd/lsl"
diff --git a/cmd/listremotes/listremotes.go b/cmd/listremotes/listremotes.go
new file mode 100644
index 000000000..ab06ba2cd
--- /dev/null
+++ b/cmd/listremotes/listremotes.go
@@ -0,0 +1,49 @@
+package ls
+
+import (
+	"fmt"
+	"sort"
+
+	"github.com/ncw/rclone/cmd"
+	"github.com/ncw/rclone/fs"
+	"github.com/spf13/cobra"
+)
+
+// Globals
+var (
+	listLong bool
+)
+
+func init() {
+	cmd.Root.AddCommand(listremotesCmd)
+	listremotesCmd.Flags().BoolVarP(&listLong, "long", "l", listLong, "Show the type as well as names.")
+}
+
+var listremotesCmd = &cobra.Command{
+	Use:   "listremotes",
+	Short: `List all the remotes in the config file.`,
+	Long: `
+rclone listremotes lists all the available remotes from the config file.
+
+When uses with the -l flag it lists the types too.
+`,
+	Run: func(command *cobra.Command, args []string) {
+		cmd.CheckArgs(0, 0, command, args)
+		remotes := fs.ConfigFile.GetSectionList()
+		sort.Strings(remotes)
+		maxlen := 1
+		for _, remote := range remotes {
+			if len(remote) > maxlen {
+				maxlen = len(remote)
+			}
+		}
+		for _, remote := range remotes {
+			if listLong {
+				remoteType := fs.ConfigFile.MustValue(remote, "type", "UNKNOWN")
+				fmt.Printf("%-*s %s\n", maxlen+1, remote+":", remoteType)
+			} else {
+				fmt.Printf("%s:\n", remote)
+			}
+		}
+	},
+}