github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/config/rc.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ncw/rclone/fs"
     7  	"github.com/ncw/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  - name - name of remote to get
    41  
    42  See the [config dump command](/commands/rclone_config_dump/) command for more information on the above.
    43  `,
    44  	})
    45  }
    46  
    47  // Return the config file get
    48  func rcGet(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    49  	name, err := in.GetString("name")
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return DumpRcRemote(name), nil
    54  }
    55  
    56  func init() {
    57  	rc.Add(rc.Call{
    58  		Path:         "config/listremotes",
    59  		Fn:           rcListRemotes,
    60  		Title:        "Lists the remotes in the config file.",
    61  		AuthRequired: true,
    62  		Help: `
    63  Returns
    64  - remotes - array of remote names
    65  
    66  See the [listremotes command](/commands/rclone_listremotes/) command for more information on the above.
    67  `,
    68  	})
    69  }
    70  
    71  // Return the a list of remotes in the config file
    72  func rcListRemotes(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    73  	var remotes = []string{}
    74  	for _, remote := range getConfigData().GetSectionList() {
    75  		remotes = append(remotes, remote)
    76  	}
    77  	out = rc.Params{
    78  		"remotes": remotes,
    79  	}
    80  	return out, nil
    81  }
    82  
    83  func init() {
    84  	rc.Add(rc.Call{
    85  		Path:         "config/providers",
    86  		Fn:           rcProviders,
    87  		Title:        "Shows how providers are configured in the config file.",
    88  		AuthRequired: true,
    89  		Help: `
    90  Returns a JSON object:
    91  - providers - array of objects
    92  
    93  See the [config providers command](/commands/rclone_config_providers/) command for more information on the above.
    94  `,
    95  	})
    96  }
    97  
    98  // Return the config file providers
    99  func rcProviders(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   100  	out = rc.Params{
   101  		"providers": fs.Registry,
   102  	}
   103  	return out, nil
   104  }
   105  
   106  func init() {
   107  	for _, name := range []string{"create", "update", "password"} {
   108  		name := name
   109  		extraHelp := ""
   110  		if name == "create" {
   111  			extraHelp = "- type - type of the new remote\n"
   112  		}
   113  		rc.Add(rc.Call{
   114  			Path:         "config/" + name,
   115  			AuthRequired: true,
   116  			Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
   117  				return rcConfig(ctx, in, name)
   118  			},
   119  			Title: name + " the config for a remote.",
   120  			Help: `This takes the following parameters
   121  
   122  - name - name of remote
   123  ` + extraHelp + `
   124  
   125  See the [config ` + name + ` command](/commands/rclone_config_` + name + `/) command for more information on the above.`,
   126  		})
   127  	}
   128  }
   129  
   130  // Manipulate the config file
   131  func rcConfig(ctx context.Context, in rc.Params, what string) (out rc.Params, err error) {
   132  	name, err := in.GetString("name")
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	parameters := rc.Params{}
   137  	err = in.GetStruct("parameters", &parameters)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	switch what {
   142  	case "create":
   143  		remoteType, err := in.GetString("type")
   144  		if err != nil {
   145  			return nil, err
   146  		}
   147  		return nil, CreateRemote(name, remoteType, parameters)
   148  	case "update":
   149  		return nil, UpdateRemote(name, parameters)
   150  	case "password":
   151  		return nil, PasswordRemote(name, parameters)
   152  	}
   153  	panic("unknown rcConfig type")
   154  }
   155  
   156  func init() {
   157  	rc.Add(rc.Call{
   158  		Path:         "config/delete",
   159  		Fn:           rcDelete,
   160  		Title:        "Delete a remote in the config file.",
   161  		AuthRequired: true,
   162  		Help: `
   163  Parameters:
   164  - name - name of remote to delete
   165  
   166  See the [config delete command](/commands/rclone_config_delete/) command for more information on the above.
   167  `,
   168  	})
   169  }
   170  
   171  // Return the config file delete
   172  func rcDelete(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   173  	name, err := in.GetString("name")
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	DeleteRemote(name)
   178  	return nil, nil
   179  }