go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cmd/dump_build/main.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Command dump_build is a simple CLI debugging toolV which reads a binary Build
    16  // message (with optional zlib compression) and then dumps the decoded build as
    17  // JSONPB to stdout.
    18  package main
    19  
    20  import (
    21  	"bytes"
    22  	"compress/zlib"
    23  	"io"
    24  	"os"
    25  
    26  	bbpb "go.chromium.org/luci/buildbucket/proto"
    27  	"google.golang.org/protobuf/encoding/protojson"
    28  	"google.golang.org/protobuf/proto"
    29  )
    30  
    31  func main() {
    32  	raw, err := io.ReadAll(os.Stdin)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	if raw[0] == 0x78 { // zlib magic
    38  		r, err := zlib.NewReader(bytes.NewReader(raw))
    39  		if err != nil {
    40  			panic(err)
    41  		}
    42  		raw, err = io.ReadAll(r)
    43  		if err != nil {
    44  			panic(err)
    45  		}
    46  	}
    47  
    48  	build := &bbpb.Build{}
    49  	if err = proto.Unmarshal(raw, build); err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	if _, err = os.Stdout.WriteString(protojson.Format(build)); err != nil {
    54  		panic(err)
    55  	}
    56  	if _, err = os.Stdout.WriteString("\n"); err != nil {
    57  		panic(err)
    58  	}
    59  }