github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/listremotes/listremotes.go (about)

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