2016-08-05 18:12:27 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-11-04 19:23:12 +01:00
|
|
|
"errors"
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/ncw/rclone/cmd"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
2018-11-04 19:23:12 +01:00
|
|
|
"github.com/ncw/rclone/fs/rc"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-10-14 12:50:41 +02:00
|
|
|
cmd.Root.AddCommand(configCommand)
|
|
|
|
configCommand.AddCommand(configEditCommand)
|
|
|
|
configCommand.AddCommand(configFileCommand)
|
|
|
|
configCommand.AddCommand(configShowCommand)
|
|
|
|
configCommand.AddCommand(configDumpCommand)
|
|
|
|
configCommand.AddCommand(configProvidersCommand)
|
|
|
|
configCommand.AddCommand(configCreateCommand)
|
|
|
|
configCommand.AddCommand(configUpdateCommand)
|
|
|
|
configCommand.AddCommand(configDeleteCommand)
|
2017-11-15 15:44:45 +01:00
|
|
|
configCommand.AddCommand(configPasswordCommand)
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 12:50:41 +02:00
|
|
|
var configCommand = &cobra.Command{
|
|
|
|
Use: "config",
|
2016-08-05 18:12:27 +02:00
|
|
|
Short: `Enter an interactive configuration session.`,
|
2017-10-14 12:50:41 +02:00
|
|
|
Long: `Enter an interactive configuration session where you can setup new
|
|
|
|
remotes and manage existing ones. You may also set or remove a
|
|
|
|
password to protect your configuration.
|
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-01-12 17:30:54 +01:00
|
|
|
config.EditConfig()
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
2017-09-19 17:59:19 +02:00
|
|
|
|
2017-10-14 12:50:41 +02:00
|
|
|
var configEditCommand = &cobra.Command{
|
|
|
|
Use: "edit",
|
|
|
|
Short: configCommand.Short,
|
|
|
|
Long: configCommand.Long,
|
|
|
|
Run: configCommand.Run,
|
|
|
|
}
|
2017-09-19 17:59:19 +02:00
|
|
|
|
2017-10-14 12:50:41 +02:00
|
|
|
var configFileCommand = &cobra.Command{
|
|
|
|
Use: "file",
|
|
|
|
Short: `Show path of configuration file in use.`,
|
2016-08-05 18:12:27 +02:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2017-10-14 12:50:41 +02:00
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-01-12 17:30:54 +01:00
|
|
|
config.ShowConfigLocation()
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configShowCommand = &cobra.Command{
|
|
|
|
Use: "show [<remote>]",
|
|
|
|
Short: `Print (decrypted) config file, or the config for a single remote.`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
2017-09-19 17:59:19 +02:00
|
|
|
if len(args) == 0 {
|
2018-01-12 17:30:54 +01:00
|
|
|
config.ShowConfig()
|
2017-10-14 12:50:41 +02:00
|
|
|
} else {
|
2018-01-12 17:30:54 +01:00
|
|
|
config.ShowRemote(args[0])
|
2017-09-19 17:59:19 +02:00
|
|
|
}
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configDumpCommand = &cobra.Command{
|
|
|
|
Use: "dump",
|
|
|
|
Short: `Dump the config file as JSON.`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-01-12 17:30:54 +01:00
|
|
|
return config.Dump()
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configProvidersCommand = &cobra.Command{
|
|
|
|
Use: "providers",
|
|
|
|
Short: `List in JSON format all the providers and options.`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-01-12 17:30:54 +01:00
|
|
|
return config.JSONListProviders()
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configCreateCommand = &cobra.Command{
|
|
|
|
Use: "create <name> <type> [<key> <value>]*",
|
|
|
|
Short: `Create a new remote with name, type and options.`,
|
|
|
|
Long: `
|
|
|
|
Create a new remote of <name> with <type> and options. The options
|
|
|
|
should be passed in in pairs of <key> <value>.
|
|
|
|
|
|
|
|
For example to make a swift remote of name myremote using auto config
|
|
|
|
you would do:
|
|
|
|
|
|
|
|
rclone config create myremote swift env_auth true
|
2019-01-17 16:01:13 +01:00
|
|
|
|
|
|
|
Note that if the config process would normally ask a question the
|
|
|
|
default is taken. Each time that happens rclone will print a message
|
|
|
|
saying how to affect the value taken.
|
|
|
|
|
2019-06-10 18:58:47 +02:00
|
|
|
If any of the parameters passed is a password field, then rclone will
|
|
|
|
automatically obscure them before putting them in the config file.
|
|
|
|
|
2019-01-17 16:01:13 +01:00
|
|
|
So for example if you wanted to configure a Google Drive remote but
|
|
|
|
using remote authorization you would do this:
|
|
|
|
|
|
|
|
rclone config create mydrive drive config_is_local false
|
2017-10-14 12:50:41 +02:00
|
|
|
`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(2, 256, command, args)
|
2018-11-04 19:23:12 +01:00
|
|
|
in, err := argsToMap(args[2:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.CreateRemote(args[0], args[1], in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.ShowRemote(args[0])
|
|
|
|
return nil
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configUpdateCommand = &cobra.Command{
|
|
|
|
Use: "update <name> [<key> <value>]+",
|
|
|
|
Short: `Update options in an existing remote.`,
|
|
|
|
Long: `
|
|
|
|
Update an existing remote's options. The options should be passed in
|
|
|
|
in pairs of <key> <value>.
|
|
|
|
|
2019-06-10 18:58:47 +02:00
|
|
|
For example to update the env_auth field of a remote of name myremote
|
|
|
|
you would do:
|
2017-10-14 12:50:41 +02:00
|
|
|
|
|
|
|
rclone config update myremote swift env_auth true
|
2019-01-17 16:01:13 +01:00
|
|
|
|
2019-06-10 18:58:47 +02:00
|
|
|
If any of the parameters passed is a password field, then rclone will
|
|
|
|
automatically obscure them before putting them in the config file.
|
|
|
|
|
2019-01-17 16:01:13 +01:00
|
|
|
If the remote uses oauth the token will be updated, if you don't
|
|
|
|
require this add an extra parameter thus:
|
|
|
|
|
|
|
|
rclone config update myremote swift env_auth true config_refresh_token false
|
2017-10-14 12:50:41 +02:00
|
|
|
`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(3, 256, command, args)
|
2018-11-04 19:23:12 +01:00
|
|
|
in, err := argsToMap(args[1:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.UpdateRemote(args[0], in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.ShowRemote(args[0])
|
|
|
|
return nil
|
2017-10-14 12:50:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var configDeleteCommand = &cobra.Command{
|
|
|
|
Use: "delete <name>",
|
|
|
|
Short: `Delete an existing remote <name>.`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
2018-01-12 17:30:54 +01:00
|
|
|
config.DeleteRemote(args[0])
|
2016-08-05 18:12:27 +02:00
|
|
|
},
|
|
|
|
}
|
2017-11-15 15:44:45 +01:00
|
|
|
|
|
|
|
var configPasswordCommand = &cobra.Command{
|
|
|
|
Use: "password <name> [<key> <value>]+",
|
|
|
|
Short: `Update password in an existing remote.`,
|
|
|
|
Long: `
|
|
|
|
Update an existing remote's password. The password
|
|
|
|
should be passed in in pairs of <key> <value>.
|
|
|
|
|
|
|
|
For example to set password of a remote of name myremote you would do:
|
|
|
|
|
|
|
|
rclone config password myremote fieldname mypassword
|
2019-06-10 18:58:47 +02:00
|
|
|
|
|
|
|
This command is obsolete now that "config update" and "config create"
|
|
|
|
both support obscuring passwords directly.
|
2017-11-15 15:44:45 +01:00
|
|
|
`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(3, 256, command, args)
|
2018-11-04 19:23:12 +01:00
|
|
|
in, err := argsToMap(args[1:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.PasswordRemote(args[0], in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.ShowRemote(args[0])
|
|
|
|
return nil
|
2017-11-15 15:44:45 +01:00
|
|
|
},
|
|
|
|
}
|
2018-11-04 19:23:12 +01:00
|
|
|
|
|
|
|
// This takes a list of arguments in key value key value form and
|
|
|
|
// converts it into a map
|
|
|
|
func argsToMap(args []string) (out rc.Params, err error) {
|
|
|
|
if len(args)%2 != 0 {
|
|
|
|
return nil, errors.New("found key without value")
|
|
|
|
}
|
|
|
|
out = rc.Params{}
|
|
|
|
// Set the config
|
|
|
|
for i := 0; i < len(args); i += 2 {
|
|
|
|
out[args[i]] = args[i+1]
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|