github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/config/rc.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  
     8  	"github.com/rclone/rclone/fs"
     9  	"github.com/rclone/rclone/fs/rc"
    10  )
    11  
    12  func init() {
    13  	rc.Add(rc.Call{
    14  		Path:         "config/dump",
    15  		Fn:           rcDump,
    16  		Title:        "Dumps the config file.",
    17  		AuthRequired: true,
    18  		Help: `
    19  Returns a JSON object:
    20  - key: value
    21  
    22  Where keys are remote names and values are the config parameters.
    23  
    24  See the [config dump](/commands/rclone_config_dump/) command for more information on the above.
    25  `,
    26  	})
    27  }
    28  
    29  // Return the config file dump
    30  func rcDump(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    31  	return DumpRcBlob(), nil
    32  }
    33  
    34  func init() {
    35  	rc.Add(rc.Call{
    36  		Path:         "config/get",
    37  		Fn:           rcGet,
    38  		Title:        "Get a remote in the config file.",
    39  		AuthRequired: true,
    40  		Help: `
    41  Parameters:
    42  
    43  - name - name of remote to get
    44  
    45  See the [config dump](/commands/rclone_config_dump/) command for more information on the above.
    46  `,
    47  	})
    48  }
    49  
    50  // Return the config file get
    51  func rcGet(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    52  	name, err := in.GetString("name")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return DumpRcRemote(name), nil
    57  }
    58  
    59  func init() {
    60  	rc.Add(rc.Call{
    61  		Path:         "config/listremotes",
    62  		Fn:           rcListRemotes,
    63  		Title:        "Lists the remotes in the config file and defined in environment variables.",
    64  		AuthRequired: true,
    65  		Help: `
    66  Returns
    67  - remotes - array of remote names
    68  
    69  See the [listremotes](/commands/rclone_listremotes/) command for more information on the above.
    70  `,
    71  	})
    72  }
    73  
    74  // Return the a list of remotes in the config file
    75  // including any defined by environment variables.
    76  func rcListRemotes(ctx context.Context, in rc.Params) (out rc.Params, err error) {
    77  	remotes := FileSections()
    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](/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 += `- opt - a dictionary of options to control the configuration
   116      - obscure - declare passwords are plain and need obscuring
   117      - noObscure - declare passwords are already obscured and don't need obscuring
   118      - nonInteractive - don't interact with a user, return questions
   119      - continue - continue the config process with an answer
   120      - all - ask all the config questions not just the post config ones
   121      - state - state to restart with - used with continue
   122      - result - result to restart with - used with continue
   123  `
   124  		}
   125  		rc.Add(rc.Call{
   126  			Path:         "config/" + name,
   127  			AuthRequired: true,
   128  			Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
   129  				return rcConfig(ctx, in, name)
   130  			},
   131  			Title: name + " the config for a remote.",
   132  			Help: `This takes the following parameters:
   133  
   134  - name - name of remote
   135  - parameters - a map of \{ "key": "value" \} pairs
   136  ` + extraHelp + `
   137  
   138  See the [config ` + name + `](/commands/rclone_config_` + name + `/) command for more information on the above.`,
   139  		})
   140  	}
   141  }
   142  
   143  // Manipulate the config file
   144  func rcConfig(ctx context.Context, in rc.Params, what string) (out rc.Params, err error) {
   145  	name, err := in.GetString("name")
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	parameters := rc.Params{}
   150  	err = in.GetStruct("parameters", &parameters)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	var opt UpdateRemoteOpt
   155  	err = in.GetStruct("opt", &opt)
   156  	if err != nil && !rc.IsErrParamNotFound(err) {
   157  		return nil, err
   158  	}
   159  	// Backwards compatibility
   160  	if value, err := in.GetBool("obscure"); err == nil {
   161  		opt.Obscure = value
   162  	}
   163  	if value, err := in.GetBool("noObscure"); err == nil {
   164  		opt.NoObscure = value
   165  	}
   166  	var configOut *fs.ConfigOut
   167  	switch what {
   168  	case "create":
   169  		remoteType, typeErr := in.GetString("type")
   170  		if typeErr != nil {
   171  			return nil, typeErr
   172  		}
   173  		configOut, err = CreateRemote(ctx, name, remoteType, parameters, opt)
   174  	case "update":
   175  		configOut, err = UpdateRemote(ctx, name, parameters, opt)
   176  	case "password":
   177  		err = PasswordRemote(ctx, name, parameters)
   178  	default:
   179  		err = errors.New("unknown rcConfig type")
   180  	}
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  	if !opt.NonInteractive {
   185  		return nil, nil
   186  	}
   187  	if configOut == nil {
   188  		configOut = &fs.ConfigOut{}
   189  	}
   190  	err = rc.Reshape(&out, configOut)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  	return out, nil
   195  }
   196  
   197  func init() {
   198  	rc.Add(rc.Call{
   199  		Path:         "config/delete",
   200  		Fn:           rcDelete,
   201  		Title:        "Delete a remote in the config file.",
   202  		AuthRequired: true,
   203  		Help: `
   204  Parameters:
   205  
   206  - name - name of remote to delete
   207  
   208  See the [config delete](/commands/rclone_config_delete/) command for more information on the above.
   209  `,
   210  	})
   211  }
   212  
   213  // Return the config file delete
   214  func rcDelete(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   215  	name, err := in.GetString("name")
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  	DeleteRemote(name)
   220  	return nil, nil
   221  }
   222  
   223  func init() {
   224  	rc.Add(rc.Call{
   225  		Path:         "config/setpath",
   226  		Fn:           rcSetPath,
   227  		Title:        "Set the path of the config file",
   228  		AuthRequired: true,
   229  		Help: `
   230  Parameters:
   231  
   232  - path - path to the config file to use
   233  `,
   234  	})
   235  }
   236  
   237  // Set the config file path
   238  func rcSetPath(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   239  	path, err := in.GetString("path")
   240  	if err != nil {
   241  		return nil, err
   242  	}
   243  	err = SetConfigPath(path)
   244  	return nil, err
   245  }
   246  
   247  func init() {
   248  	rc.Add(rc.Call{
   249  		Path:         "config/paths",
   250  		Fn:           rcPaths,
   251  		Title:        "Reads the config file path and other important paths.",
   252  		AuthRequired: true,
   253  		Help: `
   254  Returns a JSON object with the following keys:
   255  
   256  - config: path to config file
   257  - cache: path to root of cache directory
   258  - temp: path to root of temporary directory
   259  
   260  Eg
   261  
   262      {
   263          "cache": "/home/USER/.cache/rclone",
   264          "config": "/home/USER/.rclone.conf",
   265          "temp": "/tmp"
   266      }
   267  
   268  See the [config paths](/commands/rclone_config_paths/) command for more information on the above.
   269  `,
   270  	})
   271  }
   272  
   273  // Set the config file path
   274  func rcPaths(ctx context.Context, in rc.Params) (out rc.Params, err error) {
   275  	return rc.Params{
   276  		"config": GetConfigPath(),
   277  		"cache":  GetCacheDir(),
   278  		"temp":   os.TempDir(),
   279  	}, nil
   280  }