go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cli/app.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/auth"
    23  	"go.chromium.org/luci/auth/client/authcli"
    24  	"go.chromium.org/luci/common/api/gerrit"
    25  	"go.chromium.org/luci/common/cli"
    26  	"go.chromium.org/luci/common/flag/fixflagpos"
    27  	"go.chromium.org/luci/common/logging/gologger"
    28  )
    29  
    30  // Params is the parameters for the bb application.
    31  type Params struct {
    32  	DefaultBuildbucketHost string
    33  	Auth                   auth.Options
    34  }
    35  
    36  // application creates the application and configures its subcommands.
    37  // Ignores p.Auth.Scopes.
    38  func application(p Params) *cli.Application {
    39  	p.Auth.Scopes = []string{
    40  		auth.OAuthScopeEmail,
    41  		gerrit.OAuthScope,
    42  	}
    43  
    44  	return &cli.Application{
    45  		Name:  "bb",
    46  		Title: "A CLI client for buildbucket.",
    47  		Context: func(ctx context.Context) context.Context {
    48  			return gologger.StdConfig.Use(ctx)
    49  		},
    50  		Commands: []*subcommands.Command{
    51  			cmdAdd(p),
    52  			cmdGet(p),
    53  			cmdLS(p),
    54  			cmdLog(p),
    55  			cmdCancel(p),
    56  			cmdBatch(p),
    57  			cmdCollect(p),
    58  
    59  			{},
    60  			cmdBuilders(p),
    61  
    62  			{},
    63  			authcli.SubcommandLogin(p.Auth, "auth-login", false),
    64  			authcli.SubcommandLogout(p.Auth, "auth-logout", false),
    65  			authcli.SubcommandInfo(p.Auth, "auth-info", false),
    66  
    67  			{},
    68  			subcommands.CmdHelp,
    69  		},
    70  	}
    71  }
    72  
    73  // Main is the main function of the bb application.
    74  func Main(p Params, args []string) int {
    75  	// if subcommand is ls, transform "-$N" into "-n $N".
    76  	if len(args) > 1 && args[0] == "ls" {
    77  		for i, a := range args {
    78  			if len(a) >= 2 && a[0] == '-' && a[1] >= '0' && a[1] <= '9' {
    79  				args = append(args, "")
    80  				copy(args[i+1:], args[i:])
    81  				args[i+1] = args[i][1:]
    82  				args[i] = "-n"
    83  				break
    84  			}
    85  		}
    86  	}
    87  
    88  	return subcommands.Run(application(p), fixflagpos.FixSubcommands(args))
    89  }