2018-10-28 06:58:18 +08:00
|
|
|
// Implement config options reading and writing
|
|
|
|
//
|
|
|
|
// This is done here rather than in fs/fs.go so we don't cause a circular dependency
|
|
|
|
|
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"fmt"
|
2019-06-17 16:34:30 +08:00
|
|
|
|
2020-12-13 00:55:57 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2018-10-28 06:58:18 +08:00
|
|
|
)
|
|
|
|
|
2019-03-06 20:36:51 +08:00
|
|
|
var (
|
|
|
|
optionBlock = map[string]interface{}{}
|
2020-11-27 01:15:52 +08:00
|
|
|
optionReload = map[string]func(context.Context) error{}
|
2019-03-06 20:36:51 +08:00
|
|
|
)
|
2018-10-28 06:58:18 +08:00
|
|
|
|
|
|
|
// AddOption adds an option set
|
|
|
|
func AddOption(name string, option interface{}) {
|
|
|
|
optionBlock[name] = option
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:36:51 +08:00
|
|
|
// AddOptionReload adds an option set with a reload function to be
|
|
|
|
// called when options are changed
|
2020-11-27 01:15:52 +08:00
|
|
|
func AddOptionReload(name string, option interface{}, reload func(context.Context) error) {
|
2019-03-06 20:36:51 +08:00
|
|
|
optionBlock[name] = option
|
|
|
|
optionReload[name] = reload
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:58:18 +08:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "options/blocks",
|
|
|
|
Fn: rcOptionsBlocks,
|
|
|
|
Title: "List all the option blocks",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: `Returns:
|
2018-10-28 06:58:18 +08:00
|
|
|
- options - a list of the options block names`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the list of all the option blocks
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcOptionsBlocks(ctx context.Context, in Params) (out Params, err error) {
|
2018-10-28 06:58:18 +08:00
|
|
|
options := []string{}
|
|
|
|
for name := range optionBlock {
|
|
|
|
options = append(options, name)
|
|
|
|
}
|
|
|
|
out = make(Params)
|
|
|
|
out["options"] = options
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "options/get",
|
|
|
|
Fn: rcOptionsGet,
|
2020-12-13 00:55:57 +08:00
|
|
|
Title: "Get all the global options",
|
2018-10-28 06:58:18 +08:00
|
|
|
Help: `Returns an object where keys are option block names and values are an
|
|
|
|
object with the current option values in.
|
|
|
|
|
2020-12-13 00:55:57 +08:00
|
|
|
Note that these are the global options which are unaffected by use of
|
|
|
|
the _config and _filter parameters. If you wish to read the parameters
|
|
|
|
set in _config then use options/config and for _filter use options/filter.
|
|
|
|
|
2018-10-28 06:58:18 +08:00
|
|
|
This shows the internal names of the option within rclone which should
|
|
|
|
map to the external options very easily with a few exceptions.
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the list of all the option blocks
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcOptionsGet(ctx context.Context, in Params) (out Params, err error) {
|
2018-10-28 06:58:18 +08:00
|
|
|
out = make(Params)
|
|
|
|
for name, options := range optionBlock {
|
|
|
|
out[name] = options
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 00:55:57 +08:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "options/local",
|
|
|
|
Fn: rcOptionsLocal,
|
|
|
|
Title: "Get the currently active config for this call",
|
|
|
|
Help: `Returns an object with the keys "config" and "filter".
|
|
|
|
The "config" key contains the local config and the "filter" key contains
|
|
|
|
the local filters.
|
|
|
|
|
|
|
|
Note that these are the local options specific to this rc call. If
|
|
|
|
_config was not supplied then they will be the global options.
|
|
|
|
Likewise with "_filter".
|
|
|
|
|
|
|
|
This call is mostly useful for seeing if _config and _filter passing
|
|
|
|
is working.
|
|
|
|
|
|
|
|
This shows the internal names of the option within rclone which should
|
|
|
|
map to the external options very easily with a few exceptions.
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the current config
|
|
|
|
func rcOptionsLocal(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
out = make(Params)
|
|
|
|
out["config"] = fs.GetConfig(ctx)
|
2021-02-23 18:33:03 +08:00
|
|
|
out["filter"] = filter.GetConfig(ctx).Opt
|
2020-12-13 00:55:57 +08:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:58:18 +08:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "options/set",
|
|
|
|
Fn: rcOptionsSet,
|
|
|
|
Title: "Set an option",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: `Parameters:
|
2018-10-28 06:58:18 +08:00
|
|
|
|
|
|
|
- option block name containing an object with
|
|
|
|
- key: value
|
|
|
|
|
|
|
|
Repeated as often as required.
|
|
|
|
|
|
|
|
Only supply the options you wish to change. If an option is unknown
|
|
|
|
it will be silently ignored. Not all options will have an effect when
|
|
|
|
changed like this.
|
2019-02-03 19:18:34 +08:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2020-12-12 01:48:09 +08:00
|
|
|
This sets DEBUG level logs (-vv) (these can be set by number or string)
|
2019-02-03 19:18:34 +08:00
|
|
|
|
2020-12-12 01:48:09 +08:00
|
|
|
rclone rc options/set --json '{"main": {"LogLevel": "DEBUG"}}'
|
2019-02-03 19:18:34 +08:00
|
|
|
rclone rc options/set --json '{"main": {"LogLevel": 8}}'
|
|
|
|
|
|
|
|
And this sets INFO level logs (-v)
|
|
|
|
|
2020-12-12 01:48:09 +08:00
|
|
|
rclone rc options/set --json '{"main": {"LogLevel": "INFO"}}'
|
2019-02-03 19:18:34 +08:00
|
|
|
|
|
|
|
And this sets NOTICE level logs (normal without -v)
|
|
|
|
|
2020-12-12 01:48:09 +08:00
|
|
|
rclone rc options/set --json '{"main": {"LogLevel": "NOTICE"}}'
|
2018-10-28 06:58:18 +08:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an option in an option block
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcOptionsSet(ctx context.Context, in Params) (out Params, err error) {
|
2018-10-28 06:58:18 +08:00
|
|
|
for name, options := range in {
|
|
|
|
current := optionBlock[name]
|
|
|
|
if current == nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("unknown option block %q", name)
|
2018-10-28 06:58:18 +08:00
|
|
|
}
|
|
|
|
err := Reshape(current, options)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to write options from block %q: %w", name, err)
|
2018-10-28 06:58:18 +08:00
|
|
|
}
|
2019-03-06 20:36:51 +08:00
|
|
|
if reload := optionReload[name]; reload != nil {
|
2020-11-27 01:15:52 +08:00
|
|
|
err = reload(ctx)
|
2019-03-06 20:36:51 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to reload options from block %q: %w", name, err)
|
2019-03-06 20:36:51 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 06:58:18 +08:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|