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

     1  // Copyright 2020 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  // Binary main is a CAS client.
    16  //
    17  // This is a thin wrapper of remote-apis-sdks to upload/download files from CAS
    18  // efficiently.
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"flag"
    24  	"log"
    25  	"os"
    26  
    27  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/client"
    28  	"github.com/maruel/subcommands"
    29  
    30  	"go.chromium.org/luci/auth"
    31  	"go.chromium.org/luci/auth/client/authcli"
    32  	"go.chromium.org/luci/client/casclient"
    33  	"go.chromium.org/luci/client/cmd/cas/casimpl"
    34  	"go.chromium.org/luci/client/versioncli"
    35  	"go.chromium.org/luci/common/cli"
    36  	"go.chromium.org/luci/common/errors"
    37  	"go.chromium.org/luci/common/logging/gologger"
    38  	"go.chromium.org/luci/hardcoded/chromeinfra"
    39  )
    40  
    41  type authFlags struct {
    42  	flags       authcli.Flags
    43  	defaultOpts auth.Options
    44  	parsedOpts  *auth.Options
    45  }
    46  
    47  func (af *authFlags) Register(f *flag.FlagSet) {
    48  	af.flags.Register(f, af.defaultOpts)
    49  }
    50  
    51  func (af *authFlags) Parse() error {
    52  	opts, err := af.flags.Options()
    53  	if err != nil {
    54  		return err
    55  	}
    56  	af.parsedOpts = &opts
    57  	return nil
    58  }
    59  
    60  func (af *authFlags) NewRBEClient(ctx context.Context, addr string, instance string, readOnly bool) (*client.Client, error) {
    61  	if af.parsedOpts == nil {
    62  		return nil, errors.Reason("AuthFlags.Parse() must be called").Err()
    63  	}
    64  	return casclient.NewLegacy(ctx, addr, instance, *af.parsedOpts, readOnly)
    65  }
    66  
    67  func getApplication() *cli.Application {
    68  	authOpts := chromeinfra.DefaultAuthOptions()
    69  	af := &authFlags{defaultOpts: authOpts}
    70  
    71  	return &cli.Application{
    72  		Name:  "cas",
    73  		Title: "Client tool to access CAS.",
    74  		Context: func(ctx context.Context) context.Context {
    75  			return gologger.StdConfig.Use(ctx)
    76  		},
    77  		Commands: []*subcommands.Command{
    78  			subcommands.CmdHelp,
    79  
    80  			casimpl.CmdArchive(af),
    81  			casimpl.CmdDownload(af),
    82  
    83  			authcli.SubcommandInfo(authOpts, "whoami", false),
    84  			authcli.SubcommandLogin(authOpts, "login", false),
    85  			authcli.SubcommandLogout(authOpts, "logout", false),
    86  
    87  			versioncli.CmdVersion(casimpl.Version),
    88  		},
    89  	}
    90  }
    91  
    92  func main() {
    93  	log.SetFlags(log.Lmicroseconds)
    94  	app := getApplication()
    95  	os.Exit(subcommands.Run(app, nil))
    96  }