From bd10344d653325c41378ae0974e93c80d70190cd Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 8 Jun 2019 08:45:55 +0100 Subject: [PATCH] rc: add --loopback flag to run commands directly without a server --- cmd/rc/rc.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/rc/rc.go b/cmd/rc/rc.go index 0f89f00a8..40cd23371 100644 --- a/cmd/rc/rc.go +++ b/cmd/rc/rc.go @@ -24,6 +24,7 @@ var ( jsonInput = "" authUser = "" authPass = "" + loopback = false ) func init() { @@ -33,6 +34,7 @@ func init() { commandDefintion.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.") commandDefintion.Flags().StringVarP(&authUser, "user", "", "", "Username to use to rclone remote control.") commandDefintion.Flags().StringVarP(&authPass, "pass", "", "", "Password to use to connect to rclone remote control.") + commandDefintion.Flags().BoolVarP(&loopback, "loopback", "", false, "If set connect to this rclone instance not via HTTP.") } var commandDefintion = &cobra.Command{ @@ -58,6 +60,12 @@ The --json parameter can be used to pass in a JSON blob as an input instead of key=value arguments. This is the only way of passing in more complicated values. +Use --loopback to connect to the rclone instance running "rclone rc". +This is very useful for testing commands without having to run an +rclone rc server, eg: + + rclone rc --loopback operations/about fs=/ + Use "rclone rc" to see a list of all possible commands.`, Run: func(command *cobra.Command, args []string) { cmd.CheckArgs(0, 1E9, command, args) @@ -102,6 +110,15 @@ func setAlternateFlag(flagName string, output *string) { // // if err is set, out may be a valid error return or it may be nil func doCall(path string, in rc.Params) (out rc.Params, err error) { + // If loopback set, short circuit HTTP request + if loopback { + call := rc.Calls.Get(path) + if call == nil { + return nil, errors.Errorf("method %q not found", path) + } + return call.Fn(in) + } + // Do HTTP request client := fshttp.NewClient(fs.Config) url += path