go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cli/get.go (about)

     1  // Copyright 2016 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  package cli
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/maruel/subcommands"
    21  
    22  	"go.chromium.org/luci/buildbucket/protoutil"
    23  	"go.chromium.org/luci/common/cli"
    24  
    25  	pb "go.chromium.org/luci/buildbucket/proto"
    26  )
    27  
    28  func cmdGet(p Params) *subcommands.Command {
    29  	return &subcommands.Command{
    30  		UsageLine: `get [flags] [BUILD [BUILD...]]`,
    31  		ShortDesc: "print builds",
    32  		LongDesc: doc(`
    33  			Print builds.
    34  
    35  			Argument BUILD can be an int64 build id or a string
    36  			<project>/<bucket>/<builder>/<build_number>, e.g.
    37  			chromium/ci/linux-rel/1.
    38  			If no builds were specified on the command line, they are read
    39  			from stdin.
    40  		`),
    41  		CommandRun: func() subcommands.CommandRun {
    42  			r := &getRun{}
    43  			r.RegisterDefaultFlags(p)
    44  			r.RegisterFieldFlags()
    45  			return r
    46  		},
    47  	}
    48  }
    49  
    50  type getRun struct {
    51  	printRun
    52  }
    53  
    54  func (r *getRun) Run(a subcommands.Application, args []string, env subcommands.Env) int {
    55  	ctx := cli.GetContext(a, r, env)
    56  	if err := r.initClients(ctx, nil); err != nil {
    57  		return r.done(ctx, err)
    58  	}
    59  
    60  	fields, err := r.FieldMask()
    61  	if err != nil {
    62  		return r.done(ctx, err)
    63  	}
    64  
    65  	return r.PrintAndDone(ctx, args, argOrder, func(ctx context.Context, arg string) (*pb.Build, error) {
    66  		req, err := protoutil.ParseGetBuildRequest(arg)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		req.Fields = fields
    71  		return r.buildsClient.GetBuild(ctx, req, expectedCodeRPCOption)
    72  	})
    73  }