forked from TrueCloudLab/rclone
backend: add --json flag to always output JSON
This commit is contained in:
parent
5eb558e058
commit
995cd0dc32
1 changed files with 18 additions and 8 deletions
|
@ -18,12 +18,14 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
options []string
|
options []string
|
||||||
|
useJSON bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd.Root.AddCommand(commandDefinition)
|
cmd.Root.AddCommand(commandDefinition)
|
||||||
cmdFlags := commandDefinition.Flags()
|
cmdFlags := commandDefinition.Flags()
|
||||||
flags.StringArrayVarP(cmdFlags, &options, "option", "o", options, "Option in the form name=value or name.")
|
flags.StringArrayVarP(cmdFlags, &options, "option", "o", options, "Option in the form name=value or name.")
|
||||||
|
flags.BoolVarP(cmdFlags, &useJSON, "json", "", useJSON, "Always output in JSON format.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var commandDefinition = &cobra.Command{
|
var commandDefinition = &cobra.Command{
|
||||||
|
@ -97,15 +99,23 @@ Note to run these commands on a running backend then see
|
||||||
|
|
||||||
}
|
}
|
||||||
// Output the result
|
// Output the result
|
||||||
switch x := out.(type) {
|
writeJSON := false
|
||||||
case nil:
|
if useJSON {
|
||||||
case string:
|
writeJSON = true
|
||||||
fmt.Println(out)
|
} else {
|
||||||
case []string:
|
switch x := out.(type) {
|
||||||
for _, line := range x {
|
case nil:
|
||||||
fmt.Println(line)
|
case string:
|
||||||
|
fmt.Println(out)
|
||||||
|
case []string:
|
||||||
|
for _, line := range x {
|
||||||
|
fmt.Println(line)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
writeJSON = true
|
||||||
}
|
}
|
||||||
default:
|
}
|
||||||
|
if writeJSON {
|
||||||
// Write indented JSON to the output
|
// Write indented JSON to the output
|
||||||
enc := json.NewEncoder(os.Stdout)
|
enc := json.NewEncoder(os.Stdout)
|
||||||
enc.SetIndent("", "\t")
|
enc.SetIndent("", "\t")
|
||||||
|
|
Loading…
Reference in a new issue