github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/config/rc.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/rclone/rclone/fs"
     7  	"github.com/rclone/rclone/fs/rc"
     8  )
     9  
    10  func init() {
    11  	rc.Add(rc.Call{
    12  		Path:         "config/dump",
    13  		Fn:           rcDump,
    14  		Title:        "Dumps the config file.",
    15  		AuthRequired: true,
    16  		Help: `
    17  Returns a JSON object:
    18  - key: value
    19  
    20  Where keys are remote names and values are the config parameters.
    21  
    22  See the [config dump command](/commands/rclone_config_dump/) command for more information on the above.
    23  `,
    24  	})
    25  }
    26  
    27  // Return the config file dump
    28  func rcDump(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    29  	return DumpRcBlob(), nil
    30  }
    31  
    32  func init() {
    33  	rc.Add(rc.Call{
    34  		Path:         "config/get",
    35  		Fn:           rcGet,
    36  		Title:        "Get a remote in the config file.",
    37  		AuthRequired: true,
    38  		Help: `
    39  Parameters:
    40  
    41  - name - name of remote to get
    42  
    43  See the [config dump command](/commands/rclone_config_dump/) command for more information on the above.
    44  `,
    45  	})
    46  }
    47  
    48  // Return the config file get
    49  func rcGet(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    50  	name, err := in.GetString("name")
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return DumpRcRemote(name), nil
    55  }
    56  
    57  func init() {
    58  	rc.Add(rc.Call{
    59  		Path:         "config/listremotes",
    60  		Fn:           rcListRemotes,
    61  		Title:        "Lists the remotes in the config file.",
    62  		AuthRequired: true,
    63  		Help: `
    64  Returns
    65  - remotes - array of remote names
    66  
    67  See the [listremotes command](/commands/rclone_listremotes/) command for more information on the above.
    68  `,
    69  	})
    70  }
    71  
    72  // Return the a list of remotes in the config file
    73  func rcListRemotes(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    74  	var remotes = []string{}
    75  	for _, remote := range getConfigData().GetSectionList() {
    76  		remotes = append(remotes, remote)
    77  	}
    78  	out = rc.Params{
    79  		"remotes": remotes,
    80  	}
    81  	return out, nil
    82  }
    83  
    84  func init() {
    85  	rc.Add(rc.Call{
    86  		Path:         "config/providers",
    87  		Fn:           rcProviders,
    88  		Title:        "Shows how providers are configured in the config file.",
    89  		AuthRequired: true,
    90  		Help: `
    91  Returns a JSON object:
    92  - providers - array of objects
    93  
    94  See the [config providers command](/commands/rclone_config_providers/) command for more information on the above.
    95  `,
    96  	})
    97  }
    98  
    99  // Return the config file providers
   100  func rcProviders(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   101  	out = rc.Params{
   102  		"providers": fs.Registry,
   103  	}
   104  	return out, nil
   105  }
   106  
   107  func init() {
   108  	for _, name := range []string{"create", "update", "password"} {
   109  		name := name
   110  		extraHelp := ""
   111  		if name == "create" {
   112  			extraHelp = "- type - type of the new remote\n"
   113  		}
   114  		if name == "create" || name == "update" {
   115  			extraHelp += "- obscure - optional bool - forces obscuring of passwords\n"
   116  			extraHelp += "- noObscure - optional bool - forces passwords not to be obscured\n"
   117  		}
   118  		rc.Add(rc.Call{
   119  			Path:         "config/" + name,
   120  			AuthRequired: true,
   121  			Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
   122  				return rcConfig(ctx, in, name)
   123  			},
   124  			Title: name + " the config for a remote.",
   125  			Help: `This takes the following parameters
   126  
   127  - name - name of remote
   128  - parameters - a map of \{ "key": "value" \} pairs
   129  ` + extraHelp + `
   130  
   131  See the [config ` + name + ` command](/commands/rclone_config_` + name + `/) command for more information on the above.`,
   132  		})
   133  	}
   134  }
   135  
   136  // Manipulate the config file
   137  func rcConfig(ctx context.Context, in rc.Params, what string) (out rc.Params, err error) {
   138  	name, err := in.GetString("name")
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	parameters := rc.Params{}
   143  	err = in.GetStruct("parameters", &parameters)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  	doObscure, _ := in.GetBool("obscure")
   148  	noObscure, _ := in.GetBool("noObscure")
   149  	switch what {
   150  	case "create":
   151  		remoteType, err := in.GetString("type")
   152  		if err != nil {
   153  			return nil, err
   154  		}
   155  		return nil, CreateRemote(name, remoteType, parameters, doObscure, noObscure)
   156  	case "update":
   157  		return nil, UpdateRemote(name, parameters, doObscure, noObscure)
   158  	case "password":
   159  		return nil, PasswordRemote(name, parameters)
   160  	}
   161  	panic("unknown rcConfig type")
   162  }
   163  
   164  func init() {
   165  	rc.Add(rc.Call{
   166  		Path:         "config/delete",
   167  		Fn:           rcDelete,
   168  		Title:        "Delete a remote in the config file.",
   169  		AuthRequired: true,
   170  		Help: `
   171  Parameters:
   172  
   173  - name - name of remote to delete
   174  
   175  See the [config delete command](/commands/rclone_config_delete/) command for more information on the above.
   176  `,
   177  	})
   178  }
   179  
   180  // Return the config file delete
   181  func rcDelete(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   182  	name, err := in.GetString("name")
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	DeleteRemote(name)
   187  	return nil, nil
   188  }