go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/cl-util/abandon_cl.go (about) 1 // Copyright 2020 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package main 6 7 import ( 8 "context" 9 "errors" 10 "fmt" 11 12 "github.com/maruel/subcommands" 13 "go.chromium.org/luci/auth" 14 "go.chromium.org/luci/common/logging" 15 "go.chromium.org/luci/common/logging/gologger" 16 17 "go.fuchsia.dev/infra/gerrit" 18 ) 19 20 func cmdAbandonCL(authOpts auth.Options) *subcommands.Command { 21 return &subcommands.Command{ 22 UsageLine: "abandon-cl -host <gerrit-host> -project <gerrit-project> -change-num <change-num>", 23 ShortDesc: "Abandon a CL.", 24 LongDesc: "Abandon a CL.", 25 CommandRun: func() subcommands.CommandRun { 26 c := &abandonCLRun{} 27 c.Init(authOpts) 28 return c 29 }, 30 } 31 } 32 33 type abandonCLRun struct { 34 commonFlags 35 changeNum int64 36 } 37 38 func (c *abandonCLRun) Init(defaultAuthOpts auth.Options) { 39 c.commonFlags.Init(defaultAuthOpts) 40 c.Flags.Int64Var(&c.changeNum, "change-num", 0, "Gerrit change number.") 41 } 42 43 func (c *abandonCLRun) Parse(a subcommands.Application, args []string) error { 44 if err := c.commonFlags.Parse(); err != nil { 45 return err 46 } 47 if c.changeNum == 0 { 48 return errors.New("-change-num is required") 49 } 50 return nil 51 } 52 53 func (c *abandonCLRun) main(a subcommands.Application) error { 54 ctx := context.Background() 55 ctx = logging.SetLevel(ctx, c.logLevel) 56 ctx = gologger.StdConfig.Use(ctx) 57 authClient, err := newAuthClient(ctx, c.parsedAuthOpts) 58 if err != nil { 59 return err 60 } 61 gerritClient, err := gerrit.NewClient(c.gerritHost, c.gerritProject, authClient) 62 if err != nil { 63 return err 64 } 65 return gerritClient.AbandonChange(ctx, c.changeNum) 66 } 67 68 func (c *abandonCLRun) Run(a subcommands.Application, args []string, env subcommands.Env) int { 69 if err := c.Parse(a, args); err != nil { 70 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 71 return 1 72 } 73 74 if err := c.main(a); err != nil { 75 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 76 return 1 77 } 78 return 0 79 }