2022-08-28 13:21:57 +02:00
// Package config provides the config command.
2016-08-05 18:12:27 +02:00
package config
import (
2019-08-12 12:08:07 +02:00
"context"
"encoding/json"
2021-11-04 11:12:57 +01:00
"errors"
2019-08-12 12:08:07 +02:00
"fmt"
"os"
"sort"
2020-05-18 20:53:13 +02:00
"strings"
2018-11-04 19:23:12 +01:00
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/cmd"
2019-08-12 12:08:07 +02:00
"github.com/rclone/rclone/fs"
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/fs/config"
2019-10-11 17:55:04 +02:00
"github.com/rclone/rclone/fs/config/flags"
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/fs/rc"
2016-08-05 18:12:27 +02:00
"github.com/spf13/cobra"
2020-05-12 15:24:53 +02:00
"github.com/spf13/pflag"
2016-08-05 18:12:27 +02:00
)
func init ( ) {
2017-10-14 12:50:41 +02:00
cmd . Root . AddCommand ( configCommand )
configCommand . AddCommand ( configEditCommand )
configCommand . AddCommand ( configFileCommand )
2021-04-10 23:02:54 +02:00
configCommand . AddCommand ( configTouchCommand )
2021-09-10 14:42:57 +02:00
configCommand . AddCommand ( configPathsCommand )
2017-10-14 12:50:41 +02:00
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 )
2019-08-12 12:08:07 +02:00
configCommand . AddCommand ( configReconnectCommand )
configCommand . AddCommand ( configDisconnectCommand )
configCommand . AddCommand ( configUserInfoCommand )
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 .
` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2021-04-06 22:27:34 +02:00
RunE : func ( command * cobra . Command , args [ ] string ) error {
2017-10-14 12:50:41 +02:00
cmd . CheckArgs ( 0 , 0 , command , args )
2021-04-06 22:27:34 +02:00
return config . EditConfig ( context . Background ( ) )
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 ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
Run : configCommand . Run ,
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 configFileCommand = & cobra . Command {
Use : "file" ,
Short : ` Show path of configuration file in use. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.38" ,
} ,
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
} ,
}
2021-04-10 23:02:54 +02:00
var configTouchCommand = & cobra . Command {
Use : "touch" ,
Short : ` Ensure configuration file exists. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.56" ,
} ,
2021-04-10 23:02:54 +02:00
Run : func ( command * cobra . Command , args [ ] string ) {
cmd . CheckArgs ( 0 , 0 , command , args )
config . SaveConfig ( )
} ,
}
2021-09-10 14:42:57 +02:00
var configPathsCommand = & cobra . Command {
Use : "paths" ,
Short : ` Show paths used for configuration, cache, temp etc. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.57" ,
} ,
2021-09-10 14:42:57 +02:00
Run : func ( command * cobra . Command , args [ ] string ) {
cmd . CheckArgs ( 0 , 0 , command , args )
fmt . Printf ( "Config file: %s\n" , config . GetConfigPath ( ) )
2021-09-10 15:35:53 +02:00
fmt . Printf ( "Cache dir: %s\n" , config . GetCacheDir ( ) )
2021-09-10 14:42:57 +02:00
fmt . Printf ( "Temp dir: %s\n" , os . TempDir ( ) )
} ,
}
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. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.38" ,
} ,
2017-10-14 12:50:41 +02:00
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 {
2020-05-18 20:53:13 +02:00
name := strings . TrimRight ( args [ 0 ] , ":" )
config . ShowRemote ( name )
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. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2017-10-14 12:50:41 +02:00
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. ` ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2017-10-14 12:50:41 +02:00
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
} ,
}
2021-05-05 12:45:30 +02:00
var updateRemoteOpt config . UpdateRemoteOpt
2020-05-12 15:24:53 +02:00
2021-05-05 12:45:30 +02:00
var configPasswordHelp = strings . ReplaceAll ( `
2021-05-05 13:22:39 +02:00
Note that if the config process would normally ask a question the
default is taken ( unless | -- non - interactive | is used ) . Each time
that happens rclone will print or DEBUG a message saying how to
affect the value taken .
2020-05-12 15:24:53 +02:00
If any of the parameters passed is a password field , then rclone will
automatically obscure them if they aren ' t already obscured before
putting them in the config file .
* * NB * * If the password parameter is 22 characters or longer and
consists only of base64 characters then rclone can get confused about
whether the password is already obscured or not and put unobscured
passwords into the config file . If you want to be 100 % certain that
2021-05-05 12:45:30 +02:00
the passwords get obscured then use the | -- obscure | flag , or if you
2020-05-12 15:24:53 +02:00
are 100 % certain you are already passing obscured passwords then use
2021-05-05 12:45:30 +02:00
| -- no - obscure | . You can also set obscured passwords using the
| rclone config password | command .
The flag | -- non - interactive | is for use by applications that wish to
2022-08-14 04:56:32 +02:00
configure rclone themselves , rather than using rclone ' s text based
2021-05-05 12:45:30 +02:00
configuration questions . If this flag is set , and rclone needs to ask
the user a question , a JSON blob will be returned with the question in
it .
This will look something like ( some irrelevant detail removed ) :
2021-05-17 11:28:56 +02:00
|| |
2021-05-05 12:45:30 +02:00
{
"State" : "*oauth-islocal,teamdrive,," ,
"Option" : {
"Name" : "config_is_local" ,
2022-12-07 20:44:28 +01:00
"Help" : "Use web browser to automatically authenticate rclone with remote?\n * Say Y if the machine running rclone has a web browser you can use\n * Say N if running rclone on a (remote) machine without web browser access\nIf not sure try Y. If Y failed, try N.\n" ,
2021-05-05 12:45:30 +02:00
"Default" : true ,
"Examples" : [
{
"Value" : "true" ,
"Help" : "Yes"
} ,
{
"Value" : "false" ,
"Help" : "No"
}
] ,
"Required" : false ,
"IsPassword" : false ,
2021-05-17 11:28:56 +02:00
"Type" : "bool" ,
"Exclusive" : true ,
2021-05-05 12:45:30 +02:00
} ,
"Error" : "" ,
}
2021-05-17 11:28:56 +02:00
|| |
2020-05-12 15:24:53 +02:00
2021-05-05 12:45:30 +02:00
The format of | Option | is the same as returned by | rclone config
providers | . The question should be asked to the user and returned to
2021-05-11 14:36:34 +02:00
rclone as the | -- result | option along with the | -- state | parameter .
2021-05-05 12:45:30 +02:00
2021-05-17 11:28:56 +02:00
The keys of | Option | are used as follows :
- | Name | - name of variable - show to user
- | Help | - help text . Hard wrapped at 80 chars . Any URLs should be clicky .
- | Default | - default value - return this if the user just wants the default .
- | Examples | - the user should be able to choose one of these
- | Required | - the value should be non - empty
- | IsPassword | - the value is a password and should be edited as such
- | Type | - type of value , eg | bool | , | string | , | int | and others
- | Exclusive | - if set no free - form entry allowed only the | Examples |
- Irrelevant keys | Provider | , | ShortOpt | , | Hide | , | NoPrefix | , | Advanced |
2021-05-05 12:45:30 +02:00
If | Error | is set then it should be shown to the user at the same
time as the question .
2021-05-11 14:36:34 +02:00
rclone config update name -- continue -- state "*oauth-islocal,teamdrive,," -- result "true"
2021-05-05 12:45:30 +02:00
Note that when using | -- continue | all passwords should be passed in
2021-05-09 17:03:18 +02:00
the clear ( not obscured ) . Any default config values should be passed
in with each invocation of | -- continue | .
2021-05-05 12:45:30 +02:00
At the end of the non interactive process , rclone will return a result
with | State | as empty string .
2021-05-09 17:03:18 +02:00
If | -- all | is passed then rclone will ask all the config questions ,
not just the post config questions . Any parameters are used as
defaults for questions as usual .
2021-05-17 11:28:56 +02:00
Note that | bin / config . py | in the rclone source implements this protocol
as a readable demonstration .
2021-05-05 12:45:30 +02:00
` , "|", " ` " )
2017-10-14 12:50:41 +02:00
var configCreateCommand = & cobra . Command {
2021-08-26 14:51:00 +02:00
Use : "create name type [key value]*" ,
2017-10-14 12:50:41 +02:00
Short : ` Create a new remote with name, type and options. ` ,
2021-05-05 13:22:39 +02:00
Long : strings . ReplaceAll ( `
Create a new remote of | name | with | type | and options . The options
should be passed in pairs of | key | | value | or as | key = value | .
2017-10-14 12:50:41 +02:00
2021-11-04 12:50:43 +01:00
For example , to make a swift remote of name myremote using auto config
2017-10-14 12:50:41 +02:00
you would do :
rclone config create myremote swift env_auth true
2021-05-05 13:22:39 +02:00
rclone config create myremote swift env_auth = true
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 :
2021-05-05 13:22:39 +02:00
rclone config create mydrive drive config_is_local = false
` , "|", " ` " ) + configPasswordHelp ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
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
}
2021-05-05 12:45:30 +02:00
return doConfig ( args [ 0 ] , in , func ( opts config . UpdateRemoteOpt ) ( * fs . ConfigOut , error ) {
return config . CreateRemote ( context . Background ( ) , args [ 0 ] , args [ 1 ] , in , opts )
} )
} ,
}
func doConfig ( name string , in rc . Params , do func ( config . UpdateRemoteOpt ) ( * fs . ConfigOut , error ) ) error {
out , err := do ( updateRemoteOpt )
if err != nil {
return err
}
if ! ( updateRemoteOpt . NonInteractive || updateRemoteOpt . Continue ) {
config . ShowRemote ( name )
} else {
if out == nil {
out = & fs . ConfigOut { }
}
outBytes , err := json . MarshalIndent ( out , "" , "\t" )
2018-11-04 19:23:12 +01:00
if err != nil {
return err
}
2021-05-05 12:45:30 +02:00
_ , _ = os . Stdout . Write ( outBytes )
_ , _ = os . Stdout . WriteString ( "\n" )
}
return nil
2017-10-14 12:50:41 +02:00
}
2020-05-12 15:24:53 +02:00
func init ( ) {
for _ , cmdFlags := range [ ] * pflag . FlagSet { configCreateCommand . Flags ( ) , configUpdateCommand . Flags ( ) } {
2021-08-16 11:30:01 +02:00
flags . BoolVarP ( cmdFlags , & updateRemoteOpt . Obscure , "obscure" , "" , false , "Force any passwords to be obscured" )
flags . BoolVarP ( cmdFlags , & updateRemoteOpt . NoObscure , "no-obscure" , "" , false , "Force any passwords not to be obscured" )
flags . BoolVarP ( cmdFlags , & updateRemoteOpt . NonInteractive , "non-interactive" , "" , false , "Don't interact with user and return questions" )
flags . BoolVarP ( cmdFlags , & updateRemoteOpt . Continue , "continue" , "" , false , "Continue the configuration process with an answer" )
flags . BoolVarP ( cmdFlags , & updateRemoteOpt . All , "all" , "" , false , "Ask the full set of config questions" )
flags . StringVarP ( cmdFlags , & updateRemoteOpt . State , "state" , "" , "" , "State - use with --continue" )
flags . StringVarP ( cmdFlags , & updateRemoteOpt . Result , "result" , "" , "" , "Result - use with --continue" )
2020-05-12 15:24:53 +02:00
}
}
2017-10-14 12:50:41 +02:00
var configUpdateCommand = & cobra . Command {
2021-08-26 14:51:00 +02:00
Use : "update name [key value]+" ,
2017-10-14 12:50:41 +02:00
Short : ` Update options in an existing remote. ` ,
2021-05-05 13:22:39 +02:00
Long : strings . ReplaceAll ( `
2017-10-14 12:50:41 +02:00
Update an existing remote ' s options . The options should be passed in
2021-05-05 13:22:39 +02:00
pairs of | key | | value | or as | key = value | .
2017-10-14 12:50:41 +02:00
2021-11-04 12:50:43 +01:00
For example , to update the env_auth field of a remote of name myremote
2019-06-10 18:58:47 +02:00
you would do :
2017-10-14 12:50:41 +02:00
2021-05-05 13:22:39 +02:00
rclone config update myremote env_auth true
rclone config update myremote env_auth = true
2020-02-10 16:07:40 +01:00
If the remote uses OAuth the token will be updated , if you don ' t
2019-01-17 16:01:13 +01:00
require this add an extra parameter thus :
2021-06-08 08:41:32 +02:00
rclone config update myremote env_auth = true config_refresh_token = false
2021-05-05 13:22:39 +02:00
` , "|", " ` " ) + configPasswordHelp ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2017-10-14 12:50:41 +02:00
RunE : func ( command * cobra . Command , args [ ] string ) error {
2021-05-05 13:22:39 +02:00
cmd . CheckArgs ( 1 , 256 , command , args )
2018-11-04 19:23:12 +01:00
in , err := argsToMap ( args [ 1 : ] )
if err != nil {
return err
}
2021-05-05 12:45:30 +02:00
return doConfig ( args [ 0 ] , in , func ( opts config . UpdateRemoteOpt ) ( * fs . ConfigOut , error ) {
return config . UpdateRemote ( context . Background ( ) , args [ 0 ] , in , opts )
} )
2017-10-14 12:50:41 +02:00
} ,
}
var configDeleteCommand = & cobra . Command {
2021-08-26 14:51:00 +02:00
Use : "delete name" ,
Short : "Delete an existing remote." ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2017-10-14 12:50:41 +02:00
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 {
2021-08-26 14:51:00 +02:00
Use : "password name [key value]+" ,
2017-11-15 15:44:45 +01:00
Short : ` Update password in an existing remote. ` ,
2021-05-05 13:22:39 +02:00
Long : strings . ReplaceAll ( `
2017-11-15 15:44:45 +01:00
Update an existing remote ' s password . The password
2021-05-05 13:22:39 +02:00
should be passed in pairs of | key | | password | or as | key = password | .
The | password | should be passed in in clear ( unobscured ) .
2017-11-15 15:44:45 +01:00
2021-11-04 12:50:43 +01:00
For example , to set password of a remote of name myremote you would do :
2017-11-15 15:44:45 +01:00
rclone config password myremote fieldname mypassword
2021-05-05 13:22:39 +02:00
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 .
2021-05-05 13:22:39 +02:00
` , "|", " ` " ) ,
2022-11-26 23:40:49 +01:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.39" ,
} ,
2017-11-15 15:44:45 +01:00
RunE : func ( command * cobra . Command , args [ ] string ) error {
2021-05-05 13:22:39 +02:00
cmd . CheckArgs ( 1 , 256 , command , args )
2018-11-04 19:23:12 +01:00
in , err := argsToMap ( args [ 1 : ] )
if err != nil {
return err
}
2020-11-05 19:02:26 +01:00
err = config . PasswordRemote ( context . Background ( ) , args [ 0 ] , in )
2018-11-04 19:23:12 +01:00
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
2021-05-05 13:22:39 +02:00
// This takes a list of arguments in key value key value form, or
// key=value key=value form and converts it into a map
2018-11-04 19:23:12 +01:00
func argsToMap ( args [ ] string ) ( out rc . Params , err error ) {
out = rc . Params { }
2021-05-05 13:22:39 +02:00
for i := 0 ; i < len ( args ) ; i ++ {
key := args [ i ]
equals := strings . IndexRune ( key , '=' )
var value string
if equals >= 0 {
key , value = key [ : equals ] , key [ equals + 1 : ]
} else {
i ++
if i >= len ( args ) {
return nil , errors . New ( "found key without value" )
}
value = args [ i ]
}
out [ key ] = value
2018-11-04 19:23:12 +01:00
}
return out , nil
}
2019-08-12 12:08:07 +02:00
var configReconnectCommand = & cobra . Command {
Use : "reconnect remote:" ,
Short : ` Re-authenticates user with remote. ` ,
Long : `
This reconnects remote : passed in to the cloud storage system .
To disconnect the remote use "rclone config disconnect" .
This normally means going through the interactive oauth flow again .
` ,
RunE : func ( command * cobra . Command , args [ ] string ) error {
2020-11-05 19:02:26 +01:00
ctx := context . Background ( )
2019-08-12 12:08:07 +02:00
cmd . CheckArgs ( 1 , 1 , command , args )
2021-04-29 10:28:18 +02:00
fsInfo , configName , _ , m , err := fs . ConfigFs ( args [ 0 ] )
2019-08-12 12:08:07 +02:00
if err != nil {
return err
}
2021-04-29 10:28:18 +02:00
return config . PostConfig ( ctx , configName , m , fsInfo )
2019-08-12 12:08:07 +02:00
} ,
}
var configDisconnectCommand = & cobra . Command {
Use : "disconnect remote:" ,
Short : ` Disconnects user from remote ` ,
Long : `
This disconnects the remote : passed in to the cloud storage system .
This normally means revoking the oauth token .
To reconnect use "rclone config reconnect" .
` ,
RunE : func ( command * cobra . Command , args [ ] string ) error {
cmd . CheckArgs ( 1 , 1 , command , args )
f := cmd . NewFsSrc ( args )
doDisconnect := f . Features ( ) . Disconnect
if doDisconnect == nil {
2021-11-04 11:12:57 +01:00
return fmt . Errorf ( "%v doesn't support Disconnect" , f )
2019-08-12 12:08:07 +02:00
}
err := doDisconnect ( context . Background ( ) )
if err != nil {
2022-06-08 22:54:39 +02:00
return fmt . Errorf ( "disconnect call failed: %w" , err )
2019-08-12 12:08:07 +02:00
}
return nil
} ,
}
var (
jsonOutput bool
)
func init ( ) {
2019-10-11 17:55:04 +02:00
flags . BoolVarP ( configUserInfoCommand . Flags ( ) , & jsonOutput , "json" , "" , false , "Format output as JSON" )
2019-08-12 12:08:07 +02:00
}
var configUserInfoCommand = & cobra . Command {
Use : "userinfo remote:" ,
Short : ` Prints info about logged in user of remote. ` ,
Long : `
This prints the details of the person logged in to the cloud storage
system .
` ,
RunE : func ( command * cobra . Command , args [ ] string ) error {
cmd . CheckArgs ( 1 , 1 , command , args )
f := cmd . NewFsSrc ( args )
doUserInfo := f . Features ( ) . UserInfo
if doUserInfo == nil {
2021-11-04 11:12:57 +01:00
return fmt . Errorf ( "%v doesn't support UserInfo" , f )
2019-08-12 12:08:07 +02:00
}
u , err := doUserInfo ( context . Background ( ) )
if err != nil {
2021-11-04 11:12:57 +01:00
return fmt . Errorf ( "UserInfo call failed: %w" , err )
2019-08-12 12:08:07 +02:00
}
if jsonOutput {
out := json . NewEncoder ( os . Stdout )
out . SetIndent ( "" , "\t" )
return out . Encode ( u )
}
var keys [ ] string
var maxKeyLen int
for key := range u {
keys = append ( keys , key )
if len ( key ) > maxKeyLen {
maxKeyLen = len ( key )
}
}
sort . Strings ( keys )
for _ , key := range keys {
fmt . Printf ( "%*s: %s\n" , maxKeyLen , key , u [ key ] )
}
return nil
} ,
}