rc: implement options/blocks,get,set and register options

This commit is contained in:
Nick Craig-Wood 2018-10-27 23:58:18 +01:00
parent 0bfa9811f7
commit b972dcb0ae
7 changed files with 106 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package ftpflags
import (
"github.com/ncw/rclone/cmd/serve/ftp/ftpopt"
"github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/rc"
"github.com/spf13/pflag"
)
@ -13,6 +14,7 @@ var (
// AddFlagsPrefix adds flags for the ftpopt
func AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string, Opt *ftpopt.Options) {
rc.AddOption("ftp", &Opt)
flags.StringVarP(flagSet, &Opt.ListenAddr, prefix+"addr", "", Opt.ListenAddr, "IPaddress:Port or :Port to bind server to.")
flags.StringVarP(flagSet, &Opt.PassivePorts, prefix+"passive-port", "", Opt.PassivePorts, "Passive port range to use.")
flags.StringVarP(flagSet, &Opt.BasicUser, prefix+"user", "", Opt.BasicUser, "User name for authentication.")

View File

@ -3,6 +3,7 @@ package httpflags
import (
"github.com/ncw/rclone/cmd/serve/httplib"
"github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/rc"
"github.com/spf13/pflag"
)
@ -13,6 +14,7 @@ var (
// AddFlagsPrefix adds flags for the httplib
func AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string, Opt *httplib.Options) {
rc.AddOption(prefix+"http", &Opt)
flags.StringVarP(flagSet, &Opt.ListenAddr, prefix+"addr", "", Opt.ListenAddr, "IPaddress:Port or :Port to bind server to.")
flags.DurationVarP(flagSet, &Opt.ServerReadTimeout, prefix+"server-read-timeout", "", Opt.ServerReadTimeout, "Timeout for server reading data")
flags.DurationVarP(flagSet, &Opt.ServerWriteTimeout, prefix+"server-write-timeout", "", Opt.ServerWriteTimeout, "Timeout for server writing data")

View File

@ -12,6 +12,7 @@ import (
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/rc"
"github.com/spf13/pflag"
)
@ -31,6 +32,7 @@ var (
// AddFlags adds the non filing system specific flags to the command
func AddFlags(flagSet *pflag.FlagSet) {
rc.AddOption("main", fs.Config)
// NB defaults which aren't the zero for the type should be set in fs/config.go NewConfig
flags.CountVarP(flagSet, &verbose, "verbose", "v", "Print lots more stuff (repeat for more)")
flags.BoolVarP(flagSet, &quiet, "quiet", "q", false, "Print as little stuff as possible")

View File

@ -4,6 +4,7 @@ package filterflags
import (
"github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/filter"
"github.com/ncw/rclone/fs/rc"
"github.com/spf13/pflag"
)
@ -14,6 +15,7 @@ var (
// AddFlags adds the non filing system specific flags to the command
func AddFlags(flagSet *pflag.FlagSet) {
rc.AddOption("filter", &Opt)
flags.BoolVarP(flagSet, &Opt.DeleteExcluded, "delete-excluded", "", false, "Delete files on dest excluded from sync")
flags.StringArrayVarP(flagSet, &Opt.FilterRule, "filter", "f", nil, "Add a file-filtering rule")
flags.StringArrayVarP(flagSet, &Opt.FilterFrom, "filter-from", "", nil, "Read filtering patterns from a file")

95
fs/rc/config.go Normal file
View File

@ -0,0 +1,95 @@
// 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 (
"github.com/pkg/errors"
)
var optionBlock = map[string]interface{}{}
// AddOption adds an option set
func AddOption(name string, option interface{}) {
optionBlock[name] = option
}
func init() {
Add(Call{
Path: "options/blocks",
Fn: rcOptionsBlocks,
Title: "List all the option blocks",
Help: `Returns
- options - a list of the options block names`,
})
}
// Show the list of all the option blocks
func rcOptionsBlocks(in Params) (out Params, err error) {
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,
Title: "Get all the options",
Help: `Returns an object where keys are option block names and values are an
object with the current option values in.
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
func rcOptionsGet(in Params) (out Params, err error) {
out = make(Params)
for name, options := range optionBlock {
out[name] = options
}
return out, nil
}
func init() {
Add(Call{
Path: "options/set",
Fn: rcOptionsSet,
Title: "Set an option",
Help: `Parameters
- 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.
`,
})
}
// Set an option in an option block
func rcOptionsSet(in Params) (out Params, err error) {
for name, options := range in {
current := optionBlock[name]
if current == nil {
return nil, errors.Errorf("unknown option block %q", name)
}
err := Reshape(current, options)
if err != nil {
return nil, errors.Wrapf(err, "failed to write options from block %q", name)
}
}
return out, nil
}

View File

@ -15,6 +15,7 @@ var (
// AddFlags adds the remote control flags to the flagSet
func AddFlags(flagSet *pflag.FlagSet) {
rc.AddOption("rc", &Opt)
flags.BoolVarP(flagSet, &Opt.Enabled, "rc", "", false, "Enable the remote control server.")
flags.StringVarP(flagSet, &Opt.Files, "rc-files", "", "", "Serve these files on the HTTP server.")
httpflags.AddFlagsPrefix(flagSet, "rc-", &Opt.HTTPOptions)

View File

@ -3,6 +3,7 @@ package vfsflags
import (
"github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/rc"
"github.com/ncw/rclone/vfs"
"github.com/spf13/pflag"
)
@ -14,6 +15,7 @@ var (
// AddFlags adds the non filing system specific flags to the command
func AddFlags(flagSet *pflag.FlagSet) {
rc.AddOption("vfs", &Opt)
flags.BoolVarP(flagSet, &Opt.NoModTime, "no-modtime", "", Opt.NoModTime, "Don't read/write the modification time (can speed things up).")
flags.BoolVarP(flagSet, &Opt.NoChecksum, "no-checksum", "", Opt.NoChecksum, "Don't compare checksums on up/download.")
flags.BoolVarP(flagSet, &Opt.NoSeek, "no-seek", "", Opt.NoSeek, "Don't allow seeking in files.")