mirror of
https://github.com/rclone/rclone.git
synced 2024-12-23 07:29:35 +01:00
cmd/rc: add --json flag for structured JSON input
This commit is contained in:
parent
bb5637d46a
commit
45d5339fcb
33
cmd/rc/rc.go
33
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
|
||||
|
@ -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
|
||||
<!--- autogenerated start - run make rcdocs - don't edit here -->
|
||||
### cache/expire: Purge a remote from cache
|
||||
|
Loading…
Reference in New Issue
Block a user