Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse JSON-style reports in golang side #18159

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cli/src/alluxio.org/cli/cmd/info/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var Report = &ReportCommand{

type ReportCommand struct {
*env.BaseJavaCommand
format string
}

func (c *ReportCommand) Base() *env.BaseJavaCommand {
Expand All @@ -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
}

Expand All @@ -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})
}
15 changes: 14 additions & 1 deletion cli/src/alluxio.org/cli/env/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading