github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/rc/config.go (about)

     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  
     5  package rc
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  var (
    14  	optionBlock  = map[string]interface{}{}
    15  	optionReload = map[string]func() error{}
    16  )
    17  
    18  // AddOption adds an option set
    19  func AddOption(name string, option interface{}) {
    20  	optionBlock[name] = option
    21  }
    22  
    23  // AddOptionReload adds an option set with a reload function to be
    24  // called when options are changed
    25  func AddOptionReload(name string, option interface{}, reload func() error) {
    26  	optionBlock[name] = option
    27  	optionReload[name] = reload
    28  }
    29  
    30  func init() {
    31  	Add(Call{
    32  		Path:  "options/blocks",
    33  		Fn:    rcOptionsBlocks,
    34  		Title: "List all the option blocks",
    35  		Help: `Returns
    36  - options - a list of the options block names`,
    37  	})
    38  }
    39  
    40  // Show the list of all the option blocks
    41  func rcOptionsBlocks(ctx context.Context, in Params) (out Params, err error) {
    42  	options := []string{}
    43  	for name := range optionBlock {
    44  		options = append(options, name)
    45  	}
    46  	out = make(Params)
    47  	out["options"] = options
    48  	return out, nil
    49  }
    50  
    51  func init() {
    52  	Add(Call{
    53  		Path:  "options/get",
    54  		Fn:    rcOptionsGet,
    55  		Title: "Get all the options",
    56  		Help: `Returns an object where keys are option block names and values are an
    57  object with the current option values in.
    58  
    59  This shows the internal names of the option within rclone which should
    60  map to the external options very easily with a few exceptions.
    61  `,
    62  	})
    63  }
    64  
    65  // Show the list of all the option blocks
    66  func rcOptionsGet(ctx context.Context, in Params) (out Params, err error) {
    67  	out = make(Params)
    68  	for name, options := range optionBlock {
    69  		out[name] = options
    70  	}
    71  	return out, nil
    72  }
    73  
    74  func init() {
    75  	Add(Call{
    76  		Path:  "options/set",
    77  		Fn:    rcOptionsSet,
    78  		Title: "Set an option",
    79  		Help: `Parameters
    80  
    81  - option block name containing an object with
    82    - key: value
    83  
    84  Repeated as often as required.
    85  
    86  Only supply the options you wish to change.  If an option is unknown
    87  it will be silently ignored.  Not all options will have an effect when
    88  changed like this.
    89  
    90  For example:
    91  
    92  This sets DEBUG level logs (-vv)
    93  
    94      rclone rc options/set --json '{"main": {"LogLevel": 8}}'
    95  
    96  And this sets INFO level logs (-v)
    97  
    98      rclone rc options/set --json '{"main": {"LogLevel": 7}}'
    99  
   100  And this sets NOTICE level logs (normal without -v)
   101  
   102      rclone rc options/set --json '{"main": {"LogLevel": 6}}'
   103  `,
   104  	})
   105  }
   106  
   107  // Set an option in an option block
   108  func rcOptionsSet(ctx context.Context, in Params) (out Params, err error) {
   109  	for name, options := range in {
   110  		current := optionBlock[name]
   111  		if current == nil {
   112  			return nil, errors.Errorf("unknown option block %q", name)
   113  		}
   114  		err := Reshape(current, options)
   115  		if err != nil {
   116  			return nil, errors.Wrapf(err, "failed to write options from block %q", name)
   117  		}
   118  		if reload := optionReload[name]; reload != nil {
   119  			err = reload()
   120  			if err != nil {
   121  				return nil, errors.Wrapf(err, "failed to reload options from block %q", name)
   122  			}
   123  		}
   124  	}
   125  	return out, nil
   126  }