forked from TrueCloudLab/rclone
cmd/rc: add --json flag for structured JSON input
This commit is contained in:
parent
bb5637d46a
commit
45d5339fcb
2 changed files with 44 additions and 8 deletions
21
cmd/rc/rc.go
21
cmd/rc/rc.go
|
@ -21,12 +21,14 @@ import (
|
||||||
var (
|
var (
|
||||||
noOutput = false
|
noOutput = false
|
||||||
url = "http://localhost:5572/"
|
url = "http://localhost:5572/"
|
||||||
|
jsonInput = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd.Root.AddCommand(commandDefintion)
|
cmd.Root.AddCommand(commandDefintion)
|
||||||
commandDefintion.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.")
|
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(&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{
|
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 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.`,
|
Use "rclone rc" to see a list of all possible commands.`,
|
||||||
Run: func(command *cobra.Command, args []string) {
|
Run: func(command *cobra.Command, args []string) {
|
||||||
cmd.CheckArgs(0, 1E9, command, args)
|
cmd.CheckArgs(0, 1E9, command, args)
|
||||||
|
@ -115,14 +121,25 @@ func run(args []string) (err error) {
|
||||||
|
|
||||||
// parse input
|
// parse input
|
||||||
in := make(rc.Params)
|
in := make(rc.Params)
|
||||||
for _, param := range args[1:] {
|
params := args[1:]
|
||||||
|
if jsonInput == "" {
|
||||||
|
for _, param := range params {
|
||||||
equals := strings.IndexRune(param, '=')
|
equals := strings.IndexRune(param, '=')
|
||||||
if equals < 0 {
|
if equals < 0 {
|
||||||
return errors.Errorf("No '=' found in parameter %q", param)
|
return errors.Errorf("no '=' found in parameter %q", param)
|
||||||
}
|
}
|
||||||
key, value := param[:equals], param[equals+1:]
|
key, value := param[:equals], param[equals+1:]
|
||||||
in[key] = value
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do the call
|
// Do the call
|
||||||
out, callErr := doCall(path, in)
|
out, callErr := doCall(path, in)
|
||||||
|
|
|
@ -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
|
Run `rclone rc` on its own to see the help for the installed remote
|
||||||
control commands.
|
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
|
## Supported commands
|
||||||
<!--- autogenerated start - run make rcdocs - don't edit here -->
|
<!--- autogenerated start - run make rcdocs - don't edit here -->
|
||||||
### cache/expire: Purge a remote from cache
|
### cache/expire: Purge a remote from cache
|
||||||
|
|
Loading…
Reference in a new issue