config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Implement config options reading and writing
  2. //
  3. // This is done here rather than in fs/fs.go so we don't cause a circular dependency
  4. package rc
  5. import (
  6. "context"
  7. "fmt"
  8. "github.com/rclone/rclone/fs"
  9. "github.com/rclone/rclone/fs/filter"
  10. )
  11. var (
  12. optionBlock = map[string]interface{}{}
  13. optionReload = map[string]func(context.Context) error{}
  14. )
  15. // AddOption adds an option set
  16. func AddOption(name string, option interface{}) {
  17. optionBlock[name] = option
  18. }
  19. // AddOptionReload adds an option set with a reload function to be
  20. // called when options are changed
  21. func AddOptionReload(name string, option interface{}, reload func(context.Context) error) {
  22. optionBlock[name] = option
  23. optionReload[name] = reload
  24. }
  25. func init() {
  26. Add(Call{
  27. Path: "options/blocks",
  28. Fn: rcOptionsBlocks,
  29. Title: "List all the option blocks",
  30. Help: `Returns:
  31. - options - a list of the options block names`,
  32. })
  33. }
  34. // Show the list of all the option blocks
  35. func rcOptionsBlocks(ctx context.Context, in Params) (out Params, err error) {
  36. options := []string{}
  37. for name := range optionBlock {
  38. options = append(options, name)
  39. }
  40. out = make(Params)
  41. out["options"] = options
  42. return out, nil
  43. }
  44. func init() {
  45. Add(Call{
  46. Path: "options/get",
  47. Fn: rcOptionsGet,
  48. Title: "Get all the global options",
  49. Help: `Returns an object where keys are option block names and values are an
  50. object with the current option values in.
  51. Note that these are the global options which are unaffected by use of
  52. the _config and _filter parameters. If you wish to read the parameters
  53. set in _config then use options/config and for _filter use options/filter.
  54. This shows the internal names of the option within rclone which should
  55. map to the external options very easily with a few exceptions.
  56. `,
  57. })
  58. }
  59. // Show the list of all the option blocks
  60. func rcOptionsGet(ctx context.Context, in Params) (out Params, err error) {
  61. out = make(Params)
  62. for name, options := range optionBlock {
  63. out[name] = options
  64. }
  65. return out, nil
  66. }
  67. func init() {
  68. Add(Call{
  69. Path: "options/local",
  70. Fn: rcOptionsLocal,
  71. Title: "Get the currently active config for this call",
  72. Help: `Returns an object with the keys "config" and "filter".
  73. The "config" key contains the local config and the "filter" key contains
  74. the local filters.
  75. Note that these are the local options specific to this rc call. If
  76. _config was not supplied then they will be the global options.
  77. Likewise with "_filter".
  78. This call is mostly useful for seeing if _config and _filter passing
  79. is working.
  80. This shows the internal names of the option within rclone which should
  81. map to the external options very easily with a few exceptions.
  82. `,
  83. })
  84. }
  85. // Show the current config
  86. func rcOptionsLocal(ctx context.Context, in Params) (out Params, err error) {
  87. out = make(Params)
  88. out["config"] = fs.GetConfig(ctx)
  89. out["filter"] = filter.GetConfig(ctx).Opt
  90. return out, nil
  91. }
  92. func init() {
  93. Add(Call{
  94. Path: "options/set",
  95. Fn: rcOptionsSet,
  96. Title: "Set an option",
  97. Help: `Parameters:
  98. - option block name containing an object with
  99. - key: value
  100. Repeated as often as required.
  101. Only supply the options you wish to change. If an option is unknown
  102. it will be silently ignored. Not all options will have an effect when
  103. changed like this.
  104. For example:
  105. This sets DEBUG level logs (-vv) (these can be set by number or string)
  106. rclone rc options/set --json '{"main": {"LogLevel": "DEBUG"}}'
  107. rclone rc options/set --json '{"main": {"LogLevel": 8}}'
  108. And this sets INFO level logs (-v)
  109. rclone rc options/set --json '{"main": {"LogLevel": "INFO"}}'
  110. And this sets NOTICE level logs (normal without -v)
  111. rclone rc options/set --json '{"main": {"LogLevel": "NOTICE"}}'
  112. `,
  113. })
  114. }
  115. // Set an option in an option block
  116. func rcOptionsSet(ctx context.Context, in Params) (out Params, err error) {
  117. for name, options := range in {
  118. current := optionBlock[name]
  119. if current == nil {
  120. return nil, fmt.Errorf("unknown option block %q", name)
  121. }
  122. err := Reshape(current, options)
  123. if err != nil {
  124. return nil, fmt.Errorf("failed to write options from block %q: %w", name, err)
  125. }
  126. if reload := optionReload[name]; reload != nil {
  127. err = reload(ctx)
  128. if err != nil {
  129. return nil, fmt.Errorf("failed to reload options from block %q: %w", name, err)
  130. }
  131. }
  132. }
  133. return out, nil
  134. }