Skip to content

Commit

Permalink
bb execute: add response JSON file flag (#7970)
Browse files Browse the repository at this point in the history
Allow writing the `ExecuteResponse` to a path specified by
`-response_json_file`. This makes it possible to access metadata,
outputs, etc.
  • Loading branch information
bduffany authored Dec 2, 2024
1 parent 70ef6f7 commit 0b39287
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cli/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package execute

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -44,7 +45,8 @@ var (
// Note: bazel has remote_default_exec_properties but it has somewhat
// confusing semantics, so we call this "exec_properties" to avoid
// confusion.
execProperties = flag.New(flags, "exec_properties", []string{}, "Platform exec property, as a `NAME=VALUE` pair. Can be specified more than once.")
execProperties = flag.New(flags, "exec_properties", []string{}, "Platform exec property, as a `NAME=VALUE` pair. Can be specified more than once.")
responseJSONFile = flags.String("response_json_file", "", "If set, write the JSON-serialized ExecuteResponse to this path.")
)

const (
Expand Down Expand Up @@ -203,13 +205,18 @@ func execute(cmdArgs []string) error {
log.Debugf("Downloaded results in %s", time.Since(stageStart))
log.Debugf("End-to-end execution time: %s", time.Since(start))

executionMetadata := rsp.ExecuteResponse.GetResult().GetExecutionMetadata()
if b, err := protojson.Marshal(executionMetadata); err == nil {
log.Debugf("Execution metadata: %s", string(b))
}

os.Stdout.Write(res.Stdout)
os.Stderr.Write(res.Stderr)

if *responseJSONFile != "" {
b, err := protojson.Marshal(rsp.ExecuteResponse)
if err != nil {
return fmt.Errorf("marshal response JSON: %w", err)
}
if err := os.WriteFile(*responseJSONFile, b, 0644); err != nil {
return fmt.Errorf("write response JSON file: %w", err)
}
}

return nil
}

0 comments on commit 0b39287

Please sign in to comment.