diff --git a/cli/src/alluxio.org/cli/cmd/info/report.go b/cli/src/alluxio.org/cli/cmd/info/report.go index 6b792018ae9a..14fe7e0d3bfd 100644 --- a/cli/src/alluxio.org/cli/cmd/info/report.go +++ b/cli/src/alluxio.org/cli/cmd/info/report.go @@ -32,6 +32,7 @@ var Report = &ReportCommand{ type ReportCommand struct { *env.BaseJavaCommand + format string } func (c *ReportCommand) Base() *env.BaseJavaCommand { @@ -56,6 +57,8 @@ Defaults to summary if no arg is provided return c.Run(args) }, }) + cmd.Flags().StringVar(&c.format, "format", "json", + "Set output format, any of [json, yaml]") return cmd } @@ -78,5 +81,8 @@ func (c *ReportCommand) Run(args []string) error { reportArg = args[0] } // TODO: output all in a serializable format and filter/trim as specified by flags - return c.Base().Run([]string{reportArg}) + if c.format != "json" && c.format != "yaml" { + return stacktrace.NewError("Invalid format %v, must be one of [json, yaml]", c.format) + } + return c.Base().RunAndFormat(c.format, nil, []string{reportArg}) } diff --git a/cli/src/alluxio.org/cli/env/command.go b/cli/src/alluxio.org/cli/env/command.go index a2ee82f3e25b..32e13e98fa93 100644 --- a/cli/src/alluxio.org/cli/env/command.go +++ b/cli/src/alluxio.org/cli/env/command.go @@ -114,7 +114,20 @@ func (c *BaseJavaCommand) RunWithIO(args []string, stdin io.Reader, stdout, stde func (c *BaseJavaCommand) RunAndFormat(format string, stdin io.Reader, args []string) error { switch strings.ToLower(format) { case "json": - return c.RunWithIO(args, stdin, os.Stdout, os.Stderr) + buf := &bytes.Buffer{} + if err := c.RunWithIO(args, stdin, buf, os.Stderr); err != nil { + io.Copy(os.Stdout, buf) + return err + } + var obj json.RawMessage + if err := json.Unmarshal(buf.Bytes(), &obj); err != nil { + return stacktrace.Propagate(err, "error unmarshalling json from java command") + } + prettyJson, err := json.MarshalIndent(obj, "", " ") + if err != nil { + return stacktrace.Propagate(err, "error marshalling json to pretty format") + } + os.Stdout.Write(append(prettyJson, '\n')) case "yaml": buf := &bytes.Buffer{} if err := c.RunWithIO(args, stdin, buf, os.Stderr); err != nil {