go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cli/cancel.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 "fmt" 20 "strings" 21 22 "github.com/maruel/subcommands" 23 24 "go.chromium.org/luci/common/cli" 25 26 pb "go.chromium.org/luci/buildbucket/proto" 27 ) 28 29 func cmdCancel(p Params) *subcommands.Command { 30 return &subcommands.Command{ 31 UsageLine: `cancel [flags] [BUILD [BUILD...]]`, 32 ShortDesc: "cancel builds", 33 LongDesc: doc(` 34 Cancel builds. 35 36 Argument BUILD can be an int64 build id or a string 37 <project>/<bucket>/<builder>/<build_number>, e.g. 38 chromium/ci/linux-rel/1. 39 If no builds were specified on the command line, they are read 40 from stdin. 41 `), 42 CommandRun: func() subcommands.CommandRun { 43 r := &cancelRun{} 44 r.RegisterDefaultFlags(p) 45 r.RegisterFieldFlags() 46 r.Flags.StringVar(&r.reason, "reason", "", doc(` 47 reason of cancellation in Markdown format; required 48 `)) 49 return r 50 }, 51 } 52 } 53 54 type cancelRun struct { 55 printRun 56 reason string 57 } 58 59 func (r *cancelRun) Run(a subcommands.Application, args []string, env subcommands.Env) int { 60 ctx := cli.GetContext(a, r, env) 61 if err := r.initClients(ctx, nil); err != nil { 62 return r.done(ctx, err) 63 } 64 65 r.reason = strings.TrimSpace(r.reason) 66 if r.reason == "" { 67 return r.done(ctx, fmt.Errorf("-reason is required")) 68 } 69 70 fields, err := r.FieldMask() 71 if err != nil { 72 return r.done(ctx, err) 73 } 74 75 return r.PrintAndDone(ctx, args, argOrder, func(ctx context.Context, arg string) (*pb.Build, error) { 76 id, err := r.retrieveBuildID(ctx, arg) 77 if err != nil { 78 return nil, err 79 } 80 81 req := &pb.CancelBuildRequest{ 82 Id: id, 83 SummaryMarkdown: r.reason, 84 Fields: fields, 85 } 86 return r.buildsClient.CancelBuild(ctx, req, expectedCodeRPCOption) 87 }) 88 }