From 45d5339fcb12f48dddf3ee238c7c3cb7f878a30e Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 26 Oct 2018 14:45:44 +0100 Subject: [PATCH] cmd/rc: add --json flag for structured JSON input --- cmd/rc/rc.go | 33 +++++++++++++++++++++++++-------- docs/content/rc.md | 19 +++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/cmd/rc/rc.go b/cmd/rc/rc.go index cdfec97af..25e4f8b0b 100644 --- a/cmd/rc/rc.go +++ b/cmd/rc/rc.go @@ -19,14 +19,16 @@ import ( ) var ( - noOutput = false - url = "http://localhost:5572/" + noOutput = false + url = "http://localhost:5572/" + jsonInput = "" ) func init() { cmd.Root.AddCommand(commandDefintion) commandDefintion.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.") commandDefintion.Flags().StringVarP(&url, "url", "", url, "URL to connect to rclone remote control.") + commandDefintion.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.") } var commandDefintion = &cobra.Command{ @@ -40,6 +42,10 @@ Arguments should be passed in as parameter=value. The result will be returned as a JSON object by default. +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 "rclone rc" to see a list of all possible commands.`, Run: func(command *cobra.Command, args []string) { cmd.CheckArgs(0, 1E9, command, args) @@ -115,13 +121,24 @@ func run(args []string) (err error) { // parse input in := make(rc.Params) - for _, param := range args[1:] { - equals := strings.IndexRune(param, '=') - if equals < 0 { - return errors.Errorf("No '=' found in parameter %q", param) + params := args[1:] + if jsonInput == "" { + for _, param := range params { + equals := strings.IndexRune(param, '=') + if equals < 0 { + return errors.Errorf("no '=' found in parameter %q", param) + } + key, value := param[:equals], param[equals+1:] + in[key] = value + } + } else { + if len(params) > 0 { + return errors.New("can't use --json and parameters together") + } + err = json.Unmarshal([]byte(jsonInput), &in) + if err != nil { + return errors.Wrap(err, "bad --json input") } - key, value := param[:equals], param[equals+1:] - in[key] = value } // Do the call diff --git a/docs/content/rc.md b/docs/content/rc.md index 80cee1e54..0997a1d0f 100644 --- a/docs/content/rc.md +++ b/docs/content/rc.md @@ -67,6 +67,25 @@ $ rclone rc rc/noop param1=one param2=two Run `rclone rc` on its own to see the help for the installed remote control commands. +`rclone rc` also supports a `--json` flag which can be used to send +more complicated input parameters. + +``` +$ rclone rc --json '{ "p1": [1,"2",null,4], "p2": { "a":1, "b":2 } }' rc/noop +{ + "p1": [ + 1, + "2", + null, + 4 + ], + "p2": { + "a": 1, + "b": 2 + } +} +``` + ## Supported commands ### cache/expire: Purge a remote from cache